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'.
array_push
adds new element at the end of array. If you want to replace something in array, use this construction$arr[$index] = $data
$contentDataArray[0] = $newMemberData;
- you're assuming this element is always at index point 0, this isn't dynamic @potiev