PHP JSON特定于数组的键 [英] PHP JSON Specific Key To Array

查看:106
本文介绍了PHP JSON特定于数组的键的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在尝试仅从我的json响应中取回selectionId并将它们放置在数组中,但是我无法管理它.我的json响应为

I am trying to bring back just selectionId's and place them in an array from my json response, but I just can't manage it. I have my json response as

[
{
marketId: "1.133102670",
marketName: "2m Hrd",
totalMatched: 39556.86,
runners: [
{
selectionId: 13764229,
runnerName: "Kereman",
handicap: 0,
sortPriority: 1
},
{
selectionId: 11378898,
runnerName: "City Dreamer",
handicap: 0,
sortPriority: 2
},
{
selectionId: 12279462,
runnerName: "Costa Percy",
handicap: 0,
sortPriority: 3
},
{
selectionId: 11316530,
runnerName: "Platos Kode",
handicap: 0,
sortPriority: 4
},
{
selectionId: 11177700,
runnerName: "Parkwarden",
handicap: 0,
sortPriority: 5
}
]
},
{
marketId: "1.133102480",
marketName: "7f Nov Stks",
totalMatched: 23164.32,
runners: [
{
selectionId: 13428423,
runnerName: "Delph Crescent",
handicap: 0,
sortPriority: 1
},
{
selectionId: 13079071,
runnerName: "Red Force One",
handicap: 0,
sortPriority: 2
},
{
selectionId: 13372659,
runnerName: "Porth Swtan",
handicap: 0,
sortPriority: 3
},
{
selectionId: 12943079,
runnerName: "Zoffalee",
handicap: 0,
sortPriority: 4
},
{
selectionId: 13373353,
runnerName: "Snooker Jim",
handicap: 0,
sortPriority: 5
},
{
selectionId: 13763129,
runnerName: "Lineofintelligence",
handicap: 0,
sortPriority: 6
},
{
selectionId: 13437954,
runnerName: "Surrender",
handicap: 0,
sortPriority: 7
},
{
selectionId: 13605452,
runnerName: "Cum Spiro Spero",
handicap: 0,
sortPriority: 8
}
]
}

我的代码如下:

$arr = json_decode($jsonResponse, true);
echo $arr['runners'][0]['selectionId'];

关于我哪里出错了,甚至如何解决这个问题的所有想法,以带回阵列中的所有选择ID.

Any ideas where I am going wrong or even how I can fix this to bring back all selection ID's in an array.

在此先感谢您提供的帮助或建议.

Thanks in advance for any help or advice given.

推荐答案

只需使用php函数

just use php function array_column() like this :

array array_column(array $ input,混合$ column_key [,混合 $ index_key = null])

array array_column ( array $input , mixed $column_key [, mixed $index_key = null ] )

array_column()返回输入的单个列中的值, 由column_key标识.

array_column() returns the values from a single column of the input, identified by the column_key.

输入:一个多维数组或对象数组,可从中提取一列值.

input : A multi-dimensional array or an array of objects from which to pull a column of values from.

column_key :要返回的值列.此值可以是您要检索的列的整数键,也可以是关联数组或属性名称的字符串键名.

column_key : The column of values to return. This value may be an integer key of the column you wish to retrieve, or it may be a string key name for an associative array or property name.

$arr = json_decode($jsonResponse, true);
$selectionIds = array_column($records, 'selectionId');

这篇关于PHP JSON特定于数组的键的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆