tags:

views:

3422

answers:

5

I have a multidimensional array. I need to search it for a specific range of values, edit those values and return the edited data.

Example array:

array(3) {
  ["first"]=>
  array(1) {
    [0]=>
    string(4) "baz1"
  }
  ["second"]=>
  array(1) {
    [0]=>
    string(4) "foo1"
  }
  ["third"]=>
  array(1) {
    [0]=>
    string(4) "foo2"
  }

Now I want to find any values that match foo (foo1 and foo2 in the example array), insert "-bar" into them (foo-bar1, foo-bar2) and return that value. What are the best ways to approach this?

EDIT I should have mentioned that foo could actually be anythingfoo (ex. examplefoo1, somethingelsefoo2, blahblahfoo3). I think this rules out str_replace.

+4  A: 

How about something like this:

function addDashBar($arr)
{
    foreach ($arr as $key => $value)
    {
       if (is_array($value))
           $arr[$key] = addDashBar($value)
       else
       {
           $arr[$key] = str_replace($value, "foo", "foo-bar");
       }
    }

    return $arr;
}
bobwienholt
Upvoted! I was writing the same code... =)
Seiti
+6  A: 

If your array will not be extremely deep, this can work. ($array being what you want to replace later with yours)

$array= array('first' => array('bazi1'), 'second' => array('foo1'), 'third' => array('foo2') );
function modify_foo(&$item, $key)
{
   $item = str_replace('foo', 'foo-bar', $item);
}
array_walk_recursive( $array, 'modify_foo' );

If you want foo to be replaced even in somethingelsefoo2, then str_replace will be just fine.

Marek
+1  A: 
 function test_replace1(&$input, $search, $replace) {
    $result = array();
    $numReplacements = 0;
    foreach ($input as &$value) {
     if (is_array($value)) {
      $result = array_merge($result, test_replace1($value, $search, $replace));
     } else {
      $value = str_replace($search, $replace, $value, $numReplacements);
      if ($numReplacements) {
       $result[] = $value;
      }
     }
    }
    return $result;
 }

 $changed_values = test_replace1($arr, 'foo', 'foo-bar');
Tom Haigh
This may be an ignorant question but what does the ampersand do when you place it in front of a variable? It's kind of hard to use a search engine to find that answer.
Stephen
Look for PHP references (http://www.php.net/manual/en/language.references.php)
Seiti
+1  A: 

If you have a 1 dimensional array, you should be able to use array_map();

** Edit: I had some code here but, after testing , it doesn't work.

In regards to your edit. Just because Foo is at the end of the string, does not mean str_replace will no longer work.

echo str_replace("foo","foo-bar","mycrazystringwithfoorightinthemiddleofit");

will still return

mycrazystringwithfoo-barrightinthemiddleofit

if your array is a tree structure of arbitrary depth, then it is unavoidable that you will have to use recursion and the problem becomes non-trivial. You might want to check out the

array_recursive_walk() function. here Hope this helps.

Vinh
+1  A: 

May this link is Helpful for youprogram of single array acting as multiple stacks