Link to home
Start Free TrialLog in
Avatar of Colin Brazier
Colin BrazierFlag for United Kingdom of Great Britain and Northern Ireland

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
                )
       )
)
Avatar of Beverley Portlock
Beverley Portlock
Flag of United Kingdom of Great Britain and Northern Ireland image

Seems to work for me. YOu had defined $myarray and where using $myArray, but when i changed that it was OK. Tested code and output below

<?php


$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'
                )
       )
);


    foreach($myarray  as $date => $date1):
       foreach($date1 as $team => $team1):
            echo $team1['title_team']."<br />";
       endforeach;
    endforeach;

Open in new window


xTeam
yTeam
zTeam
xTeam
yTeam
zTeam
Avatar of Colin Brazier

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
If you want unique values then build a results array and output anything that is not already stored in the array.

    $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;

 

Open in new window


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/>";
            }
       }
    }

 

Open in new window

ASKER CERTIFIED SOLUTION
Avatar of Beverley Portlock
Beverley Portlock
Flag of United Kingdom of Great Britain and Northern Ireland image

Link to home
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
Start Free Trial
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