如何使用 php 在 foreach 中使用键和值?

发布于 2024-11-16 06:34:13 字数 779 浏览 0 评论 0原文

我有以下friendDetails数组输出,

数组 ( [0] =>大批 ( [id] => 1 [名字] =>阿鲁恩 [姓氏] =>苏库马尔 [照片] => .jpg )

[1] => Array
    (
        [id] => 2
        [first_name] => senthilkumar
        [last_name] => Kumar
        [photo] => jpg
    )

) 我使用下面的代码来获得最终输出

  foreach($friendDetails as $value){
       array_push($friendList, $value[id].".".$value[photo]."-".$value[first_name]." ".$value[last_name]);
  }

最终输出将是,

Array
(
    [0] => 1.jpg-Aruun Sukumar
    [1] => 2.jpg-senthilkumar Kumar
    [2] => 18.jpg-senthilkumar sugumar
)

这里我收到带有确切输出的通知错误。我在代码上做错了什么? 还有其他方法可以获得最终输出吗?

I am having the following friendDetails array output,

Array
(
[0] => Array
(
[id] => 1
[first_name] => Aruun
[last_name] => Sukumar
[photo] => jpg
)

[1] => Array
    (
        [id] => 2
        [first_name] => senthilkumar
        [last_name] => Kumar
        [photo] => jpg
    )

)
I use the following piece of code to to get final output

  foreach($friendDetails as $value){
       array_push($friendList, $value[id].".".$value[photo]."-".$value[first_name]." ".$value[last_name]);
  }

Final output will be,

Array
(
    [0] => 1.jpg-Aruun Sukumar
    [1] => 2.jpg-senthilkumar Kumar
    [2] => 18.jpg-senthilkumar sugumar
)

Here I am getting Notice Error with exact output. What i done wrong on the code?
Is there any other way to get Final Output ?

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(6

寄居人 2024-11-23 06:34:13

您会收到通知错误,因为您没有将数组的键放在引号中。

它应该是:

foreach($friendDetails as $value){                       
    array_push($friendList, $value['id'].".".$value['photo']."-".$value['first_name']." ".$value['last_name']);                    
}

参见http://php.net/manual/en/language。类型.array.php

You get the notice error as you are not putting the keys of your array in quotes.

It should be:

foreach($friendDetails as $value){                       
    array_push($friendList, $value['id'].".".$value['photo']."-".$value['first_name']." ".$value['last_name']);                    
}

see http://php.net/manual/en/language.types.array.php

む无字情书 2024-11-23 06:34:13

您需要在关键标识符周围加上引号:

$value['id'] . "." . $value['photo']

等等。请参阅“为什么 $foo[bar] 错误?”在 http://php.net/manual/en/language.types.array。 php

You need to put quotes around your key identifiers:

$value['id'] . "." . $value['photo']

etc. See "Why is $foo[bar] wrong?" at http://php.net/manual/en/language.types.array.php

黄昏下泛黄的笔记 2024-11-23 06:34:13

试试这个你会得到键和值:

  foreach ($friendDetails as $key_name => $key_value) {
   print "Key = " . $key_name . " Value = " . $key_value . "<BR>";
  }

Try this you will get both key and value:

  foreach ($friendDetails as $key_name => $key_value) {
   print "Key = " . $key_name . " Value = " . $key_value . "<BR>";
  }
╰◇生如夏花灿烂 2024-11-23 06:34:13

有两件事:

  • 您需要在 array_push 中的数组键周围加上引号 (")(即 $value["id"]
  • 确保您定义了 $friendList 作为 foreach 之前的数组

<?php

$friendDetails = array(
  array(
    'id' => 1,
    'first_name' => 'Aruun',
    'last_name' => 'Sukumar',
    'photo' => 'jpg'
  ),
  array(
    'id' => 2,
    'first_name' => 'senthilkumar',
    'last_name' => 'Kumar',
    'photo' => 'jpg'
  )
);

$friendList = array();

foreach($friendDetails as $value){                       
 array_push($friendList, $value["id"].".".$value["photo"]."-".$value["first_name"]." ".$value["last_name"]);                    
}

print_r($friendList);

?>

Two things:

  • You need to put quotes (") around the array keys in the array_push (i.e. $value["id"])
  • Make sure that you define $friendList as an array before the foreach.

A working example:

<?php

$friendDetails = array(
  array(
    'id' => 1,
    'first_name' => 'Aruun',
    'last_name' => 'Sukumar',
    'photo' => 'jpg'
  ),
  array(
    'id' => 2,
    'first_name' => 'senthilkumar',
    'last_name' => 'Kumar',
    'photo' => 'jpg'
  )
);

$friendList = array();

foreach($friendDetails as $value){                       
 array_push($friendList, $value["id"].".".$value["photo"]."-".$value["first_name"]." ".$value["last_name"]);                    
}

print_r($friendList);

?>
那小子欠揍 2024-11-23 06:34:13

在数组值周围使用引号:

foreach($friendDetails as $value){                         
   array_push($friendList, $value['id'].".".$value['photo']."-".$value['first_name']." ".$value['last_name']);                     
} 

Use quotations around your array values:

foreach($friendDetails as $value){                         
   array_push($friendList, $value['id'].".".$value['photo']."-".$value['first_name']." ".$value['last_name']);                     
} 
泪痕残 2024-11-23 06:34:13
$friendList = array();    
foreach($friendDetails as $key=> $value){                       
        $friendList[] =  $value['id'].".".$value['photo']."-".$value['first_name']." ".$value['last_name']);                    
    }
$friendList = array();    
foreach($friendDetails as $key=> $value){                       
        $friendList[] =  $value['id'].".".$value['photo']."-".$value['first_name']." ".$value['last_name']);                    
    }
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文