关于php:从关联数组中删除元素

Removing elements from an associative array

本文描述了如何删除带有unset的关联数组元素,即unset($array['key1']);

我有这个阵列:

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
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]等。

使用unset,即unset($array['querylocator']);无效。

我做错什么了?谢谢。


下面是实现我想要的代码——从Salesforce查询中获取输出,并根据easyui数据报的要求对其进行格式化。

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
 //--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。

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
 {  
"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"
  },

我在数组操作上有点纠结,但它起作用了。欢迎提出任何代码改进建议!