vote up 0 vote down
star

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.

offensive?
add comment

5 Answers:

vote up 5 vote down

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.

link|offensive?
add comment
vote up 3 vote down

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;
}
link|offensive?
comments (1)
vote up 0 vote down
 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');
link|offensive?
comments (3)
vote up 0 vote down

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.

link|offensive?
add comment
vote up 0 vote down

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

link|offensive?
add comment

Your Answer:

Get an OpenID
or

Not the answer you're looking for? Browse other questions tagged or ask your own question.