如果使用PHP不存在索引值,如何在数组中插入值?

Can you help me with my problem? I have a problem in array. To make my problem short here it is. I created a code that can automatically replace and add a value in an array using an onclick. But javascript here is not an issue. The scenario is, after the user clicked the button or submit. The code will validate if it is existing in the array. By the way I have a serialized array. If the array is not existed in the list it will automatically added to the end of the array. If existed it will just replace the certain parts of array.

Here's my code:

$data = unserialize(file_get_contents('addresses.txt'));

                //fn_print_r($data);

                $data_update = array(
                    'restaurant_id' => $restaurant_id, 
                    'new_lat' => $new_lat_entry, 
                    'new_long' => $new_long_entry, 
                    'date_updated' => date('Y-m-d H:i:s')
                );

                $iterate = -1;

                foreach($data as $key => &$value){

                    $iterate++;

                    //echo $data[$iterate]['restaurant_id'];

                    if($data[$iterate]['restaurant_id'] == $data_update['restaurant_id']){

                        $data[] = $data_update;

                    }else{

                        array_push($data,$data_update);
                        break;

                    }

                }

This is the the sample serialized array:

a:1:{i:0;{s:13:"id";s:4:"3";s:7:"name";s:8:"lmlm";}}

This is the example:

'BASE ARRAY'

Array(

    [0] => Array(
        [id] => 1,
        [name] => popo

    ),

    [1] => Array(
        [id] => 3,
        [name] => wewe

    ),

    [2] => Array(
        [id] => 2,
        [name] => lolo

    ),

)

'NEW ARRAY'

Array(

    [id] => 2,
    [name] => fifi

)

If the id is found in the 'BASE' array. The output is:

Array(

    [0] => Array(
        [id] => 1,
        [name] => popo

    ),

    [1] => Array(
        [id] => 3,
        [name] => wewe

    ),

    [2] => Array(
        [id] => 2,
        [name] => fifi

    ),

)

If not found. If we have a new array like this:

Array(

    [id] => 6,
    [name] => fifi

)

The result should be:

Array(

    [0] => Array(
        [id] => 1,
        [name] => popo

    ),

    [1] => Array(
        [id] => 3,
        [name] => wewe

    ),

    [2] => Array(
        [id] => 2,
        [name] => lolo

    ),

    [3] => Array(
        [id] => 6,
        [name] => fifi

    ),

)

First some clean-up,

  • You're initializing $temp_array twice
  • You're not using register_globals, are you?
  • You're not using $target
  • You don't need $iterate as you already have $key
  • You should unset() $value after foreach, as otherwise it will keep its reference

Now,

Your examples are confusing. Why has fifi, with an id of 2, replaced lolo, who has an id of 5?

It looks like you have the append code correct. One would use $data[] = ... to append, though presumably in the 'else' case, not the 'if' case. In order to replace, you should use $data[$key] = ....

Edit:

Don't try to do everything inside a for loop -- it'll get confusing, as it did with me earlier. Simply do:

if (restaurant exists in array)
    update array entry
else
    append to array

As you're matching by id only, you cannot simply use array_search() et al. Write your own version which compares id's. It can still return an index/FALSE so you know which element to replace in the case of a positive result.

An idea to work on:

$key = get_restaurant_index_by_id($data, $data_update['restaurant_id']);

if ($key === FALSE) {
    $data[] = $data_update;
} else {
    $data[$key] = $data_update;
}