我正在将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.
)
)
)
)
)
)
)
)
)
我正试图得到这样的数据!
我有两个阵列"customer"one_answers"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
jon
基里托
源代码检查多维数组中是否存在特定的数组键-PHP