我似乎无法从以下 foreach 代码中得到任何结果。我似乎无法递归地遍历该数组,也无法制作该数组的较小数组。我只想从数组中挑选数据并将数据放在更小的数组中。
PHP代码
$ii = 0;
foreach ($array as $key => $value)
{
$vv = $value;
if($vv== 'Alpha')
{
$ii++;
$list[$ii]['info'] = $vv; // my new array
}
}
PHP print_r($array)
Array
(
[0] => Array
(
[moredetails] => Array
(
[moredetails] => Array
(
[0] => Array
(
[productId] => 9999
[productName] => Name
[productType] => Alpha
[serialNumber] =>
[serviceItemId] => 123456
)
[1] => Array
(
[productId] => 8888
[productName] => Name
[productType] => Beta
[serialNumber] =>
[serviceItemId] => 123456
)
)
)
)
)
理想结果
Array
(
[0]
(
[productID] => 9999
[productName] => Name
{productType] => Alpha
)
)
找到解决方案!
function miniArray($array, &$simple, $ii)
{
foreach($array as $key => $value)
{
if(is_array($value)){
$ii++;
miniArray($value, $simple, $ii);
}
else
{
$simple[$ii][$key] = $value;
}
}
}
miniArray($service, $simple_array, 0);