baileyJchoi
baileyJchoi

Reputation: 473

Modify multidimensional array value from searching by key = value

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

Answers (6)

Gur Janjua
Gur Janjua

Reputation: 253

foreach($records as $key => $data) {
	if(in_array(8,$data)) {
		$records[$key]['num'] = 253+50;
	}
}

echo "<pre>";
print_r($records);

Upvotes: 1

Rahul
Rahul

Reputation: 18577

This one liner trick will solve your problem,

$records[array_search(8, array_column($records, 'id'))]['num'] += 5;
print_r($records);

array_searchSearches the array for a given value and returns the first corresponding key if successful
array_columnReturn the values from a single column in the input array

Here is working demo.

Upvotes: 2

Nims Patel
Nims Patel

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

Arun Kumaresh
Arun Kumaresh

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

Sarvan Kumar
Sarvan Kumar

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

Talk2Nit
Talk2Nit

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

Related Questions