0

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
}
]
}

My code looks like this:

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

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.

BCLtd
  • 1,459
  • 2
  • 18
  • 45

3 Answers3

2

just use php function array_column() like this :

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

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 : 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');
Community
  • 1
  • 1
yoeunes
  • 2,927
  • 2
  • 15
  • 26
0

You have to loop throw all runners element and then aggregate them in another array.

$selectionIds =  array();
for($i=0; $i<count($arr['runners']) ; $i++)
{
array_push($selectionIds, $arr[0]['runners'][$i]->selectionId);
}
var_dump($selectionIds);
MD Ruhul Amin
  • 4,386
  • 1
  • 22
  • 37
0

It should be $arr[0]['runners'][0]['selectionId']

Sasa Blagojevic
  • 2,110
  • 17
  • 22