PHP Multidimensional array problem

by 6 replies
7
Hi Warrior PHP experts; here is my problem.

I have an api that returns an array, $services.

This command: print_r($services);

produces this:

Array ( [blogger] => Array ( [name] => Blogger [methods] => Array ( [0] => blog [1] => images ) ) [posterous] => Array ( [name] => Posterous [methods] => Array ( [0] => blog [1] => microblog [2] => status [3] => images ) ) [tumblr] => Array ( [name] => Tumblr [methods] => Array ( [0] => blog [1] => images ) ) [typepad] => Array ( [name] => TypePad [methods] => Array ( [0] => blog [1] => microblog [2] => status ) ) [vox] => Array ( [name] => Vox [methods] => Array ( [0] => blog ) ) [wordpress] => Array ( [name] => WordPress.com [methods] => Array ( [0] => blog [1] => microblog [2] => status ) ) [xanga] => Array ( [name] => Xanga [methods] => Array ( [0] => blog [1] => microblog [2] => status ) ) )

My question is this - how do I programmatically get to the strings in the [methods] strings above array, ie 'blog' , 'microblog', 'status'
#programming #array #multidimensional #php #problem
  • It depends how and why you want to get to the strings. If you want to 'look up' a particular string or just loop through them all for some reason.

    Say your variable holding the array is actually called $services.... this code would print out the items recursively through each dimension of the array, to 3 levels:

    PHP Code:
    foreach ($services as $first) {
        echo 
    "<b>1st dimension: $first</b><br>";
        foreach (
    $first as $second) {
            echo 
    "2nd dimension: $second<br>";
            foreach (
    $second as $third) {
                echo 
    "3rd dimension: $third<br>";
            }
        }

    It helps if you can visualize the array a little better. This is a nicer way to see that array:

    Code:
    Array (
        [blogger] => Array (
            [name] => Blogger 
            [methods] => Array (
                [0] => blog
                [1] => images
            )
        )
        [posterous] => Array (
            [name] => Posterous 
            [methods] => Array ( 
                      [0] => blog 
                      [1] => microblog 
                      [2] => status 
                      [3] => images
            )
        )
        [tumblr] => Array (
            [name] => Tumblr
            [methods] => Array (
                      [0] => blog 
                      [1] => images
            )
        )
        [typepad] => Array (
            [name] => TypePad
            [methods] => Array (
                      [0] => blog 
                      [1] => microblog 
                      [2] => status
            )
        )
        [vox] => Array (
            [name] => Vox
            [methods] => Array (
                      [0] => blog
            )
        )
        [wordpress] => Array (
            [name] => WordPress.com
            [methods] => Array (
                      [0] => blog 
                      [1] => microblog 
                      [2] => status 
            )
        )
        [xanga] => Array ( 
            [name] => Xanga 
            [methods] => Array (
                      [0] => blog 
                      [1] => microblog 
                      [2] => status )
        )
    )
  • Untested, but I think this will work:
    PHP Code:
    foreach( as ) {
        foreach( as =>) {
            if( == 
    "methods") {
                foreach( as ) {
                    echo ;
                }

            }
        }

    ***EDIT***
    Lol, looks like we posted at the same time...
    ***EDIT Again...***
    looks like it is filtering my code for some weird reason...
    • [2] replies
    • Try these

      $services['blogger']['name']
      $services['blogger']['methods'][0]
      $services['blogger']['methods'][1]

      $services['posterous']['name']
      $services['posterous']['methods'][0]
      $services['posterous']['methods'][1]
      $services['posterous']['methods'][2]
      $services['posterous']['methods'][3]

      etc.

      ...or view the output with something like this:

      foreach($services as $k => $v) {
      echo "<br> ".$k." => 'methods' => ";
      for($t = 0; $t < count($services[$k]['methods']); $t++) {
      echo "&nbsp;&nbsp;'".$services[$k]['methods'][$t]."', ";
      }
      }
    • Yes I had the same problem, you have to uncheck 'automatically parse links' when you post it, and use PHP tags

      • [1] reply
  • Thanks for all the info - with you help, I've figured out my problem!

Next Topics on Trending Feed