Skip to content
Advertisement

How can I access the position of an object in the array (json) and change its values by php

I have to access the array and change the data of the member chosen to modify and overwrite the old ones, I have tried several times but does nothing but add it instead of removing it.

I tried passing the position of the element through its index but it doesn’t replace it. Even looking through its unique id.

The first element had to overwrite the second one instead created a new one at the beginning of the array. json

[
    {
        "surname": "",
        "name": "Andrea Fiore",
        "city": "Crocino",
        "address": "via Emo Mannucci, 2",
        "telephone": "123456789",
        "email": "x@gmail.com",
        "startData": "2019-07-04",
        "finishData": "2019-07-25",
        "cardId": "2",
        "state": "true"
    },
    {
        "surname": "Fioregg",
        "name": "Andrea Fiore",
        "city": "Crocino",
        "address": "via Emo Mannucci, 2",
        "telephone": "123456789",
        "email": "x@gmail.com",
        "startData": "2019-07-04",
        "finishData": "2019-07-25",
        "cardId": "2",
        "state": "true"
    }
]

The last code I used to try to modify the member. php

<?php

   $getOldData = "data.php";
   $elaborateNewMember = array();
  try
  {
       $contenentData = file_get_contents($getOldData);

       $elaborateNewMember = json_decode($contenentData, true);
       $find = array_filter($elaborateNewMember, function($searchMember) {
           return $searchMember['cardId'] == $_POST['cardId'];
       });
       if(count(($indexs = array_keys($find))) == 1) {
           $elaborateNewMember[indexs[0]] = array(
            'surname'=> $_POST['surname'],
            'name'=> $_POST['name'],
            'city'=> $_POST['city'],
            'address'=> $_POST['address'],
            'telephone'=> $_POST['telephone'],
            'email'=> $_POST['email'],
            'startData'=> $_POST['startData'],
            'finishData'=> $_POST['finishData'],
            'cardId'=> $_POST['cardId'],
            'state'=> $_POST['state'],
           );
       $contenentData = json_encode($elaborateNewMember, JSON_PRETTY_PRINT);
       
       if(file_put_contents($getOldData, $contenentData)) {
            echo 'Aggiornamento riuscito.';
        }
       else 
            echo "Errore.";

   }
  }
   catch (Exception $e) {
            echo 'Eccezione: ',  $e->getMessage(), "n";
   }

?>

I tried this but after in the json file it add the member to change in new object with index ‘i’.

Advertisement

Answer

You can use array_filter to preserve the array keys.

$data = json_decode(file_get_contents('data.php'), true);    

$filter = array_filter($data, function($row) {
    return $row['email'] == $_POST['email']; // Assuming this is unique
});

If a match was returned, and only one, you can update that index in your data.

if(count(($indexs = array_keys($filter))) == 1) {
    $data[$indexs[0]] = array(
        'surname'=> $_POST['surname'],
        'name'=> $_POST['name'],
        'city'=> $_POST['city'],
        'address'=> $_POST['address'],
        'telephone'=> $_POST['telephone'],
        'email'=> $_POST['email'],
        'startData'=> $_POST['startData'],
        'finishData'=> $_POST['finishData'],
        'cardId'=> $_POST['cardId'],
        'state'=> $_POST['state'],
    );
}

Then just re-write the file. See a working demo here

If you have multiple emails for that user you want to update the arrays for, you can loop through the indexs.

if(count(($indexs = array_keys($filter))) >= 1)
    foreach($indexs as $key)
        $data[$key] = array(
            'surname'=> $_POST['surname'],
            'name'=> $_POST['name'],
            'city'=> $_POST['city'],
            'address'=> $_POST['address'],
            'telephone'=> $_POST['telephone'],
            'email'=> $_POST['email'],
            'startData'=> $_POST['startData'],
            'finishData'=> $_POST['finishData'],
            'cardId'=> $_POST['cardId'],
            'state'=> $_POST['state'],
        );
User contributions licensed under: CC BY-SA
2 People found this is helpful
Advertisement