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
Related
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;
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
I have a generated data from get_post_meta which I need to convert it to JSON. The generated array are as follows:
Array
(
[0] => Array
(
[0] => Array
(
[0] => Array
(
[rate] => 5
[review] => Array
(
[content] => Test Review
[custom_field_1] => Azman Ishak
[custom_field_2] => Selagnor
[date] => 2017-06-06 , 15:06
[title] =>
[status] => publish
[weight] => 5
)
[user] => Array
(
[name] => ebeneadmin
[email] => azman#considerdigital.com
[id] => 1
[ip] => 115.135.10.5
)
)
)
)
[1] => Array
(
[0] => Array
(
[0] => Array
(
[rate] => 4
[review] => Array
(
[content] => Test review 2
[custom_field_1] => Azman
[custom_field_2] => Beranang
[date] => 2017-06-06 , 16:01
[title] =>
[status] => publish
[weight] => 5
)
[user] => Array
(
[name] => Azman Ishak
[email] => azmunk#gmail.com
[id] =>
[ip] => 115.135.10.5
)
)
)
)
[2] => Array
(
[0] => Array
(
)
)
)
The value that I need to get from the above arrays are:
rate
content
custom_field_1
custom_field_2
date
title
status
This data need to be converted into JSON.
Try this:
convert your get_post_meta result with json_encode();
it will make your result to be
<?php
$arr = array('a' => 1, 'b' => 2, 'c' => 3, 'd' => 4, 'e' => 5);
echo json_encode($arr);
?>
Output:
{"a":1,"b":2,"c":3,"d":4,"e":5}
I have one array in two format. I want to change array from
Array
(
[step_number] => 4
[app_id] => Array
(
[0] => 2
[1] => 3
)
[formdata] => Array
(
[0] => Array
(
[name] => app_id[]
[value] => 2
)
[1] => Array
(
[name] => app_id[]
[value] => 3
)
[2] => Array
(
[name] => fieldval[2][2][]
[value] => 1
)
[3] => Array
(
[name] => fieldval[3][3][]
[value] => 200
)
[4] => Array
(
[name] => fieldval[3][3][]
[value] => day
)
[5] => Array
(
[name] => title
[value] => new plan
)
[6] => Array
(
[name] => feature_plan
[value] => 3
)
[7] => Array
(
[name] => plan_type
[value] => free
)
[8] => Array
(
[name] => price
[value] =>
)
[9] => Array
(
[name] => sell_type
[value] => us
)
)
)
this format to
Array
(
[app_id] => Array
(
[0] => 2
[1] => 3
)
[fieldval] => Array
(
[2] => Array
(
[2] => Array
(
[0] => 1
)
)
[3] => Array
(
[3] => Array
(
[0] => 200
[1] => day
)
)
)
[title] => new plan
[feature_plan] => 3
[plan_type] => free
[price] =>
[sell_type] => us
)
these are are one array into two format. i have data in to first array format and i want to change that format to second array type format.
please tell me how i am trying this for 2 days but not succeed.
Here is a function you could use to produce that conversion:
function convert_formdata($input) {
$output = array();
foreach($input['formdata'] as $data) {
$keys = preg_split("#[\[\]]+#", $data['name']);
$value = $data['value'];
$target = &$output;
foreach($keys as $key) {
// Get index for "[]" reference
if ($key == '') $key = count($target);
// Create the key in the parent array if not there yet
if (!isset($target[$key])) $target[$key] = array();
// Move pointer one level down the hierarchy
$target = &$target[$key];
}
// Write the value at the pointer location
$target = $value;
}
return $output;
}
You would call it like this:
$output = convert_formdata($input);
See it run on eval.in for the given input. The output is:
array (
'app_id' =>
array (
0 => 2,
1 => 3,
),
'fieldval' =>
array (
2 =>
array (
2 =>
array (
0 => 1,
),
),
3 =>
array (
3 =>
array (
0 => 200,
1 => 'day',
),
),
),
'title' => 'new plan,',
'feature_plan' => 3,
'plan_type' => 'free',
'price' => NULL,
'sell_type' => 'us',
)
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