donglilian0061 2013-11-19 00:48
浏览 108
已采纳

如果使用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

    ),

)
  • 写回答

1条回答 默认 最新

  • dsyrdwdbo47282676 2013-11-19 01:10
    关注

    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;
    }
    
    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论

报告相同问题?

悬赏问题

  • ¥15 素材场景中光线烘焙后灯光失效
  • ¥15 请教一下各位,为什么我这个没有实现模拟点击
  • ¥15 执行 virtuoso 命令后,界面没有,cadence 启动不起来
  • ¥50 comfyui下连接animatediff节点生成视频质量非常差的原因
  • ¥20 有关区间dp的问题求解
  • ¥15 多电路系统共用电源的串扰问题
  • ¥15 slam rangenet++配置
  • ¥15 有没有研究水声通信方面的帮我改俩matlab代码
  • ¥15 ubuntu子系统密码忘记
  • ¥15 保护模式-系统加载-段寄存器