PHP Foreach by Reference Causes Weird Glitch When Going Through Array of Objects

attempting to assign a property to an array

The foreach control structure does not pass the value by reference by default.

If you want to be able modify the array internally, you need to use & to indicate you want it passed as a reference like so:

foreach ($myArray as &$array) {
$array['link_root'] = "a string";
}

print($myArray[0]['link_root']);

The difference is subtle. Another way to do this is pass the index and reference it directly like:

foreach ($myArray as $index => $array) {
$myArray[$index]['link_root'] = "a string";
}

print($myArray[0]['link_root']);

Reference: http://php.net/manual/en/control-structures.foreach.php

php sorting drop list by last name

The problem is that you echoes names in the same foreach() loop that invert name and surname.

With a very little modification, you can obtain desired result.

First of all, use reference in foreach() loop (& before $name). By this way, you will change directly the array values, not a copy of them. The $id is not necessary in this foreach. Remove the echo from this loop:

foreach( $names as &$name )
{
$parts = explode(" ", $name);
$lastname = array_pop($parts);
$firstname = implode(" ", $parts);
$name = $lastname.", ".$firstname." ";
}

At the end of foreach() loop, we have to unset() $name to obtain a correct result (thank to mr. Don't Panic for advice):

unset( $name );

Then, use natcasesort() to sort your array. With natcasesort, you sort case insensitive and maintain original keys, so the id in your <option> has the same of original array:

natcasesort( $names );

At the end, perform an additional foreach() loop to echo names:

foreach( $names as $id => $name )
{
echo "<option value='$id'>$name</option>\n";
}

Edit:

If you prefer (I prefer), you can replace completely first foreach() loop with array_walk() and preg_replace():

array_walk
(
$names,
function( &$val )
{
$val = '['.preg_replace('/^(.+) +(\S+)$/','\2, \1',$val).']';
}
);

I have exploded syntax for clarity, but it is one line of code.

update a global array within a function

Pass the variable $stuff by reference. Note the & in the function parameters.

function insert($anchor, &$stuff){    // note the & mark
foreach($stuff as $part){
$new_array = array($anchor => rand());
if($part == $anchor){
array_push($stuff, $new_array);
}
}
}

How can I recursively search for and replace values inside of an unknown-depth multidimensional PHP array?

With array_walk_recursive:

function replace_data($json_array, $data = 'REPLACE TEST') {
array_walk_recursive($json_array, function (&$value, $key) use ($data) {
if (!is_array($value) && $key === 'content') {
// $value passed by reference
$value = $data;
}
});
return $json_array;
}

And without references:

function replace_data($json_array, $data = 'REPLACE TEST') {
foreach ($json_array as $key => $value) {
if (is_array($value)) {
$json_array[$key] = replace_data($value, $data);
} elseif ($key === 'content') {
$json_array[$key] = $data;
}
}
return $json_array;
}

How to update specific key's value in an associative array in PHP?

Change your foreach to something like this, You are not assigning data back to your return variable $data after performing operation on that.

foreach($data as $key => $value)
{
$data[$key]['transaction_date'] = date('d/m/Y',$value['transaction_date']);
}

Codepad DEMO.



Related Topics



Leave a reply



Submit