Php Json数组,从每个数组键获取第一个值
我试图做的是获取每个数组键,然后将第一个“价格”和“数量”与每个数组键相关联。 我的阵列:Php Json数组,从每个数组键获取第一个值,php,arrays,json,Php,Arrays,Json,我试图做的是获取每个数组键,然后将第一个“价格”和“数量”与每个数组键相关联。 我的阵列: { "32557580": { "bids": [ { "price": 2500, "quantity": 38265661 }, { "price": 2439, "quantity": 57414444 }, { "price": 2381,
{
"32557580": {
"bids": [
{
"price": 2500,
"quantity": 38265661
},
{
"price": 2439,
"quantity": 57414444
},
{
"price": 2381,
"quantity": 1092179
},
{
"price": 2174,
"quantity": 27140648
}
],
"offers": [
{
"price": 2564,
"quantity": 33634591
},
{
"price": 2597,
"quantity": 27842125
},
{
"price": 2632,
"quantity": 925092
},
{
"price": 2667,
"quantity": 3565173
},
{
"price": 2778,
"quantity": 27589980
}
]
},
"32557581": {
"bids": [
{
"price": 4854,
"quantity": 33786947
},
{
"price": 4808,
"quantity": 22881344
},
{
"price": 4762,
"quantity": 2513747
},
{
"price": 4717,
"quantity": 2650000
},
{
"price": 4587,
"quantity": 15714786
}
],
"offers": [
{
"price": 4950,
"quantity": 31749492
},
{
"price": 5000,
"quantity": 3193999
},
{
"price": 5051,
"quantity": 2292463
},
{
"price": 5102,
"quantity": 34770816
},
{
"price": 5128,
"quantity": 2605693
}
]
},
"32557582": {
"bids": [
{
"price": 2532,
"quantity": 60354703
},
{
"price": 2500,
"quantity": 113667648
},
{
"price": 2439,
"quantity": 5125100
},
{
"price": 2222,
"quantity": 120803051
}
],
"offers": [
{
"price": 2564,
"quantity": 1492990
},
{
"price": 2597,
"quantity": 22121811
},
{
"price": 2632,
"quantity": 42119270
},
{
"price": 2667,
"quantity": 43680406
},
{
"price": 2703,
"quantity": 1176966
}
]
}
}
例如:
Id: 32557580
Price: 2500
Quantity: 38265661
Id: 32557581
Price: 4854
Quantity: 33786947
等等
这就是我目前得到的
$obj = json_decode($result,true);
$all_keys = array_keys($obj);
foreach ($all_keys as $key => $value) {
echo 'Id: '.$value.'<br>';
}
我真的不确定从这里走到哪里,我试着看了很多问题和答案。
我尝试在我得到的一个foreach中添加另一个foreach,但我只从第一个数组键获得了价格和数量
非常感谢所有帮助我走上正轨的人 只需循环主数组,并从
bids
获取第一个数组,然后使用该数组:
foreach($obj as $key => $value) {
$first = reset($value['bids']);
echo "ID: $key<br />";
echo "Price: " . $first['price'] . "<br />";
echo "Quantity: " . $first['quantity'] . "<br />";
}
只需循环主数组,从
bids
获取第一个数组,然后使用该数组:
foreach($obj as $key => $value) {
$first = reset($value['bids']);
echo "ID: $key<br />";
echo "Price: " . $first['price'] . "<br />";
echo "Quantity: " . $first['quantity'] . "<br />";
}
哦这是有道理的。谢谢。:)哦这是有道理的。谢谢。:)
foreach($obj as $key => $value) {
echo "ID: $key<br />";
echo "Price: " . $value['bids'][0]['price'] . "<br />";
echo "Quantity: " . $value['bids'][0]['quantity'] . "<br />";
}