2

I have an array like this.

$array = [
       0 =>[
           "name" => "John",
           "age" => "20",
           "class" => "first"
       ],
       1 => [
           "name" => "Doe",
            "age" => "22",
            "class" => "second"
       ],
       2 => [
            "name" => "Template",
            "age" => "30",
            "class" => "third"
        ],
    ];

foreach ($array as $arr) {
    if ($arr["name"] == "Template") {
        $arr["result"] = ["maths" => "60", "English" => "20"];
        array_push($arr,$arr["result"]);
    }
}

I have tried several things like using array_push($arr,$arr["result"]) but that still returns the array. How can i loop through it to return something like this.

    2 => [
        "name" => "Template",
        "age" => "30",
        "class" => "third",
        "result"=> ["maths" => "60", "English" => "20"],
    ],

3 Answers 3

5

You're very close, just change this one line:

foreach ($array as $arr) {

To this:

foreach ($array as &$arr) {

And then remove the array_push(), as it's not needed. In the former, $arr is a copy of the subarray that gets discarded at the end of the loop iteration. In the latter, $arr is a pointer to the original subarray, so changes are maintained. See PHP: References Explained for details.

Sign up to request clarification or add additional context in comments.

1 Comment

other way is use foreach ($array as $key => $arr) and then change value using $array[$key]
3

you need to make the loop by reference with &

foreach ($array as &$arr) {
    if ($arr["name"] == "Template") {
        $arr["result"] = ["maths" => "60", "English" => "20"];
    }
}

This results in var_dump($array);:

array(3) {
  [0]=>
  array(3) {
    ["name"]=>
    string(4) "John"
    ["age"]=>
    string(2) "20"
    ["class"]=>
    string(5) "first"
  }
  [1]=>
  array(3) {
    ["name"]=>
    string(3) "Doe"
    ["age"]=>
    string(2) "22"
    ["class"]=>
    string(6) "second"
  }
  [2]=>
  &array(4) {
    ["name"]=>
    string(8) "Template"
    ["age"]=>
    string(2) "30"
    ["class"]=>
    string(5) "third"
    ["result"]=>
    array(2) {
      ["maths"]=>
      string(2) "60"
      ["English"]=>
      string(2) "20"
    }
  }
}

Another method is to make the array associative on name and directly access the correct subarray.

$array = array_column($array, null, "name");
$array["Template"]["result"] = ["maths" => "60", "English" => "20"];


var_dump($array);

https://3v4l.org/WFqfG

Just as with the loop method it requires unique names.

1 Comment

Thanks a lot. This was just what i needed.
2

Since there is a laravel tag, here is one way you could achieve it using laravel collections.

$array = collect($array)->map(function($item){
   if($item['name'] == "Template")
      $item['result'] = ["maths" => "60", "English" => "20"];
   return $item;
})->toArray();

collect method converts the array into a collection. map function will go over all elements of the array and using the callback function you can bring changes to the original collection. The changes are maintained. The toArray at the end is added assuming that the final data is required as an array. Without the, toArray it'd be a collection.

Edit: There was a laravel tag.

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.