Modify multidimensional array value from searching by key = value

121
January 22, 2018, at 2:31 PM

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,
    )
);
Answer 1

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.

Answer 2

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;
Answer 3

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);
Answer 4

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;
 }  
}
Answer 5
function AddNum($arr,$id,$number)
{
    foreach($arr as $key=>$value)
    {
        if($value['id']==$id)
        {
            $arr[$key]['num']+=$number;
            return $arr;
        }
    }
}

That should work :) example:

$x=AddNum($records,2,2);
print_r($x);
Answer 6
<?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;
}
?>
READ ALSO
How i can do download file from database using CodeIgniter based on the file id?

How i can do download file from database using CodeIgniter based on the file id?

that my code but didn't work anyway so how can I make download function using Codeigniter based on the file id?

156
WebPage display value different from the one in the source code

WebPage display value different from the one in the source code

I am new to the web and "php" and am currently working with the "simple_html_domphp"

120
HTML form not submitting to php script

HTML form not submitting to php script

UPDATE: The code works in Microsoft Edge, but not FirefoxTesting other browsers

162