4

I'm trying to update an array but the array contains arrays.

For a example,

$data = array(
    array('name'=>'John','age'=>'19','alive'=>'false'),
    array('name'=>'Bob','age'=>'32','alive'=>'false'),  
    array('name'=>'Kate','age'=>'22','alive'=>'false'), 

);

I need to add another element to all these arrays.

I tried using foreach

foreach($data as $onearray){
     $onearray['alive'] = 'true';
}

Do I need to create a new array and add all updated arrays back to new one?

3 Answers 3

7

Did you try using the data array key? Like so:

foreach($data as $key => $onearray){
     $data[$key]['alive'] = 'true';
}

( Notice the $onearray never gets used )

Even faster would be:

for($i = 0; $i < count($data); $i++) {
    $data[$i]['alive'] = 'true';
}
Sign up to request clarification or add additional context in comments.

5 Comments

Even faster would be for($i = 0; $c = count($data); $i < $c; $i++) {, but the time difference will be very small anyway, so it doesn't matter.
You're absolutely right. The gain is not much. However, it's good to try and accustom to using a for loop when foreach is not necessary ( because foreach runs on a 'copy' of the array, thus using up more memory )
Sry, but this solution is VERY POOR. uses hashmap-lookups all the time. use array_walk or references instead to get the code runing in a good time. you should NOT USE THIS EXAMPLE
I did a small benchmark with four different methods posted here (1000000 iterations) and it turns out passing by references is the fastest and the solution provided in the second part of this answer is the slowest. See the results here.
Then I stand corrected, and have learned! Thanks. I would like to add that for a novice programmer using references might be harder to understand ( although this example is easy enough ), especially if one forgets to unset the last reference and uses the variable later on. Don't you think? Also very poor might not be the best way to describe my answer, a bit harsh, no?
5

use the referencing:

foreach($data as &$onearray) {
     $onearray['alive'] = 'true';
}
unset($onearray)

and clean up the reference afterwards, so further code will not mess it up


In order to be able to directly modify array elements within the loop precede $value with &. In that case the value will be assigned by reference.

see foreach-reference for more: https://www.php.net/manual/en/control-structures.foreach.php

Comments

1

Use the built in function array_walk — Apply a user function to every member of an array

<?php
$fruits = array("d" => "lemon", "a" => "orange", "b" => "banana", "c" => "apple");

function test_alter(&$item1, $key, $prefix)
{
    $item1 = "$prefix: $item1";
}

function test_print($item2, $key)
{
    echo "$key. $item2<br />\n";
}

echo "Before ...:\n";
array_walk($fruits, 'test_print');

array_walk($fruits, 'test_alter', 'fruit');
echo "... and after:\n";

array_walk($fruits, 'test_print');
?>

Output

Before ...:
d. lemon
a. orange
b. banana
c. apple
... and after:
d. fruit: lemon
a. fruit: orange
b. fruit: banana
c. fruit: apple

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.