Skip to content

Instantly share code, notes, and snippets.

Show Gist options
  • Save kingjmaningo/a90097ccf0df22172e9900bd227c3895 to your computer and use it in GitHub Desktop.
Save kingjmaningo/a90097ccf0df22172e9900bd227c3895 to your computer and use it in GitHub Desktop.
Update an element in an array if certain value matched
<?php
// Uinique id to point on a certain data
$id = 3;
// New value
$name = 'Michael';
// We are trying to change the name that matches the id
$array_values = array (
array(
'id' => 1,
'name' => 'Kobe',
'email' => 'user_email_1',
'birth_date' => '10-22-2019' ),
array(
'id' => 2,
'name' => 'Lebron',
'email' => 'user_email_2',
'birth_date' => '11-01-2019' ),
array(
'id' => 3,
'name' => 'Mike',
'email' => 'user_email_3',
'birth_date' => '10-18-2019' ),
array(
'id' => 4,
'name' => 'Magic',
'email' => 'user_email_4',
'birth_date' => '10-11-2019' )
);
foreach($array_values as $key => $value) :
if($id == $value['id']) :
$array_values[$key]['name'] = $name;
endif;
endforeach;
// We are expecting Mike to change to Michael To check:
echo '<pre>';
print_r($array_values);
echo '<pre>';
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment