-3

This post describes how to remove an element of an associative array with Unset i.e unset($array['key1']);

I have this array:

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] => 
    )

) )

I want to delete the key [queryLocator], replace the key [done] with [total], replace the key [records] with [rows] and delete all subsequent keys i.e [size], [pointer] etc.

Using unset, i.e unset($array['queryLocator']); has no effect.

What am I doing wrong ? Thanks.

4
  • just use unset($array_values['queryLocator']); Commented Sep 7, 2016 at 7:30
  • $array_values is undefined. Did you mean ? $result = array($response); unset(array_values($result['queryLocator'])); This gives Fatal error: Can't use function return value in write context in (unset line). Commented Sep 7, 2016 at 8:29
  • $array_values should be replaced by your array variable name @peter Commented Sep 7, 2016 at 8:41
  • Sorry but $result = array($response); unset($result['queryLocator']); does nothing - no error but the queryLocator key is still present. @jothi Commented Sep 7, 2016 at 9:02

1 Answer 1

1

Here is the code that achieves what I wanted - taking output from a Sales Force query and formatting it as required by a EasyUI datagrid.

 //--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);

And here's a snippet of the validated 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"
  },

I struggled a bit with the array manipulation but it works. Any code improvement suggestions most welcome !

Sign up to request clarification or add additional context in comments.

Comments

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.