我发现我找不到想要的输出。
我有JSON原始数据,包含以下内容:
Array
(
[data] => Array
(
[0] => Array
(
[title] => currency1
[tickers] => Array
(
[0] => USD
)
)
[1] => Array
(
[title] => currency2
[tickers] => Array
(
[0] => USD
[1] => EUR
)
)
[2] => Array
(
[title] => currency3
[tickers] => Array
(
[0] => USD
)
)
)
)这就是我试过的
$num =0;
foreach ($json_data as $key => $story){
$num++;
foreach($story as $subkey => $subvalue){
if(is_array($subvalue)){
$title = $story[$subkey]['title'];
$tickers = $story[$subkey]['tickers'][$num];
foreach($tickers as $key2 => $val2){
if($val2>=1){
unset($story);
}else{
//echo output
}
}
}
}我想要得到数组的所有键,如果滴答有多个值,不要回显它。
示例所需输出:
curreny1 Key is 0 and tickers is USD
curreny3 Key is 2 and tickers is USD发布于 2021-05-17 01:11:44
您应该能够只循环遍历源数据的data键,提取标题和代码,并输出结果(如果代码号的计数为1):
foreach ($json_data['data'] as $key => $story) {
$tickers = $story['tickers'];
if (count($tickers) != 1) continue;
$title = $story['title'];
echo "$title Key is $key and tickers is {$tickers[0]}\n";
}输出(用于样本数据):
currency1 Key is 0 and tickers is USD
currency3 Key is 2 and tickers is USDhttps://stackoverflow.com/questions/67562736
复制相似问题