Reputation: 473
I have an array here:
$records = array(
array(
'id' => 2,
'first_name' => 'John',
'last_name' => 'Doe',
'num' => 123,
),
array(
'id' => 4,
'first_name' => 'Sally',
'last_name' => 'Smith',
'num' => 146,
),
array(
'id' => 8,
'first_name' => 'Jane',
'last_name' => 'Jones',
'num' => 253,
),
array(
'id' => 9,
'first_name' => 'Peter',
'last_name' => 'Doe',
'num' => 632,
)
);
How can I search for ID 8 in array $results
and modify the num
? For example, find array that has ID = 8 and add 5 to the num
? The the modified $result
array will be:
$records = array(
array(
'id' => 2,
'first_name' => 'John',
'last_name' => 'Doe',
'num' => 123,
),
array(
'id' => 4,
'first_name' => 'Sally',
'last_name' => 'Smith',
'num' => 146,
),
array(
'id' => 8,
'first_name' => 'Jane',
'last_name' => 'Jones',
'num' => 258,
),
array(
'id' => 9,
'first_name' => 'Peter',
'last_name' => 'Doe',
'num' => 632,
)
);
Upvotes: 0
Views: 399
Reputation: 253
foreach($records as $key => $data) {
if(in_array(8,$data)) {
$records[$key]['num'] = 253+50;
}
}
echo "<pre>";
print_r($records);
Upvotes: 1
Reputation: 18577
This one liner trick will solve your problem,
$records[array_search(8, array_column($records, 'id'))]['num'] += 5;
print_r($records);
array_search — Searches the array for a given value and returns the first corresponding key if successful
array_column — Return the values from a single column in the input array
Here is working demo.
Upvotes: 2
Reputation: 1056
<?php
$records = array(
array(
'id' => 2,
'first_name' => 'John',
'last_name' => 'Doe',
'num' => 123,
),
array(
'id' => 4,
'first_name' => 'Sally',
'last_name' => 'Smith',
'num' => 146,
),
array(
'id' => 8,
'first_name' => 'Jane',
'last_name' => 'Jones',
'num' => 253,
),
array(
'id' => 9,
'first_name' => 'Peter',
'last_name' => 'Doe',
'num' => 632,
));
$id2 = searchForId(8, $records);
function searchForId($id, $records) {
foreach ($records as $key => $val) {
if ($val['id'] === $id) {
$abc = $records[$key]['num'] = $val['num']+5;
echo "<pre>";
print_r($records);
}
}
return null;
}
?>
Upvotes: 0
Reputation: 6311
try this
use &$r
array is passed by reference so you can update the value in for loop without having the key
foreach($records as &$r){
if($r['id'] == 8){
$r['num'] = 258;
}
}
Upvotes: 1
Reputation: 934
Use this one...
foreach ($records as $key => $value) {
$search_value = "";
$search_value = array_search("8",$value);
if($search_value!="")
$value["num"] = $value["num"]+5;
$result[] = $value;
}
print_r($result);
Upvotes: 1
Reputation: 1135
This is one way to achieve output
$records = array(
array(
'id' => 2,
'first_name' => 'John',
'last_name' => 'Doe',
'num' => 123,
),
array(
'id' => 4,
'first_name' => 'Sally',
'last_name' => 'Smith',
'num' => 146,
),
array(
'id' => 8,
'first_name' => 'Jane',
'last_name' => 'Jones',
'num' => 253,
),
array(
'id' => 9,
'first_name' => 'Peter',
'last_name' => 'Doe',
'num' => 632,
)
);
foreach($records as $key => $value)
{
if($value['id']=='8'){
$records[$key]['num'] = $value['num']+5;
}
}
echo "<pre>";
print_r($records);
exit;
Upvotes: 1