PHP-嵌套foreach循环内多维数组键的字符串值
我正在尝试获取最里面数组的键的字符串值,但是,PHP-嵌套foreach循环内多维数组键的字符串值,php,arrays,multidimensional-array,foreach,Php,Arrays,Multidimensional Array,Foreach,我正在尝试获取最里面数组的键的字符串值,但是,食品和饮料,糖果&茶具或咖啡套装只需作为数组返回即可 <?php $categoryParents = array( 'Food & Drink' => array( 'Beverages' => array( 'Energy & Sports Drinks', 'Wine & Champagne' ), 'Candy' =>
食品和饮料
,糖果
&茶具或咖啡套装
只需作为数组
返回即可
<?php
$categoryParents = array(
'Food & Drink' => array(
'Beverages' => array(
'Energy & Sports Drinks',
'Wine & Champagne'
),
'Candy' => array(
'Chewing Gum',
'Lollipops',
'Jelly Beans'
),
'Candy Dishes',
'Food Gifts',
'Fruit',
'Popcorn',
'Pretzels',
'Seeds',
'Spices',
'Tea Or Coffee Sets' => array(
'Coffee',
'Tea'
),
'Water'
)
);
foreach ($categoryParents as $pcat => $psubcats) {
foreach ($psubcats as $psubcat) {
if (is_array($psubcat) && sizeof($psubcat) > 0) {
print_r($psubcat . " <-- parent test \n");
foreach ($psubcat as $psub2cat) {
print_r($psub2cat . " <-- sub \n");
}
} else {
print_r($psubcat . "<-- parent \n");
}
}
}
?>
此脚本在我的计算机上的输出如下:
PHP Notice: Array to string conversion in /Users/guy/Desktop/test-2.php on line 34
Notice: Array to string conversion in /Users/guy/Desktop/test-2.php on line 34
Array <-- parent test
Energy & Sports Drinks <-- sub
Wine & Champagne <-- sub
PHP Notice: Array to string conversion in /Users/guy/Desktop/test-2.php on line 34
Notice: Array to string conversion in /Users/guy/Desktop/test-2.php on line 34
Array <-- parent test
Chewing Gum <-- sub
Lollipops <-- sub
Jelly Beans <-- sub
Candy Dishes<-- parent
Food Gifts<-- parent
Fruit<-- parent
Popcorn<-- parent
Pretzels<-- parent
Seeds<-- parent
Spices<-- parent
PHP Notice: Array to string conversion in /Users/guy/Desktop/test-2.php on line 34
Notice: Array to string conversion in /Users/guy/Desktop/test-2.php on line 34
Array <-- parent test
Coffee <-- sub
Tea <-- sub
Water<-- parent
PHP注意:第34行/Users/guy/Desktop/test-2.PHP中的数组到字符串转换
注意:第34行/Users/guy/Desktop/test-2.php中的数组到字符串转换
数组写入此片段以便打印数组键
foreach ($psubcats as $k => $psubcat) {
if (is_array($psubcat) && sizeof($psubcat) > 0) {
print_r($k . " <-- parent test \n");
foreach($psubcat作为$k=>$psubcat){
如果(is_阵列($psubcat)&sizeof($psubcat)>0){
印刷费($k)。”