A
A
Alexander2018-10-24 15:46:13
PHP
Alexander, 2018-10-24 15:46:13

Why does writing to json file go through multiple times?


I write an array to json

$jsonFileUser[$_SESSION['email']]["orders"][] = $content;
    print_r($jsonFileUser[$_SESSION['email']]["orders"]);

gives out:
Array ( [212] => Array ( [id] => 212 [name_order] => Получение разрешения на строительство [date] => 23.10.18 [order_id] => 1 [step] => 2 [answer_status] => received [performed] => true [inquiry] => Array ( [object_address] => [cad_number] => [name_obejct] => [floors] => [commissioning] => [electric] => [water] => [firefighting] => [consumption] => firefighting_max [directions_of_gas] => ) ) [213] => Array ( [name_order] => Получение разрешения на строительство [date] => 24.10.18 [order_id] => 1 [step] => 2 [answer_status] => received [performed] => true [inquiry] => Array ( [object_address] => asd [cad_number] => 12:12:3123123:123 [name_obejct] => BoilerRoom [floors] => 123 [commissioning] => 0333-03-12 [electric] => MoreHundredFifty [water] => waterSupply [firefighting] => 123 [consumption] => firefighting_max [directions_of_gas] => Горячее водоснабжение, Отопление, ) ) )

In short, it gets a value from what it was, and there was 1 record (212), writes a new one - 213, after transferring to json everything remains the same, only the format changes.
BUT
When I write through
file_put_contents($fileUser,$contentJson);
He writes already 2 times, that is, it turns out that 212, 213, 214 is written, although 214 should not be there.
{  
   "[email protected]":{  
      "name":"\u0412\u0438\u043a\u0442\u043e\u0440",
      "secname":"\u041a\u043e\u0442\u043e\u0432",
      "order":"true",
      "orders":{  
         "212":{  
            "id":212,
            "name_order":"\u041f\u043e\u043b\u0443\u0447\u0435\u043d\u0438\u0435 \u0440\u0430\u0437\u0440\u0435\u0448\u0435\u043d\u0438\u044f \u043d\u0430 \u0441\u0442\u0440\u043e\u0438\u0442\u0435\u043b\u044c\u0441\u0442\u0432\u043e",
            "date":"23.10.18",
            "order_id":"1",
            "step":"2",
            "answer_status":"received",
            "performed":"true",
            "inquiry":{  
               "object_address":null,
               "cad_number":null,
               "name_obejct":null,
               "floors":null,
               "commissioning":null,
               "electric":null,
               "water":null,
               "firefighting":null,
               "consumption":"firefighting_max",
               "directions_of_gas":""
            }
         },
         "213":{  
            "name_order":"\u041f\u043e\u043b\u0443\u0447\u0435\u043d\u0438\u0435 \u0440\u0430\u0437\u0440\u0435\u0448\u0435\u043d\u0438\u044f \u043d\u0430 \u0441\u0442\u0440\u043e\u0438\u0442\u0435\u043b\u044c\u0441\u0442\u0432\u043e",
            "date":"24.10.18",
            "order_id":"1",
            "step":"2",
            "answer_status":"received",
            "performed":"true",
            "inquiry":{  
               "object_address":"asd",
               "cad_number":"12:12:3123123:123",
               "name_obejct":"BoilerRoom",
               "floors":"123",
               "commissioning":"0333-03-12",
               "electric":"MoreHundredFifty",
               "water":"waterSupply",
               "firefighting":"123",
               "consumption":"firefighting_max",
               "directions_of_gas":"\u0413\u043e\u0440\u044f\u0447\u0435\u0435 \u0432\u043e\u0434\u043e\u0441\u043d\u0430\u0431\u0436\u0435\u043d\u0438\u0435, \u041e\u0442\u043e\u043f\u043b\u0435\u043d\u0438\u0435, "
            }
         }
      }
   }
}

This is how it should be in the file, in reality it turns out like this:
{  
   "[email protected]":{  
      "name":"\u0412\u0438\u043a\u0442\u043e\u0440",
      "secname":"\u041a\u043e\u0442\u043e\u0432",
      "order":"true",
      "orders":{  
         "212":{  
            "id":212,
            "name_order":"\u041f\u043e\u043b\u0443\u0447\u0435\u043d\u0438\u0435 \u0440\u0430\u0437\u0440\u0435\u0448\u0435\u043d\u0438\u044f \u043d\u0430 \u0441\u0442\u0440\u043e\u0438\u0442\u0435\u043b\u044c\u0441\u0442\u0432\u043e",
            "date":"23.10.18",
            "order_id":"1",
            "step":"2",
            "answer_status":"received",
            "performed":"true",
            "inquiry":{  
               "object_address":null,
               "cad_number":null,
               "name_obejct":null,
               "floors":null,
               "commissioning":null,
               "electric":null,
               "water":null,
               "firefighting":null,
               "consumption":"firefighting_max",
               "directions_of_gas":""
            }
         },
         "213":{  
            "name_order":"\u041f\u043e\u043b\u0443\u0447\u0435\u043d\u0438\u0435 \u0440\u0430\u0437\u0440\u0435\u0448\u0435\u043d\u0438\u044f \u043d\u0430 \u0441\u0442\u0440\u043e\u0438\u0442\u0435\u043b\u044c\u0441\u0442\u0432\u043e",
            "date":"24.10.18",
            "order_id":"1",
            "step":"2",
            "answer_status":"received",
            "performed":"true",
            "inquiry":{  
               "object_address":"asd",
               "cad_number":"12:12:3123123:123",
               "name_obejct":"BoilerRoom",
               "floors":"123",
               "commissioning":"0333-03-12",
               "electric":"MoreHundredFifty",
               "water":"waterSupply",
               "firefighting":"123",
               "consumption":"firefighting_max",
               "directions_of_gas":"\u0413\u043e\u0440\u044f\u0447\u0435\u0435 \u0432\u043e\u0434\u043e\u0441\u043d\u0430\u0431\u0436\u0435\u043d\u0438\u0435, \u041e\u0442\u043e\u043f\u043b\u0435\u043d\u0438\u0435, "
            }
         },
         "214":{  
            "name_order":"\u041f\u043e\u043b\u0443\u0447\u0435\u043d\u0438\u0435 \u0440\u0430\u0437\u0440\u0435\u0448\u0435\u043d\u0438\u044f \u043d\u0430 \u0441\u0442\u0440\u043e\u0438\u0442\u0435\u043b\u044c\u0441\u0442\u0432\u043e",
            "date":"24.10.18",
            "order_id":"1",
            "step":"2",
            "answer_status":"received",
            "performed":"true",
            "inquiry":{  
               "object_address":"asd",
               "cad_number":"12:12:3123123:123",
               "name_obejct":"BoilerRoom",
               "floors":"123",
               "commissioning":"0333-03-12",
               "electric":"MoreHundredFifty",
               "water":"waterSupply",
               "firefighting":"123",
               "consumption":"firefighting_max",
               "directions_of_gas":"\u0413\u043e\u0440\u044f\u0447\u0435\u0435 \u0432\u043e\u0434\u043e\u0441\u043d\u0430\u0431\u0436\u0435\u043d\u0438\u0435, \u041e\u0442\u043e\u043f\u043b\u0435\u043d\u0438\u0435, "
            }
         }
      }
   }
}

Answer the question

In order to leave comments, you need to log in

1 answer(s)
D
dollar, 2018-10-24
@AleksandrB

This line is executed 2 times:

$jsonFileUser[$_SESSION['email']]["orders"][] = $content;

Didn't find what you were looking for?

Ask your question

Ask a Question

731 491 924 answers to any question