We help IT Professionals succeed at work.

Find values in multidimensional associative array

Colin Brazier
on
253 Views
Last Modified: 2012-02-15
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
                )
       )
)
Comment
Watch Question

CERTIFIED EXPERT

Commented:
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

Author

Commented:
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
CERTIFIED EXPERT

Commented:
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

CERTIFIED EXPERT
Commented:
This one is on us!
(Get your first solution completely free - no credit card required)
UNLOCK SOLUTION

Author

Commented:
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

Gain unlimited access to on-demand training courses with an Experts Exchange subscription.

Get Access
Why Experts Exchange?

Experts Exchange always has the answer, or at the least points me in the correct direction! It is like having another employee that is extremely experienced.

Jim Murphy
Programmer at Smart IT Solutions

When asked, what has been your best career decision?

Deciding to stick with EE.

Mohamed Asif
Technical Department Head

Being involved with EE helped me to grow personally and professionally.

Carl Webster
CTP, Sr Infrastructure Consultant
Empower Your Career
Did You Know?

We've partnered with two important charities to provide clean water and computer science education to those who need it most. READ MORE

Ask ANY Question

Connect with Certified Experts to gain insight and support on specific technology challenges including:

  • Troubleshooting
  • Research
  • Professional Opinions
Unlock the solution to this question.
Join our community and discover your potential

Experts Exchange is the only place where you can interact directly with leading experts in the technology field. Become a member today and access the collective knowledge of thousands of technology experts.

*This site is protected by reCAPTCHA and the Google Privacy Policy and Terms of Service apply.

OR

Please enter a first name

Please enter a last name

8+ characters (letters, numbers, and a symbol)

By clicking, you agree to the Terms of Use and Privacy Policy.