Colin Brazier
asked on
Find values in multidimensional associative array
Hi experts,
I have been struggling with multidimensional arrays for a while now and have worked out most solutions but I would like your help with this. Hopefully it's very simple. I have been as clear as I can but if you have any questions I'll be pleased to explain.
From the attached multidimensional associative array, I need to find each different value of title_team. They are repeated for each date. So I just need to look at any instance of the date level (eg. the first one) but I don't know the key of the date.
At the moment I have this but of course it keeps going through every date!
Thanks for looking.
Col
foreach($myArray as $date => $date1):
foreach($date1 as $team => $team1):
echo $team1['title_team']."<br />";
endforeach;
endforeach;
$myArray =
Array
(
[Saturday February 18 2012] => Array
(
[x] => Array
(
[title_team] => xTeam
[title_oppo] => Eltham Palace
[td_class] => away_game
)
[y] => Array
(
[title_team] => yTeam
[title_oppo] => To be advised
[td_class] => home_game
)
[z] => Array
(
[title_team] => zTeam
[title_oppo] => State Street
[td_class] => away_game
)
)
[Sunday February 19 2012] => Array
(
[x] => Array
(
[title_team] => xTeam
[title_oppo] => Old Roan
[td_class] => home_game
)
[y] => Array
(
[title_team] => yTeam
[title_oppo] => Heathfield
[td_class] => home_game
)
[z] => Array
(
[title_team] => zTeam
[title_oppo] => Knights
[td_class] => away_game
)
)
)
I have been struggling with multidimensional arrays for a while now and have worked out most solutions but I would like your help with this. Hopefully it's very simple. I have been as clear as I can but if you have any questions I'll be pleased to explain.
From the attached multidimensional associative array, I need to find each different value of title_team. They are repeated for each date. So I just need to look at any instance of the date level (eg. the first one) but I don't know the key of the date.
At the moment I have this but of course it keeps going through every date!
Thanks for looking.
Col
foreach($myArray as $date => $date1):
foreach($date1 as $team => $team1):
echo $team1['title_team']."<br />";
endforeach;
endforeach;
$myArray =
Array
(
[Saturday February 18 2012] => Array
(
[x] => Array
(
[title_team] => xTeam
[title_oppo] => Eltham Palace
[td_class] => away_game
)
[y] => Array
(
[title_team] => yTeam
[title_oppo] => To be advised
[td_class] => home_game
)
[z] => Array
(
[title_team] => zTeam
[title_oppo] => State Street
[td_class] => away_game
)
)
[Sunday February 19 2012] => Array
(
[x] => Array
(
[title_team] => xTeam
[title_oppo] => Old Roan
[td_class] => home_game
)
[y] => Array
(
[title_team] => yTeam
[title_oppo] => Heathfield
[td_class] => home_game
)
[z] => Array
(
[title_team] => zTeam
[title_oppo] => Knights
[td_class] => away_game
)
)
)
ASKER
Thanks for responding, and so quickly. I was typing the example out so typos were probable!
What I need is just
xTeam
yTeam
zTeam.
I don't need to iterate thru the date level but don't know how to code just for one instance of it, if you get my drift.
Col
What I need is just
xTeam
yTeam
zTeam.
I don't need to iterate thru the date level but don't know how to code just for one instance of it, if you get my drift.
Col
If you want unique values then build a results array and output anything that is not already stored in the array.
Produces
xTeam
yTeam
zTeam
You might also want to consider using a more standard notation as more people are familiar with it. It is also clearer IMO.
$results = array();
foreach($myarray as $date => $date1):
foreach($date1 as $team => $team1):
$text = $team1['title_team'];
if ( ! in_array( $text, $results ) ) {
$results [ $text ] = $text;
echo "$text<br/>";
}
endforeach;
endforeach;
Produces
xTeam
yTeam
zTeam
You might also want to consider using a more standard notation as more people are familiar with it. It is also clearer IMO.
$results = array();
foreach($myarray as $date => $date1) {
foreach($date1 as $team => $team1) {
$text = $team1['title_team'];
if ( ! in_array( $text, $results ) ) {
$results [ $text ] = $text;
echo "$text<br/>";
}
}
}
ASKER CERTIFIED SOLUTION
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
ASKER
That's it! Many thanks. I know I shouldn't have regularly repeating items in my array but that's another story/question.
Much appreciated.
Col
Much appreciated.
Col
Open in new window
xTeam
yTeam
zTeam
xTeam
yTeam
zTeam