Jerielle
Jerielle

Reputation: 7520

How to replace array value using a condition?

Sorry for my title. I just don't know how to ask this. To make the story short I just need to replace a certain part of array. Let's say we have this array:

Array
(
      [0] => Array
        (
            [restaurant_id] => 1236
            [new_lat] => 76
            [new_long] => 86
            [date_updated] => 2013-11-15 17:20:58
        )

      [1] => Array
        (
            [restaurant_id] => 1247
            [new_lat] => 6
            [new_long] => 5
            [date_updated] => 2013-11-15 17:20:58
        )

      [2] => Array
        (
            [restaurant_id] => 7456
            [new_lat] => 21
            [new_long] => 12
            [date_updated] => 2013-11-15 17:20:58
        )

)

Now I need to replace the index 2 with this:

Array(
     [2] => Array
        (
            [restaurant_id] => 1236
            [new_lat] => 2
            [new_long] => 1
            [date_updated] => 2013-11-15 17:20:58
        )
)

I need to replace if I found out that there is an existing restaurant_id. If there is. Update that line. I not add the new array.

I have an idea but I don't know how to do this. My idea is:

  1. Unserialize the array (because my array is in serialized form)

  2. Find the target index. If found remove that index and add my new index to the bottom of array then serialize it again. If not found add only in the bottom of array and serialize.

I just dont know if I remove an index. If the index will be move automatically. Means If I delete index 2 the index 3 will became index 2 and my new array is index 3.

Ok that's all guys. thanks.

Upvotes: 0

Views: 1253

Answers (5)

Axel Amthor
Axel Amthor

Reputation: 11096

if restaurant_id is a unique ID, it's quite easier to handle that array if you reorganize it a bit:

Array
(
      [1236] => Array
        (
            [new_lat] => 76
            [new_long] => 86
            [date_updated] => 2013-11-15 17:20:58
        )

      [1247] => Array
        (
            [new_lat] => 6
            [new_long] => 5
            [date_updated] => 2013-11-15 17:20:58
        )

      [7456] => Array
        (
            [new_lat] => 21
            [new_long] => 12
            [date_updated] => 2013-11-15 17:20:58
        )

)

after that you may find it easier to just access by

 ... isset($arr[$restaurantId]) ....

and

$arr[$restaurantId] = array('new_lat' => 42, .... )

will insert/update w/o any knowledge about whether the entry exists or not.

Upvotes: 1

Stefanov.sm
Stefanov.sm

Reputation: 13049

Obtain a reference variable containing the target element and modify it.

foreach ($myArray as $myKey => &$myElement) 
{
 if ($myElement['restaurant_id'] == WHAT_IM_LOOKING_FOR)
 {
  $myElement['new_lat'] = ...;
  ...;
 }
}

Upvotes: 2

Legionar
Legionar

Reputation: 7597

You dont have to remove that index; simply just overwrite it.

  1. Unserialize

  2. Find the target index. If found, just overwrite (not remove). If not found, simply add to the end with [].

Overwriting:

$my_array[2] = array(
    'restaurant_id' => 1236,
    'new_lat' => 2,
    'new_long' => 1,
    'date_updated' => '2013-11-15 17:20:58'
);

This code will overwrite your index 2 with this new. You dont need to unset it before.

Upvotes: 1

Theox
Theox

Reputation: 1363

If you use the unset function, the index will stay empty.

For example, if your $array has 4 values, 0 1 2 3, if you unset index 2, the index 3 will stay 3, and new addition to the array will be index 4.

The following code will illustrate this :

$a = array("zero", "one", "two", "three");
var_dump($a);
unset($a[2]);
var_dump($a);
$a[]="four";
var_dump($a);

Upvotes: 0

Hans Wassink
Hans Wassink

Reputation: 2577

Ok, unset the element with the id, then resort, then add a new element:

    unset($array[2]); // unset number 2
    $array = array_values($array); // resort
    $array[] = $newStuff; // add

Good luck

Upvotes: 0

Related Questions