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.

2
  • Please add this in eval.in Commented Aug 6, 2017 at 11:47
  • you can simply use array_column() to do that Commented Aug 6, 2017 at 12:34

3 Answers 3

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');
Sign up to request clarification or add additional context in comments.

Comments

0

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

2 Comments

This just still gives me all of the data
wait, you just want to filter out all selectioIds into a new array?
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);

1 Comment

Nothing seems to load for that?

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.