PHP Multidimensional array problem

6 replies
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'
#array #multidimensional #php #problem
  • Profile picture of the author HonestCoder
    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 )
        )
    )
    {{ DiscussionBoard.errors[1804506].message }}
  • Profile picture of the author wayfarer
    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...
    Signature
    I build web things, server things. I help build the startup Veenome. | Remote Programming Jobs
    {{ DiscussionBoard.errors[1804529].message }}
    • Profile picture of the author Havenhood
      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]."', ";
      }
      }
      Signature

      --= -Spazzle- =--

      {{ DiscussionBoard.errors[1804558].message }}
    • Profile picture of the author HonestCoder
      Yes I had the same problem, you have to uncheck 'automatically parse links' when you post it, and use PHP tags

      Originally Posted by wayfarer View Post

      looks like it is filtering my code for some weird reason...
      {{ DiscussionBoard.errors[1804936].message }}
      • Profile picture of the author wayfarer
        Originally Posted by HonestCoder View Post

        Yes I had the same problem, you have to uncheck 'automatically parse links' when you post it, and use PHP tags
        Thanks. I think I had this problem once before, and never noticed a solution though it hasn't happened for a long time. I don't usually post a lot of code in this forum, unlike some other forums on the web I frequent, since people tend to ask more general questions here.
        Signature
        I build web things, server things. I help build the startup Veenome. | Remote Programming Jobs
        {{ DiscussionBoard.errors[1805869].message }}
  • Profile picture of the author cringwall
    Thanks for all the info - with you help, I've figured out my problem!
    Signature

    Currently in research mode, any and all thoughtful replies are appreciated!

    {{ DiscussionBoard.errors[1807840].message }}

Trending Topics