Converting a php object into associative array - php

I am trying to read the following php object result of an api call into an associative array. Has someone done this before or can otherwise help me?
I have tried object var dump, and array casting, but this just doesnt seems to work. I know I am doing something fundamentally wrong. Can someone help?
Many thanks.
Google_Service_Directory_Users Object
(
[etag] => "sfgsfgdsfgsdfgjkdjgfd"
[kind] => admin#directory#users
[nextPageToken] =>
[triggerEvent] =>
[usersType:protected] => Google_Service_Directory_User
[usersDataType:protected] => array
[collection_key:protected] => items
[modelData:protected] => Array
(
[users] => Array
(
[0] => Array
(
[kind] => admin#directory#user
[id] => 7642341239423
[etag] => "jasdfjshwer43537345fsdfs"
[primaryEmail] => info#example.com
[name] => Array
(
[givenName] => Info -
[familyName] => Example
[fullName] => Info - Example
)
[isAdmin] =>
[isDelegatedAdmin] =>
[lastLoginTime] => 2014-07-29T08:46:28.000Z
[creationTime] => 2014-07-29T08:31:56.000Z
[agreedToTerms] => 1
[suspended] =>
[changePasswordAtNextLogin] =>
[ipWhitelisted] =>
[emails] => Array
(
[0] => Array
(
[address] => info#example.com
[primary] => 1
)
)
[nonEditableAliases] => Array
(
[0] => info#example.com.test-google-a.com
)
[customerId] => fsdfdd4
[orgUnitPath] => /
[isMailboxSetup] => 1
[includeInGlobalAddressList] => 1
)
[1] => Array
(
[kind] => admin#directory#user
[id] => 3895729453245
[etag] => "fsajdfd64hkj4534h5k3454"
[primaryEmail] => user#example.com
[name] => Array
(
[givenName] => User
[familyName] => Name
[fullName] => User Name
)
[isAdmin] => 1
[isDelegatedAdmin] =>
[lastLoginTime] => 2014-08-26T09:05:49.000Z
[creationTime] => 2012-09-16T08:55:26.000Z
[agreedToTerms] => 1
[suspended] =>
[changePasswordAtNextLogin] =>
[ipWhitelisted] =>
[emails] => Array
(
[0] => Array
(
[address] => support#example.com
)
[1] => Array
(
[address] => help#example.com
)
)
[customerId] => fsdafwr4
[orgUnitPath] => /
[isMailboxSetup] => 1
[includeInGlobalAddressList] => 1
)
)
)
[processed:protected] => Array
(
)
)

First solution:
$array = json_decode(json_encode($nested_object), true);
Second solution:
function object_to_array($data) {
if (is_array($data) || is_object($data)):
$result = array();
foreach ($data as $key => $value)
$result[$key] = object_to_array($value);
return $result;
endif;
return $data;
}
both searched from the internetz

Related

PHP array merge to subarray with keys

I have a problem with merging array's. I'll try array merge and combine but nothing is working.
First array
Array (
[type1] => Array (
[userid] => Array (
[0] => 35
[1] => 37
)
[from] => Array (
[0] => 07-06-2017
[1] => 09-06-2017
)
[till] => Array (
[0] => 07-07-2017
[1] => 09-07-2017
)
)
[type3] => Array (
[userid] => Array (
[0] => 13
)
[from] => Array (
[0] => 10-06-2017
)
[till] => Array (
[0] => 10-07-2017
)
)
)
Second array
The second array is filled with room details, but the assigned users are not added yet.
Array (
[type1] => Array (
[m2] =>
[price] =>
[rooms] => 1
[extra] =>
[rented_to] => Array
(
[0] => Array
(
[userid] =>
[from] =>
[till] =>
)
)
)
[type3] => Array
(
[m2] =>
[price] =>
[rooms] => 1
[extra] =>
[rented_to] => Array
(
[0] => Array
(
[userid] =>
[from] =>
[till] =>
)
)
)
)
I'll will put these arrays together to one array, so that the data is insert into the "rented_to" section. How can I get this array into an another array like this:
Array (
[type1] => Array (
[m2] =>
[price] =>
[rooms] => 1
[extra] =>
[rented_to] => Array
(
[0] => Array
(
[userid] => 35
[from] => 07-06-2017
[till] => 07-07-2017
)
[1] => Array
(
[userid] => 37
[from] => 09-06-2017
[till] => 09-07-2017
)
)
)
[type3] => Array
(
[m2] =>
[price] =>
[rooms] => 1
[extra] =>
[rented_to] => Array
(
[0] => Array
(
[userid] => 13
[from] => 10-06-2017
[till] => 10-07-2017
)
)
)
)
The code
$manager_add_new_room_rented_to is an array that contains the value of the first array in my example.
$manager_add_new_room_type_desc = $_POST['manager_add_new_room_type_desc'];
$manager_add_new_room_type_m2 = $_POST['manager_add_new_room_type_m2'];
$manager_add_new_room_type_rent_price = $_POST['manager_add_new_room_type_rent_price'];
$manager_add_new_room_type_rooms = $_POST['manager_add_new_room_type_rooms'];
$manager_add_new_room_type_extra = $_POST['manager_add_new_room_type_extra'];
$manager_add_new_room_rented_to = $_POST['manager_add_new_room_rented_to'];
$add_new_room_information = array();
foreach ( $manager_add_new_room_type_desc as $key => $room_type ) :
$add_new_room_information[$room_type] = array(
'm2' => $manager_add_new_room_type_m2[$key],
'price' => $manager_add_new_room_type_rent_price[$key],
'rooms' => $manager_add_new_room_type_rooms[$key],
'extra' => $manager_add_new_room_type_extra[$key],
'rented_to' => array(
array(
'userid' => '',
'from' => '',
'till' => ''
)
)
);
endforeach;
Loop through $manager_add_new_room_rented_to[$room_type] and create the new array that you want.
foreach ( $manager_add_new_room_type_desc as $key => $room_type ) :
$renters = $manager_add_new_room_rented_to[$room_type];
$rented_to = array();
foreach ($renters['userid'] as $index => $userid) :
$rented_to[] = array('userid' => $userid, 'from' => $renters['from'][$index], 'to' => $renters['to'][$index]);
endforeach;
$add_new_room_information[$room_type] = array(
'm2' => $manager_add_new_room_type_m2[$key],
'price' => $manager_add_new_room_type_rent_price[$key],
'rooms' => $manager_add_new_room_type_rooms[$key],
'extra' => $manager_add_new_room_type_extra[$key],
'rented_to' => $rented_to
)
);
endforeach;

Get a specific value from a multidimensional array provided by AgileCRM

I'm trying to get the first deal ID from AgileCRM.
When using:
$test = json_decode($deal, true);
print_r($test);
I get the following result:
Array (
[0] => Array (
[colorName] => WHITE
[id] => 5686812383117312
[apply_discount] =>
[discount_value] => 0
[discount_amt] => 0
[discount_type] => Value
[name] => New Home Loan
[contact_ids] => Array (
[0] => 5645056174194688
)
[custom_data] => Array (
)
[products] => Array (
)
[description] => New Lead
[expected_value] => 0
[milestone] => New Loan
[probability] => 10
[close_date] => 1521192269
[created_time] => 1510824270
[milestone_changed_time] => 0
[entity_type] => deal
[notes] => Array (
)
[note_ids] => Array (
)
[note_created_time] => 0
[pipeline_id] => 5719238044024832
[archived] =>
[lost_reason_id] => 0
[deal_source_id] => 0
[total_deal_value] => 0
[updated_time] => 1510824270
[isCurrencyUpdateRequired] => 1
[currency_conversion_value] => 0
[tags] => Array (
)
[tagsWithTime] => Array (
)
[contacts] => Array (
[0] => Array (
[id] => 5645056174194688
[type] => PERSON
[properties] => Array (
[0] => Array (
[type] => SYSTEM
[name] => first_name
[value] => piet
)
[1] => Array (
[type] => SYSTEM
[name] => last_name
[value] => pompies
)
[2] => Array (
[type] => SYSTEM
[name] => name
[value] =>
)
)
)
)
[owner] => Array (
[id] => 5178546118721536
[domain] => domainname
[email] => myemail#email.com
[phone] =>
[name] => Piet Pompies
[pic] => https://d1gwclp1pmzk26.cloudfront.net/img/gravatar/48.png
[schedule_id] => Piet Pompies
[calendar_url] => https://homeside.agilecrm.com/calendar/Piet_Pompies
[calendarURL] => https://homeside.agilecrm.com/calendar/Piet_Pompies
)
)
)
I want to echo "5686812383117312" from "[id] => 5686812383117312" (4th line in the array above)
I've tried "foreach" statements but my expertise on it is limited and can't seem to get it right.
Any help will be appreciated.
In order to access the ID field you should:
get the array's first key
Access the required field
Array:
Array ( //$test
[0] => Array ( //first key [0]
[colorName] => WHITE
[id] => 5686812383117312 //the required field ['id']
[apply_discount] =>
PHP:
$test = json_decode($deal, true);
print_r($test);
echo $test[0]['id']; //Output: 5686812383117312

Read object/array in PHP

I am working on Facebook leads API and successfully getting response for my lead
use FacebookAds\Object\Lead;
$form = new Lead('LEAD_ID');
$re = $form->read();
echo '<pre>';
print_r($re);
and the response is in following format :
FacebookAds\Object\Lead Object
(
[changedFields:protected] => Array
(
)
[api:protected] => FacebookAds\Api Object
(
[session:FacebookAds\Api:private] => FacebookAds\Session Object
(
[appId:protected] => 448283968712152
[appSecret:protected] => 0cf8998603f3050f9d80ded4cecdb7f7
[accessToken:protected] => EAAGXtj62rdgBAP0ZCjmnddu3ZBIgZAECUluzPL6CGUuZB07tPUlERk4L1iRb2gy31qlUx2ExBENZBegpvfHxmxsJYks8rghrKOZBHMWEblEgsIvo5GF3ySzbyVszg7lbYIuBSgeFdzAMq6GuW6iZCQrgXd4KfAVoiQZD
[appSecretProof:protected] => c6112ce522414623dea1fb41cc29f6f15ca480845b8490d7c45ec67a485fa3f6
)
[logger:protected] => FacebookAds\Logger\NullLogger Object
(
)
[httpClient:protected] => FacebookAds\Http\Client Object
(
[requestPrototype:protected] => FacebookAds\Http\Request Object
(
[client:protected] => FacebookAds\Http\Client Object
*RECURSION*
[headers:protected] =>
[method:protected] => GET
[protocol:protected] => https://
[domain:protected] =>
[path:protected] =>
[graphVersion:protected] =>
[queryParams:protected] =>
[bodyParams:protected] =>
[fileParams:protected] =>
)
[responsePrototype:protected] => FacebookAds\Http\Response Object
(
[request:protected] =>
[statusCode:protected] =>
[headers:protected] =>
[body:protected] =>
[content:protected] =>
)
[defaultRequestHeaders:protected] => FacebookAds\Http\Headers Object
(
[storage:ArrayObject:private] => Array
(
[User-Agent] => fb-php-ads-2.5.1
[Accept-Encoding] => *
)
)
[adapter:protected] => FacebookAds\Http\Adapter\CurlAdapter Object
(
[curl:protected] => FacebookAds\Http\Adapter\Curl\Curl Object
(
[handle:protected] => Resource id #34
)
[opts:protected] => ArrayObject Object
(
[storage:ArrayObject:private] => Array
(
[78] => 10
[13] => 60
[19913] => 1
[42] => 1
[10065] => /home/leadsgeneration/public_html/v1/vendor/facebook/php-ads-sdk/src/FacebookAds/Http/../../../fb_ca_chain_bundle.crt
)
)
[client:protected] => FacebookAds\Http\Client Object
*RECURSION*
)
[caBundlePath:protected] => /home/leadsgeneration/public_html/v1/vendor/facebook/php-ads-sdk/src/FacebookAds/Http/../../../fb_ca_chain_bundle.crt
[defaultGraphBaseDomain:protected] => facebook.com
)
[defaultGraphVersion:protected] => 2.5
)
[parentId:protected] =>
[data:protected] => Array
(
[ad_id] =>
[adset_id] =>
[campaign_id] =>
[created_time] => 2016-04-18T04:17:55+0000
[field_data] => Array
(
[0] => Array
(
[name] => email
[values] => Array
(
[0] => varunroute69#gmail.com
)
)
[1] => Array
(
[name] => phone_number
[values] => Array
(
[0] => +919731688688
)
)
[2] => Array
(
[name] => full_name
[values] => Array
(
[0] => Varun Majety
)
)
)
[form_id] =>
[id] => 571052196402628
[post] =>
)
)
I want to store [data:protected][field_data] in database so i want to read this array but when i try to echo like echo '-->'.$re['FacebookAds\Object\Lead Object']['data:protected'][field_data][0]['name']; it throws me an error.
How to read the above format?Any help would be greatly appreciated.
$form = new \FacebookAds\Object\Lead('LEAD_ID');
$form->read();
echo '<pre>';
print_r($form->getData());
Will give you
Array
(
[ad_id] =>
[ad_name] =>
[adset_id] =>
[adset_name] =>
[campaign_id] =>
[campaign_name] =>
[created_time] => 2016-09-18T06:57:13+0000
[custom_disclaimer_responses] =>
[field_data] => Array
(
[0] => Array
(
[name] => full_name
[values] => Array
(
[0] =>
)
)
[1] => Array
(
[name] => email
[values] => Array
(
[0] => test#fb.com
)
)
[2] => Array
(
[name] => phone_number
[values] => Array
(
[0] =>
)
)
)
[form_id] =>
[id] => 1681384948848301
[is_organic] =>
[post] =>
)

Move array in array to parent array in php

echo "<pre>";
print_r($data);
echo "</pre>";
Array
(
[0] => Array
(
[Deposit] => Array
(
[id] => 63393
[amount] => 200.00
[date] => 2014-06-17 08:40:13
[details] =>
)
[User] => Array
(
[username] => presido490
[group_id] => 1
)
)
)
How to move [username] => presido490 from [User] => Array() to [Deposit] => Array() , i need output like that
Array
(
[0] => Array
(
[Deposit] => Array
(
[id] => 63393
[amount] => 200.00
[date] => 2014-06-17 08:40:13
[username] => presido490
[details] =>
)
[User] => Array
(
[group_id] => 1
)
)
)
Do it in a simple loop
foreach($dataas $key => $element) {
$data[$key]['Deposit']['username'] = $data[$key]['User']['username'];
unset($data[$key]['User']['username']);
}
you could do it in an array_walk() or array_map() instead of a loop if you'd prefer
EDIT
If you absolutely have to position username between date and details (suggesting you don't really understand how to use array data in the first place).
foreach($dataas $key => $element) {
$data[$key]['Deposit'] = array(
'id' => $data[$key]['Deposit']['id'],
'amount' => $data[$key]['Deposit']['amount'],
'date' => $data[$key]['Deposit']['date'],
'username' => $data[$key]['User']['username'],
'details' => $data[$key]['Deposit']['details'],
);
unset($data[$key]['User']['username']);
}
But this should make no difference to your code at all, other than adding overhead

how to get parent array index from multidimensional array in php

I have an array $arr.
when I print this it shows the results as follows.
Array
(
[0] => Array
(
[name] => homeandgarden-Control Type
[1] => Array
(
[0] => product
)
)
[1] => Array
(
[name] => homeandgarden-Depth
[1] => Array
(
[0] => product
)
)
[2] => Array
(
[name] => homeandgarden-Height
[1] => Array
(
[0] => product
)
)
[3] => Array
(
[name] => homeandgarden-Load Type
[1] => Array
(
[0] => product
)
)
[4] => Array
(
[name] => homeandgarden-Machine Type
[1] => Array
(
[0] => product
)
)
[5] => Array
(
[name] => homeandgarden-Type
[1] => Array
(
[0] => product
)
)
[6] => Array
(
[name] => homeandgarden-Width
[1] => Array
(
[0] => product
)
)
[7] => Array
(
[name] => computer & ele
[label] =>
[singular_label] =>
[hierarchical] => 1
[show_ui] => 1
[query_var] => 1
[rewrite] => 1
[rewrite_slug] =>
[0] => Array
(
[search_items] =>
[popular_items] =>
[all_items] =>
[parent_item] =>
[parent_item_colon] =>
[edit_item] =>
[update_item] =>
[add_new_item] =>
[new_item_name] =>
[separate_items_with_commas] =>
[add_or_remove_items] =>
[choose_from_most_used] =>
)
[1] => Array
(
[0] => product
)
)
[8] => Array
(
[name] => computer
[label] =>
[singular_label] =>
[hierarchical] => 1
[show_ui] => 1
[query_var] => 1
[rewrite] => 1
[rewrite_slug] =>
[0] => Array
(
[search_items] =>
[popular_items] =>
[all_items] =>
[parent_item] =>
[parent_item_colon] =>
[edit_item] =>
[update_item] =>
[add_new_item] =>
[new_item_name] =>
[separate_items_with_commas] =>
[add_or_remove_items] =>
[choose_from_most_used] =>
)
[1] => Array
(
[0] => product
)
)
[9] => Array
(
[name] => homeandgardenairconditioner-Type
[label] =>
[singular_label] =>
[hierarchical] => 1
[show_ui] => 1
[query_var] => 1
[rewrite] => 1
[rewrite_slug] =>
[0] => Array
(
[search_items] =>
[popular_items] =>
[all_items] =>
[parent_item] =>
[parent_item_colon] =>
[edit_item] =>
[update_item] =>
[add_new_item] =>
[new_item_name] =>
[separate_items_with_commas] =>
[add_or_remove_items] =>
[choose_from_most_used] =>
)
[1] => Array
(
[0] => product
)
)
[10] => Array
(
[name] => homeandgardendishwasher-Control Type
[label] =>
[singular_label] =>
[hierarchical] => 1
[show_ui] => 1
[query_var] => 1
[rewrite] => 1
[rewrite_slug] =>
[0] => Array
(
[search_items] =>
[popular_items] =>
[all_items] =>
[parent_item] =>
[parent_item_colon] =>
[edit_item] =>
[update_item] =>
[add_new_item] =>
[new_item_name] =>
[separate_items_with_commas] =>
[add_or_remove_items] =>
[choose_from_most_used] =>
)
[1] => Array
(
[0] => product
)
)
)
and I have a variable $categ.For example $categ = "homeandgardenairconditioner-Type"
then I want to know the index of the parent array whose "name" is "homeandgardenairconditioner-Type"
i.e --- o/p should be 9
how to get this index. Please help me
Thanks
I think you are looking for this:
function getIndex($name, $array){
foreach($array as $key => $value){
if(is_array($value) && $value['name'] == $name)
return $key;
}
return null;
}

Categories