homepage Welcome to WebmasterWorld Guest from
register, free tools, login, search, pro membership, help, library, announcements, recent posts, open posts,
Become a Pro Member

Home / Forums Index / Code, Content, and Presentation / PHP Server Side Scripting
Forum Library, Charter, Moderators: coopster & jatar k

PHP Server Side Scripting Forum

Yet Another Foo Bar Multidimensional Array Problem
If you happen to be a Multidimensional Array expert, please help!

 9:25 am on Aug 25, 2004 (gmt 0)

Hi guys,

I have digged up arrays information online but I still can't accomplish the task below.

I have a script that out puts an array like this:

[widget_foo] => Array
[0] => 111
[1] => 111

[widget_bar] => Array
[0] => 222
[1] => 222
[foo_bar] => Array
[0] => 333
[1] => 333

How can I use output anything that begins with widget_?

And how can I output anything that does not begin with widget_?

Please help!



 9:51 am on Aug 25, 2004 (gmt 0)

Here's some example code to get you going (note, I create the array here too). It does the job - no other warranties given :)

// Create array
$widget_foo = Array(0 => "111", 1 => "111");
$widget_bar = Array(0 => "222", 1 => "222");
$foo_bar = Array(0 => "333", 1=> "333");

$testarray = Array();
$testarray["widget_foo"] = $widget_foo;
$testarray["widget_bar"]= $widget_bar;
$testarray["foo_bar"] = $foo_bar;

while (list($key, $values) = each($testarray) )
$searchstring = "widget_";
if (substr($key,0, strlen($searchstring)) == $searchstring)
print "\n" . $key;
for ($x = 0; $x < count($values); $x++)
print ",".$values[$x];



print "\n";

Basically, the while (list($key, $values) etc. line tells PHP to walk through each value of the array.

We then check to see if the key value starts with the string "widget_", and if so, outputs all the values in that subarray.

If you want to output anything which does not start with "widget_", then change:

if (substr($key,0, strlen($searchstring)) == $searchstring)


if (substr($key,0, strlen($searchstring))!= $searchstring)



 10:11 am on Aug 25, 2004 (gmt 0)

Wow jpjones, it works!
That solved my YAFBMDAP problem.

I am not satisfied though.
I will try to analyze the code in the weekend to figure out the complicated part.
I am not good when it comes to arrays.
And documentation on the net sucks big time when it comes to explanation.

Global Options:
 top home search open messages active posts  

Home / Forums Index / Code, Content, and Presentation / PHP Server Side Scripting
rss feed

All trademarks and copyrights held by respective owners. Member comments are owned by the poster.
Home ¦ Free Tools ¦ Terms of Service ¦ Privacy Policy ¦ Report Problem ¦ About ¦ Library ¦ Newsletter
WebmasterWorld is a Developer Shed Community owned by Jim Boykin.
© Webmaster World 1996-2014 all rights reserved