Forum Moderators: coopster
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!
<?
// 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)
HTH,
JP