Welcome to WebmasterWorld Guest from 54.198.108.19

Forum Moderators: coopster & jatar k

Message Too Old, No Replies

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)

Junior Member

10+ Year Member

joined:June 24, 2002
posts:110
votes: 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:

Array
(
[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_?
e.g.
widget_foo,111,111
widget_bar,222,222

And how can I output anything that does not begin with widget_?
e.g.
foo_bar,333,333

Please help!

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

Full Member

10+ Year Member

joined:Dec 9, 2002
posts:325
votes: 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)

to

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

HTH,
JP

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

Junior Member

10+ Year Member

joined:June 24, 2002
posts:110
votes: 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.

 

Join The Conversation

Moderators and Top Contributors

Hot Threads This Week

Featured Threads

Free SEO Tools

Hire Expert Members