0

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.

1
  • 1
    Is restaurant_id a unique ID for the entries? Commented Nov 15, 2013 at 9:51

5 Answers 5

2

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'] = ...;
  ...;
 }
}
Sign up to request clarification or add additional context in comments.

Comments

1

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.

5 Comments

Ok it is working now. But I am performing this in a loop. Does it affect if I perform this in a loop?
Of course, you can check it in a loop, if its existing, overwrite; if not, just do array_push to the end of the array.
Sir can you help me with my code? I have a code that creates a textfiles. I can create a file. But I can't get the correct update.
I can have a look on it.
1

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.

Comments

0

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

Comments

0

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

4 Comments

It is working sir. But what if it is in a loop can I do the same? If i loop all the arrays values?
Yes, you can do the same, why not ? :)
@Theox: why are you unseting it? Its not necessary... Simply overwrite it; see my answer below.
@Legionar : I'm simply answering Rochelle's question about deleting an index in an array. Indeed, in this case, as we want to update the array with new informations, overwriting the existing index is a good solution.

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.