从关联数组中删除元素

时间:2016-09-07 07:26:47

标签: php associative-array

This post描述了如何使用Unset删除关联数组的元素,即取消设置($ array [' key1']);

我有这个数组:

Array
(
[queryLocator] => 
[done] => 1
[records] => Array
    (
        [0] => stdClass Object
            (
                [Id] => 
                [CreatedDate] => 2016-08-28T14:43:45.000Z
                [Leader__c] => GF
                [Location__c] => Postbridge
                [Service_Date__c] => 2016-09-03
                [Service_Time__c] => 14:30
                [Service_Type__c] => Baptism
            )



    )

[size] => 42
[pointer] => 0
[QueryResultsf] => SforceEnterpriseClient Object
    (
        [sforce:protected] => SoapClient Object
            (
                [trace] => 1
                [compression] => 32
                [_encoding] => utf-8
                [_features] => 1
                [_user_agent] => salesforce-toolkit-php/20.0
                [_soap_version] => 1
                [sdl] => Resource id #8                 
        [packageVersionHeader:protected] => 
        [client_id:protected] => 
    )

) )

我想删除键[queryLocator],用[total]替换键[done],用[rows]替换键[records]并删除所有后续键,即[size],[pointer]等。< / p>

使用unset,即 unset($ array [&#39; queryLocator&#39;]); 无效。

我做错了什么?感谢。

1 个答案:

答案 0 :(得分:1)

以下是实现我想要的代码 - 从Sales Force查询中获取输出并根据EasyUI数据网格的要求对其进行格式化。

 //--Get the Sales Force Data
 $response = $mySforceConnection->query($query);

//--Encode and decode - for some reason 
$data = json_encode((array)$response);
$x = json_decode($data,true);

//--Empty Array
$q = array();


//--Add array element for number records 
$q['total'] = $numRecs;
//--Copy the array element from original query with data
$q['rows'] = $x['records'];

//--JSON Encode the new array
$y = json_encode($q);

//--Return the array to Ajax call
echo ($y);

这里是经过验证的JSON的片段..

 {  
 "total":193,
 "rows":[  
  {  
     "Id":null,
     "CreatedDate":"2016-08-28T14:43:45.000Z",
     "Leader__c":"GF",
     "Location__c":"Postbridge",
     "Service_Date__c":"2016-09-03",
     "Service_Time__c":"14:30",
     "Service_Type__c":"Baptism"
  },
  {  
     "Id":null,
     "CreatedDate":"2016-08-17T20:43:10.000Z",
     "Leader__c":"GF",
     "Location__c":"Ashburton",
     "Service_Date__c":"2016-09-04",
     "Service_Time__c":"08:00",
     "Service_Type__c":"HC 2"
  },
  {  
     "Id":null,
     "CreatedDate":"2016-08-17T20:43:10.000Z",
     "Leader__c":"GF",
     "Location__c":"Bickington",
     "Service_Date__c":"2016-09-04",
     "Service_Time__c":"09:00",
     "Service_Type__c":"HC 2"
  },
  {  
     "Id":null,
     "CreatedDate":"2016-08-17T20:43:10.000Z",
     "Leader__c":"MC",
     "Location__c":"Holne",
     "Service_Date__c":"2016-09-04",
     "Service_Time__c":"10:30",
     "Service_Type__c":"HC 1"
  },

我在数组操作上有点挣扎,但它确实有效。任何代码改进建议都是最受欢迎的!

相关问题