1

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'.

6
  • array_push adds new element at the end of array. If you want to replace something in array, use this construction $arr[$index] = $data
    – potiev
    Jul 25, 2019 at 10:48
  • I have already tried this, but did not replace it but always created a new one. Jul 25, 2019 at 10:49
  • Here you can see my solution 3v4l.org/AklO9
    – potiev
    Jul 25, 2019 at 10:58
  • $contentDataArray[0] = $newMemberData; - you're assuming this element is always at index point 0, this isn't dynamic @potiev
    – Jaquarh
    Jul 25, 2019 at 10:59
  • It is as example, you can use any unique id
    – potiev
    Jul 25, 2019 at 10:59

2 Answers 2

0

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'],
        );
2
  • Now i try this, after I test it and get the answer if this work or not for me. Thanks. Jul 29, 2019 at 9:05
  • Updated answer to provide full example @AndreaFiore
    – Jaquarh
    Jul 29, 2019 at 9:42
0

Ok I solved the problem, thanks a lot to @Jaquarh. Here is the final code I used: 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'],
            'try'=> $indexs[0],
            'tryxxx'=> $find,
           );
       $contenentData = json_encode($elaborateNewMember, JSON_PRETTY_PRINT);

       if(file_put_contents($getOldData, $contenentData)) {
            echo 'Aggiornamento riuscito.';
        }
       else 
            echo "Errore.";

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

?>

Thanks a lot for your help.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.