Reputation: 11464
This may be very simple question, sorry about my lack of knwoledge, and my stupidity...
After adding Items to array,
$List[1] = array(
'Id' => 1,
'Text'=> 'First Value'
);
I need to change the value of an item in inner array,
foreach ($List as $item)
{
$item['Text'] = 'Second Value';
}
But when i check Value remains same
foreach ($List as $item)
{
echo $item['Text']; //prints 'First Value'
}
How to update the value to 'Second Value'?
Upvotes: 0
Views: 120
Reputation: 23316
You can either set it directly:
foreach ($List as $key => $item)
{
$List[$key]['Text'] = 'Second Value';
}
Or set it by reference:
foreach ($List as &$item)
{
$item['Text'] = 'Second Value';
}
Upvotes: 3
Reputation: 5579
There may be a PHP-ish mystical Perl way to access the value, but I find it simpler to loop through the array and set the value directly.
for($i = 0; $i < count($List); $i++)
{
$List[$i] = 'Second Value';
}
Edit: Curiosity got the better of me. http://www.php.net/manual/en/control-structures.foreach.php
foreach($List as &$item)
{
$item = 'Second Value';
}
Notice the &
which causes $item
to be used by reference instead of by value.
Upvotes: 1