如何使用PHP编写带有条件的文本文件

时间:2013-11-15 04:38:19

标签: php arrays

我的代码中存在问题。我的代码是关于更新/删除文本文件中的行ig找到文本文件。我遇到了麻烦。起初我可以添加一个新的文本文件,我可以同时更新这个文件。但这是一排。如果我更新另一行。它只会在文本文件的末尾附加更新的值。我想要的是更新或删除并插入新的。但我不知道怎么做。我更新数组的过程是array_replace()。首先,我需要找出我的数据的ID是否在文本文件中找到。如果找到,我将简单更新/删除并将现有数据替换为我的新更新数据。如果没有找到只添加。

这是我的代码。

        $restaurant_id = $post_data['company_id']; 
        $new_lat_entry = $post_data['new_lat']; 
        $new_long_entry = $post_data['new_long']; 

        /****Here's my new updated array ****/
        $data_add = array( 
            'restaurant_id' => $restaurant_id, 
            'new_lat' => $new_lat_entry, 
            'new_long' => $new_long_entry, 
            'date_updated' => date('Y-m-d H:i:s') 
        ); 
        /****This is the BASE array from the existing textfile ****/
        $data = unserialize(file_get_contents('addresses.txt'));

        $target = $data_add['restaurant_id'];

        for ($i = 0; $i < count($data); $i++) {

            $get_id = $data[$i]['restaurant_id'];

            if($get_id == $target){

                //If ID is found - UPDATE

                $add_data = array();

                $add_data = array(
                    $i => $data_add
                );

                $new_array = array();
                $new_array = array_replace($data,$add_data);

                $serialize_data = serialize($new_array); 
                                $file_input_txt = fopen("addresses.txt","w+"); 
            fwrite($file_input_txt,$serialize_data); 
            fclose($file_input_txt); 

            }else{

                $new_array = array(
                    $i => $data_add
                );

                $serialize_data = serialize($new_array); 

                $file_input_txt = fopen("addresses.txt","w+"); 
                                fwrite($file_input_txt,$serialize_data); 
                fclose($file_input_txt); 


            }

    }

我的文本文件的输出是序列化形式。

a:1:{i:0;a:4:{s:13:"restaurant_id";s:4:"1519";s:7:"new_lat";s:8:"14.63823";s:8:"new_long";s:9:"121.02999";s:12:"date_updated";s:19:"2013-11-15 12:42:59";}}

这是所有人请帮助我。我现在有一个截止日期。我坚持不懈。 :-(这是我第一次基于文本文件创建CRUD,这就是我调试它时遇到问题的原因。

1 个答案:

答案 0 :(得分:1)

你能试试吗,

        <?php   

        /****This is the BASE array from the existing textfile ****/
        $data = unserialize(file_get_contents('addresses.txt'));

        $restaurant_id = '1519';
        $new_lat_entry = '14.64823';
        $new_long_entry = '121.45999';

        /****Here's my new updated array ****/
        $data_add = array(
                'restaurant_id' => $restaurant_id,
                'new_lat' => $new_lat_entry,
                'new_long' => $new_long_entry,
                'date_updated' => date('Y-m-d H:i:s')
        );

        $target = $data_add['restaurant_id'];

        $Count =count($data);
        $new_array =array();
        for ($i = 0; $i < $Count; $i++) {

             $get_id = $data[$i]['restaurant_id'];


                //If ID is found - UPDATE   

                $add_data = array(
                        $i => $data_add
                );

                if($get_id == $target){
                    $new_array = array_replace($data,$add_data);    
                }

        }
        $serialize_data= serialize($new_array);

        print_r($serialize_data);
        $file_input_txt = fopen("addresses.txt","w+");
        fwrite($file_input_txt, $serialize_data);
        fclose($file_input_txt);


    ?>