简体   繁体   English

如何访问数组(json)中对象的位置并通过php更改其值

[英]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.甚至查看它的唯一 id。

The first element had to overwrite the second one instead created a new one at the beginning of the array.第一个元素必须覆盖第二个元素,而不是在数组的开头创建一个新元素。 json 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

<?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'.我试过了,但是在 json 文件中它添加了成员​​以在索引为“i”的新对象中进行更改。

You can use array_filter to preserve the array keys.您可以使用array_filter来保留数组键。

$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'],
        );

Ok I solved the problem, thanks a lot to @Jaquarh.好的,我解决了这个问题,非常感谢@Jaquarh。 Here is the final code I used: php这是我使用的最终代码: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.非常感谢你的帮助。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM