我有这个多维数组:
Array (
[0] => Array (
[id] => 1
[list_name] => List_Red
)
[1] => Array (
[id] => 2
[list_name] => List_Blue
)
)
...我想创建一个仅包含 [id] 的新数组。
如果你们能帮助我,我将非常感激^^
提前致谢。
最佳答案
@fabrik 你的解决方案确实有效,但它也是不正确的,因为 PHP 会抛出一个 E_WARNING 告诉你你正在附加到一个尚不存在的数组。 始终在使用变量之前对其进行初始化。
$newList = array();
foreach($myList as $listItem) {
$newList[$listItem['id']] = $listItem['list_name'];
}
现在这是您的所有 list_name 的列表,格式如下。
Array (
1 => List_Red
2 => List_Blue
)
您使用起来更加容易,现在您可以像这样迭代它..
foreach($newList as $itemID => $itemName) {
echo "Item ID: $itemID - Item Name: $itemName<br>";
}
关于php - 从多维数组创建新数组,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/3351567/