【问题标题】:Removing elements from an associative array从关联数组中删除元素
【发布时间】:2017-01-14 18:42:29
【问题描述】:

This post 描述了如何使用 Unset 移除关联数组的元素,即 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],将键 [done] 替换为 [total],将键 [records] 替换为 [rows] 并删除所有后续键,即 [size]、[pointer] 等。

使用unset,即unset($array['queryLocator']);没有效果。

我做错了什么?谢谢。

【问题讨论】:

  • 只使用 unset($array_values['queryLocator']);
  • $array_values 未定义。你的意思是 ? $result = 数组($response); unset(array_values($result['queryLocator']));这给出了致命错误:Can't use function return value in write context in (unset line)。
  • $array_values 应替换为您的数组变量名称@peter
  • 对不起,但是 $result = array($response); unset($result['queryLocator']);什么都不做 - 没有错误,但 queryLocator 键仍然存在。 @jothi

标签: php associative-array


【解决方案1】:

这是实现我想要的代码 - 从销售队伍查询中获取输出并按照 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 的 sn-p..

 {  
 "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"
  },

我在数组操作方面有点吃力,但它确实有效。欢迎任何代码改进建议!

【讨论】:

    猜你喜欢
    • 2011-07-23
    • 1970-01-01
    • 2011-12-31
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2016-03-27
    • 2012-12-01
    • 1970-01-01
    相关资源
    最近更新 更多