Welcome to WebmasterWorld Guest from 54.167.22.37

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!

     

haryanto

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

10+ Year Member



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!

jpjones

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

10+ Year Member



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

haryanto

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

10+ Year Member



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.

 

Featured Threads

Hot Threads This Week

Hot Threads This Month