0

I have an array of associative array, I will like to update the values in this array, hence I created a function that looks like this.

//The Array of Associative arrays
 array (size=2)
  0 => 
    array (size=3)
      'slang' => string 'Tight' (length=5)
      'description' => string 'Means well done' (length=15)
      'example-sentence' => string 'Prosper it Tight on that job' (length=28)
  1 => 
    array (size=3)
      'slang' => string 'Sleet' (length=5)
      'description' => string 'Means to send on long errand' (length=28)
      'example-sentence' => string 'I am going to sleep sia' (length=23)

//The function

public function update($slang, $new)
{
  array_map(function($data, $key) use($slang, $new)
  {
    if($data['slang'] == $slang)
    {
      $data[$key] = array_replace($data, $new);
    }
  }, UrbanWord::$data);
}

I tired running this but the original array will not update. I need help on how to go about fixing this please.

Thanks

4
  • 3
    Afaik, array_map returns an array with the mapped data. You don't seem to catch that return. Commented Dec 2, 2015 at 14:48
  • @Afaik, yes, thanks, fixed that still does not work Commented Dec 2, 2015 at 14:52
  • As much as I love array_map, if you want to replace array elements inline, a plain old foreach (or array_walk) is probably more appropriate here imho. Commented Dec 2, 2015 at 14:52
  • 1
    If you've sorted that, can you update your question's code, then please define doesn't work. Please provide the inputs (UrbanWord::$data, $slang and $new) the current output you're getting, and the expected output you want to get. Commented Dec 2, 2015 at 14:54

1 Answer 1

1

You may use array_reduce instead of array_map as following:

public function update($array, $slang, $new)
{
  return array_reduce($array, function ($result, $item) use ($slang, $new) {
    if ($item['slang'] == $slang) {
       $item = array_replace($item, $new);
    }

    $result[] = $item;

    return $result;
  }, array());
}

Usage:

UrbanWord::$data = $this->update(
  UrbanWord::$data,
  'Tight',
  array('description' => 'another description')
);
var_dump($myUpdatedArray);

If you want to update it directly passing the UrbanWord::$data by reference you may try something like:

class UrbanWord
{
    public static $data = array(
        array(
            'slang' => 'Test',
            'Desc'  => 'Frist Desc'
        ),
        array(
            'slang' => 'Test1',
            'Desc'  => 'Second Desc'
        )
    );
}

class MyClass
{
    public function update(&$array, $slang, $new)
    {
        $array = array_reduce($array, function ($result, $item) use ($slang, $new) {
            if ($item['slang'] == $slang) {
                $item = array_replace($item, $new);
            }

            $result[] = $item;

            return $result;
        }, array());
    }
}

$myClass = new MyClass();
$myClass->update(UrbanWord::$data, 'Test', array('Desc' => 'test'));

echo '<pre>';
var_dump(UrbanWord::$data);
echo '</pre>';
Sign up to request clarification or add additional context in comments.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.