How can I replace a certain part of array using PHP?

Just need a little help here about arrays. Because I am trying to create a code that when you click it it will replace a certain array values.

In the base array suppose that we have this array:

Array(
   [0] => Array(
      [id] => 1,
      [name] => xyz
   ),
   [1] => Array(
      [id] => 4,
      [name] => fsa
   ),
)

And in my new array I have something like this

Array(
   [id] => 4,
   [name] => pop
)

I have a validation like this: In the base array I put this array in $base_array and in my new array I have $update_array

$get_updated_array_id = $update_array[id];

for($x = 0; $x <= sizeof($base_array); $x++){

    $target = $base_array[$x]['id'];

    if($get_updated_array_id == $target){

        //should be replace the array value ID '4'

    }

}

So the final result should be:

 Array(
       [0] => Array(
          [id] => 1,
          [name] => xyz
       ),
       [1] => Array(
          [id] => 4,
          [name] => pop
       ),
    )

Any idea how can I do that? Thanks

Answers


<?php
$array = array(
    array('id' => 2,'name' => 'T'),
    array('id' => 4,'name' => 'S')
);

$replace = array('id' => 4,'name' => 'New name');

foreach ($array as $key => &$value) {
    if($value['id'] == $replace['id'] ) {
        $value = $replace;
    }
}

print_r($array);

Need Your Help

how to make interaction between different django apps in a single site?

django django-urls django-apps django-1.3

I have just learnt about Django apps. I want to know that within one site, if I make different apps. like, users, profiles, polls, blogs,comments, jobs , applications then how can I manage them to ...

Using GPU to speed up BigInteger calculations

c# .net performance gpu physics

I am almost done with an algorithm that processes some very large integers (around the order of 2 raised to the power 100,000,000). This takes a couple of hours of highly-parallel code on a 16 core

About UNIX Resources Network

Original, collect and organize Developers related documents, information and materials, contains jQuery, Html, CSS, MySQL, .NET, ASP.NET, SQL, objective-c, iPhone, Ruby on Rails, C, SQL Server, Ruby, Arrays, Regex, ASP.NET MVC, WPF, XML, Ajax, DataBase, and so on.