0

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,
    )
);
2
  • 4
    you need to at least make an attempt Commented Jan 22, 2018 at 5:17
  • refer stackoverflow.com/questions/6661530/… Commented Jan 22, 2018 at 5:21

6 Answers 6

2

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.

Sign up to request clarification or add additional context in comments.

Comments

1

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;

Comments

1

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

Comments

1

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;
 }  
}

Comments

1

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

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

Comments

0
<?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;
}
?>

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.