首页
学习
活动
专区
工具
TVP
发布
社区首页 >问答首页 >PHP。更改数组的数组中的值无效

PHP。更改数组的数组中的值无效
EN

Stack Overflow用户
提问于 2018-06-30 05:03:57
回答 1查看 67关注 0票数 1

非常奇怪的问题:我尝试使用json_encode和json_decode更改json字符串。我将json字符串转换为数组(实际上是数组的数组),并尝试更改其中的一些数据。但不知何故,我无法在内部数组中更改数据。

代码语言:javascript
复制
$jsondata = '  {
          "Id": "0400006",
          "LastName": "Lincoln",
          "FirstName": "Abraham",
          "PreferredName": "Mr. Abraham Lincoln",
          "BirthDate": "1992-06-20T00:00:00",
          "PreferredEmailAddress": null,
          "Addresses": [
            {
              "AddressId": "297143",
              "Type": "Home",
              "AddressLines": [
                "201 S Grant Ave"
              ],
              "AddressModifier": "",
              "City": "Columbus",
              "State": "OH",
              "PostalCode": "43215",
              "County": "049",
              "Country": "",
              "RouteCode": "",
              "PhoneNumbers": [
                {
                  "Number": "614-555-6666",
                  "Extension": "",
                  "TypeCode": "HOME"
                }
              ],
              "AddressLabel": [
                "201 S Grant Ave",
                "Columbus, OH 43215"
              ],
              "PersonId": "0400006",
              "EffectiveStartDate": "2008-06-13T00:00:00",
              "EffectiveEndDate": null,
              "IsPreferredAddress": true,
              "IsPreferredResidence": true,
              "TypeCode": "H",
              "CountryCode": ""
            },
            {
              "AddressId": "727032",
              "Type": "Web Address",
              "AddressLines": [
                "285 E. Main Ave",
                "Apt B"
              ],
              "AddressModifier": "",
              "City": "Columbus",
              "State": "OH",
              "PostalCode": "43215",
              "County": "049",
              "Country": "",
              "RouteCode": "",
              "PhoneNumbers": [
                {
                  "Number": "614-555-6666",
                  "Extension": "",
                  "TypeCode": "HOME"
                }
              ],
              "AddressLabel": [
                "285 E. Main Ave",
                "Apt B",
                "Columbus, OH 43215"
              ],
              "PersonId": "0400006",
              "EffectiveStartDate": "2018-06-25T00:00:00",
              "EffectiveEndDate": null,
              "IsPreferredAddress": false,
              "IsPreferredResidence": false,
              "TypeCode": "WEB",
              "CountryCode": ""
            }
          ],
          "EmailAddresses": [],
          "Phones": [
            {
              "Number": "614-555-6666",
              "Extension": "",
              "TypeCode": "HOME"
            }
          ],
          "AddressConfirmationDateTime": null,
          "EmailAddressConfirmationDateTime": null,
          "PhoneConfirmationDateTime": null,
          "LastChangedDateTime": "2018-06-27T20:42:22Z",
          "ChosenFirstName": "",
          "ChosenMiddleName": "",
          "ChosenLastName": "",
          "PersonalPronounCode": "",
          "IsDeceased": false
        }
    ';


$newAddressData = [
    "address1" => "5857 Newbridge Dr.",
    "address2" => "Apt D",
     "city" => "Chicago",
     "state" => "Illions",
     "postalcode" => "23456"
    ];

public function modifyData($jsonStr, $newAddressData){
        $personInfoData = json_decode($jsonStr, true);
        $addressArray = $personInfoData['Addresses'];
        $webAddressObj = null;
        foreach($addressArray as $address){
            if($address['Type'] == 'Web Address' ){
                $webAddressObj = &$address;
                break;
            }
        }
        if($webAddressObj != null){
            echo("update it!");
            $webAddressObj['AddressId'] ='';
            $webAddressObj['PhoneNumbers']=[];
            $webAddressObj['AddressLabel'] =[];

            $webAddressObj['AddressLines'] =[$newAddressData['address1'], $newAddressData['address2']];
            $webAddressObj['EffectiveStartDate']='';//2018-06-25T00:00:00
            $webAddressObj['City']=$newAddressData['city'];
            $webAddressObj['State']=$newAddressData['state'];
            $webAddressObj['PostalCode']=$newAddressData['postalcode'];


        }else{
            echo("new address");
            $newAddress = new AddressInfo();
            $newAddress->Type = "Web Address";
            $newAddress->TypeCode = "WEB";
            $newAddress->AddressLines =[$newAddressData['address1'], $newAddressData['address2']];
            $newAddress->EffectiveStartDate ='';
            $newAddress->City = $newAddressData['city'];
            $newAddress->State=$newAddressData['state'];
            $newAddress->PostalCode=$newAddressData['postalcode'];
            $newAddressJson = json_encode($newAddress);
            $addressArray[] = $newAddressJson;
        }
        print_r($webAddressObj);
        echo "<p>";
        print_r($addressArray);
        echo "<p>";
        return $personInfoData;






    }

    $personalInfoData = modifyData($jsondata, $newAddressData);
    echo json_encode($personalInfoData);

========================================你会发现$personalInfoData并没有改变。

EN

回答 1

Stack Overflow用户

回答已采纳

发布于 2018-06-30 05:12:27

这一点:$webAddressObj = &$address;不会创建对原始数组元素的引用。它创建对foreach创建的元素的临时副本的引用。在您跳出循环后,对该副本的引用将保持不变,但对其进行更改不会影响原始数组。

如果您希望引用原始元素,则还需要首先按引用分配$addressArray

代码语言:javascript
复制
$addressArray = &$personInfoData['Addresses'];

您需要在foreach循环中维护引用,例如

代码语言:javascript
复制
foreach($addressArray as &$address){

而且你仍然需要像现在一样在循环中通过引用赋值。

代码语言:javascript
复制
$webAddressObj = &$address;

与跟踪所有引用相比,如果直接通过键引用原始数组可能更容易。

代码语言:javascript
复制
$webAddressKey = null;
foreach($addressArray as $key => $address){
    if($address['Type'] == 'Web Address' ){
        $webAddressKey = $key;
        break;
    }
}
if($webAddressKey != null){
    echo("update it!");
    $personInfoData['Addresses'][$webAddressKey]['AddressId'] ='';
    ...
}
票数 3
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/51109382

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档