Page 1 of 1

PHP Multidimensional array issue

Posted: Fri Feb 26, 2010 7:22 pm
by cringwall
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'

Re: PHP Multidimensional array issue

Posted: Fri Feb 26, 2010 7:49 pm
by AbraCadaver
Do the the following, copy and paste here in code tags and I'll look at it:

Code: Select all

echo "<pre>" . print_r($your_array, true) . "</pre>";

Re: PHP Multidimensional array issue

Posted: Fri Feb 26, 2010 7:58 pm
by cringwall
thanks - but I really don't have any code - just this array - I don't know how to attack getting to the data.

Re: PHP Multidimensional array issue

Posted: Fri Feb 26, 2010 8:32 pm
by AbraCadaver
cringwall wrote:thanks - but I really don't have any code - just this array - I don't know how to attack getting to the data.
I was talking about your array. It's very difficult to follow in the format above.

Re: PHP Multidimensional array issue

Posted: Fri Feb 26, 2010 9:43 pm
by cringwall

Code: Select all

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 ) ) 
 
)
That does look better....

Re: PHP Multidimensional array issue

Posted: Sat Feb 27, 2010 12:05 pm
by AbraCadaver
I'm not sure how you want to access these, but here is one way:

Code: Select all

foreach($your_array as $blog) {
    foreach($blog['methods'] as $method) {
        echo $method;
    }
}

Re: PHP Multidimensional array issue

Posted: Sat Feb 27, 2010 2:39 pm
by cringwall
Perfect - I took your routine and made this function that returns an array of the methods for the blog:

Code: Select all

function get_methods($name,$services){
    foreach($services as $blog) {
        if($blog['name']==$name){;
            $mymethods = array();
            foreach($blog['methods'] as $method) {
                $mymethods[] = $method;
 
            }
            return $mymethods;
        }
    }
 
}
Thank you - I have trouble getting my head around arrays.

Carl

Re: PHP Multidimensional array issue

Posted: Sat Feb 27, 2010 3:40 pm
by AbraCadaver
You are returning the 'methods' element anyway, so why loop through it and build another one? This is the same without the inner loop:

Code: Select all

function get_methods($name ,$services) {
    foreach($services as $blog) {
        if($blog['name'] == $name){;
           return $blog['methods'];
        }
    } 
}
Also, if we can assume that the first index ['Blogger'], etc. will always be the same as the corresponding ['name'], then this is all you need:

Code: Select all

$name = 'Blogger';
$methods = $services[strtolower($name)]['methods'];