[英]How to pull a specific key data from multidimensional array
我正在将XML文件转换为关联数组以提取数据,问题是我必须根据数组编号进行10次循环才能获取数据。
有没有更好的方法来获取特定的列数据而不创建很多循环? 因为我想将它们分配给变量。
我试图从中获取数据的数组
Array
(
[catalog] => Array
(
[comp] => Array
(
[0] => Array
(
[look] => Array
(
[shp] => Array
(
[wok] => Array
(
[group] => Array
(
[customer] => Array
(
[author] => jack
[title] => Midnight Rain1
[genre] => Fantasy
[price] => 5.95
[publish_date] => 2000-12-16
[description] => A former architect battles corporate zombies.
)
[customer2] => Array
(
[author] => lemass
[title] => Midnight Rain1
[genre] => Fantasy
[price] => 5.95
[publish_date] => 2000-12-16
[description] => A former architect battles corporate zombies.
)
)
)
)
)
)
[1] => Array
(
[look] => Array
(
[shp] => Array
(
[wok] => Array
(
[group] => Array
(
[customer] => Array
(
[author] => jon
[title] => Midnight Rain1
[genre] => Fantasy
[price] => 5.95
[publish_date] => 2000-12-16
[description] => A former architect battles corporate zombies.
)
[customer2] => Array
(
[author] => kirito
[title] => Midnight Rain1
[genre] => Fantasy
[price] => 5.95
[publish_date] => 2000-12-16
[description] => A former architect battles corporate zombies.
)
)
)
)
)
)
)
)
)
我正在尝试获取这样的数据!
我有2个数组“ customer”和“ customer1”
我想要这样的数据
客户=>作者
输出
jack
jon
因为它们在客户群中
它有可能做到这一点?
假设您的数组存储在$arr
您将访问comp
索引,然后对其进行循环,因为它们是数字索引。 然后,您有一个阵列可以减少更多内容。 数组结构似乎使这一切膨胀了,但可以正常工作
$arr; //Set this to your converted xml
$comps = $arr['catalog']['comp'];
foreach($comps as $comp){
echo $comp['look']['shp']['wok']['group']['customer']['author'];
}
<?php
$aVar = Array
(
'catalog' => Array
(
'comp' => Array
(
0 => Array
(
'look' => Array
(
'shp' => Array
(
'wok' => Array
(
'group' => Array
(
'customer' => Array
(
'author' => 'jack',
'title' => 'Midnight Rain1',
'genre' => 'Fantasy',
'price' => 5.95,
'publish_date' => '2000-12-16',
'description' => 'A former architect battles corporate zombies.'
),
'customer2' => Array
(
'author' => 'lemass',
'title' => 'Midnight Rain1',
'genre' => 'Fantasy',
'price' => 5.95,
'publish_date' => '2000-12-16',
'description' => 'A former architect battles corporate zombies.'
)
)
)
)
)
),
1 => Array
(
'look' => Array
(
'shp' => Array
(
'wok' => Array
(
'group' => Array
(
'customer' => Array
(
'author' => 'jon',
'title' => 'Midnight Rain1',
'genre' => 'Fantasy',
'price' => 5.95,
'publish_date' => '2000-12-16',
'description' => 'A former architect battles corporate zombies.'
),
'customer2' => Array
(
'author' => 'kirito',
'title' => 'Midnight Rain1',
'genre' => 'Fantasy',
'price' => 5.95,
'publish_date' => '2000-12-16',
'description' => 'A former architect battles corporate zombies.'
)
)
)
)
)
)
)
)
);
function findKey($array, $keySearch) {
foreach ($array as $key => $item) {
if ($key === $keySearch) {
echo $item . '<br>';
//return true; // if just the first is wanted
} else if (is_array($item) && findKey($item, $keySearch)) {
return true;
}
}
return false;
}
findKey($aVar, 'author');
打印输出:
插口
lemass
乔恩
基里托
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.