PHP Multidimensional array issue
Moderator: General Moderators
PHP Multidimensional array issue
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'
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'
- AbraCadaver
- DevNet Master
- Posts: 2572
- Joined: Mon Feb 24, 2003 10:12 am
- Location: The Republic of Texas
- Contact:
Re: PHP Multidimensional array issue
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>";mysql_function(): WARNING: This extension is deprecated as of PHP 5.5.0, and will be removed in the future. Instead, the MySQLi or PDO_MySQLextension should be used. See also MySQL: choosing an API guide and related FAQ for more information.
Re: PHP Multidimensional array issue
thanks - but I really don't have any code - just this array - I don't know how to attack getting to the data.
- AbraCadaver
- DevNet Master
- Posts: 2572
- Joined: Mon Feb 24, 2003 10:12 am
- Location: The Republic of Texas
- Contact:
Re: PHP Multidimensional array issue
I was talking about your array. It's very difficult to follow in the format above.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.
mysql_function(): WARNING: This extension is deprecated as of PHP 5.5.0, and will be removed in the future. Instead, the MySQLi or PDO_MySQLextension should be used. See also MySQL: choosing an API guide and related FAQ for more information.
Re: PHP Multidimensional array issue
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 ) )
)- AbraCadaver
- DevNet Master
- Posts: 2572
- Joined: Mon Feb 24, 2003 10:12 am
- Location: The Republic of Texas
- Contact:
Re: PHP Multidimensional array issue
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;
}
}mysql_function(): WARNING: This extension is deprecated as of PHP 5.5.0, and will be removed in the future. Instead, the MySQLi or PDO_MySQLextension should be used. See also MySQL: choosing an API guide and related FAQ for more information.
Re: PHP Multidimensional array issue
Perfect - I took your routine and made this function that returns an array of the methods for the blog:
Thank you - I have trouble getting my head around arrays.
Carl
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;
}
}
}Carl
- AbraCadaver
- DevNet Master
- Posts: 2572
- Joined: Mon Feb 24, 2003 10:12 am
- Location: The Republic of Texas
- Contact:
Re: PHP Multidimensional array issue
You are returning the 'methods' element anyway, so why loop through it and build another one? This is the same without the inner loop:
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
function get_methods($name ,$services) {
foreach($services as $blog) {
if($blog['name'] == $name){;
return $blog['methods'];
}
}
}Code: Select all
$name = 'Blogger';
$methods = $services[strtolower($name)]['methods'];mysql_function(): WARNING: This extension is deprecated as of PHP 5.5.0, and will be removed in the future. Instead, the MySQLi or PDO_MySQLextension should be used. See also MySQL: choosing an API guide and related FAQ for more information.