简体   繁体   English

PHP - 向数组中的每个 json 对象动态添加元素

[英]PHP - Adding element dynamically to each json object in array

I am using PHP for adding the JSON element to array.我正在使用 PHP 将 JSON 元素添加到数组。

This is my database table:这是我的数据库表:

在此处输入图片说明

Here is my json array:这是我的 json 数组:

array:8 [

  0 => {#305
    +"Queue": "755"
    +"Location": "427"
    +"MemberName": "PJSIP/427"
    +"Status": "5"
  }
  1 => {#306
    +"Queue": "752"
    +"Location": "427"
    +"MemberName": "PJSIP/427"
    +"Status": "5"
  }
  2 => {#304
    +"Queue": "750"
    +"Location": "427"
    +"MemberName": "PJSIP/427"
    +"Status": "5"
  }
]

Now I want to add callQueueName to every json object.现在我想将callQueueName添加到每个 json 对象。 Tried using put function as shown in below code but not getting added.尝试使用 put 函数,如下面的代码所示,但没有被添加。

Here is my working code:这是我的工作代码:

public static function getLoginAgentInfoTest(){
    $loginAgentInfo = 
      '[{"Queue":"755","Location":"427","MemberName":"PJSIP\/427","Status":"5"},
        {"Queue":"752","Location":"427","MemberName":"PJSIP\/427","Status":"5"},
        {"Queue":"750","Location":"427","MemberName":"PJSIP\/427","Status":"5"}]';

    $loginAgentInfo = json_decode($loginAgentInfo);
    
    $i = 0;
      
    foreach($loginAgentInfo[$i] as $loginData){ 
        
        $callQueueName = Tbcdrqueues::where('callqueue_no',$loginAgentInfo[$i]->Queue)->value('callqueue_name');
 
        $loginAgentInfo[$i].put('callQueueName',$callQueueName);
    
        $i++;
    }

    return $loginAgentInfo;
}

Expected output is:预期输出为:

array:8 [

  0 => {#305
    +"Queue": "755"
    +"Location": "427"
    +"MemberName": "PJSIP/427"
    +"Status": "5"
    +"callQueueName" : "New claims"
  }
  1 => {#306
    +"Queue": "752"
    +"Location": "427"
    +"MemberName": "PJSIP/427"
    +"Status": "5"
    +"callQueueName" : "Billing"
  }
  2 => {#304
    +"Queue": "750"
    +"Location": "427"
    +"MemberName": "PJSIP/427"
    +"Status": "5"
    +"callQueueName" : "Customer_Service"
  }
]

How can I achieve that ?我怎样才能做到这一点?

foreach($loginAgentInfo as $key => $loginData){ 
     $callQueueName = Tbcdrqueues::where('callqueue_no',$loginData->Queue)->value('callqueue_name');
     $loginAgentInfo[$key]->callQueueName = $callQueueName;
}

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

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