I try this array with array_merge() not working.
I have array (array one);
Array (
[0] => mf_3
[1] => mf_2
[2] => mf_1
[3] => mf_7
[4] => mf_6
[5] => mf_4
)
And on array again (second array),
Array (
[0] => stdClass Object
(
[id] => 1
[name] => review_smartphone_display
[label] => Layar
[post_type] => review_smartphone
)
[1] => stdClass Object
(
[id] => 2
[name] => review_smartphone_launch
[label] => Peluncuran
[post_type] => review_smartphone
)
[2] => stdClass Object
(
[id] => 3
[name] => review_smartphone_platform
[label] => Platform
[post_type] => review_smartphone
)
[3] => stdClass Object
(
[id] => 4
[name] => review_smartphone_camera
[label] => Kamera
[post_type] => review_smartphone
)
[4] => stdClass Object
(
[id] => 6
[name] => review_smartphone_design
[label] => Desain
[post_type] => review_smartphone
)
[5] => stdClass Object
(
[id] => 7
[name] => review_smartphone_battery
[label] => Baterai
[post_type] => review_smartphone
)
)
Now, how to replace second array [id] with value from array one.
I want the result like this;
Array (
[0] => stdClass Object
(
[id] => mf_3
[name] => review_smartphone_display
[label] => Layar
[post_type] => review_smartphone
)
[1] => stdClass Object
(
[id] => mf_2
[name] => review_smartphone_launch
[label] => Peluncuran
[post_type] => review_smartphone
)
[2] => stdClass Object
(
[id] => mf_1
[name] => review_smartphone_platform
[label] => Platform
[post_type] => review_smartphone
)
[3] => stdClass Object
(
[id] => mf_7
[name] => review_smartphone_camera
[label] => Kamera
[post_type] => review_smartphone
)
[4] => stdClass Object
(
[id] => mf_6
[name] => review_smartphone_design
[label] => Desain
[post_type] => review_smartphone
)
[5] => stdClass Object
(
[id] => mf_4
[name] => review_smartphone_battery
[label] => Baterai
[post_type] => review_smartphone
)
)
Please help, thanks.
Make use of foreach like below
foreach($first_array as $index => $farray)
{
$second_array[$index]->id = $farray;
}
foreach ($first_array as $key => $value) {
$second_array[$key]->id = $value;
}
Related
This question already has an answer here:
How to extract and access data from JSON with PHP?
(1 answer)
Closed 3 years ago.
i have data in json something like that:
stdClass Object
(
[contacts] => stdClass Object
(
[14] => stdClass Object
(
[data] => stdClass Object
(
[email] => veer#gmail.com
[first_name] => veer
[last_name] =>
[user_id] => 16
[owner_id] => 0
[optin_status] => 0
[date_created] => 2020-01-20 13:14:54
[ID] => 14
[gravatar] => http://localhost:8888/wordpress/wp-content/themes/wp_lms/assets/images/avatar.jpg
[age] =>
)
[meta] => stdClass Object
(
[preferences_changed] => 1579526094
[user_login] => veer
[profile_picture] => http://localhost:8888/wordpress/wp-content/themes/wp_lms/assets/images/avatar.jpg
[birthday] =>
)
[tags] => Array
(
[0] => 92
[1] => 13
[2] => 12
[3] => 9
)
[files] => Array
(
)
)
[9] => stdClass Object
(
[data] => stdClass Object
(
[email] => singh.pratibha1432#gmail.com
[first_name] => Pratibha
[last_name] => Singh
[user_id] => 8
[owner_id] => 0
[optin_status] => 0
[date_created] => 2020-01-20 13:14:54
[ID] => 9
[gravatar] => http://localhost:8888/wordpress/wp-content/themes/wp_lms/assets/images/avatar.jpg
[age] =>
)
[meta] => stdClass Object
(
[preferences_changed] => 1579526094
[user_login] => pratibha
[profile_picture] => http://localhost:8888/wordpress/wp-content/themes/wp_lms/assets/images/avatar.jpg
[birthday] =>
)
[tags] => Array
(
[0] => 94
[1] => 93
[2] => 92
[3] => 91
[4] => 82
[5] => 15
[6] => 14
[7] => 13
[8] => 9
)
[files] => Array
(
)
)
[4] => stdClass Object
(
[data] => stdClass Object
(
[email] => singh.dev1432#gmail.com
[first_name] => Devesh
[last_name] => Singh
[user_id] => 7
[owner_id] => 0
[optin_status] => 0
[date_created] => 2020-01-20 13:14:54
[ID] => 4
[gravatar] => http://localhost:8888/wordpress/wp-content/themes/wp_lms/assets/images/avatar.jpg
[age] =>
)
[meta] => stdClass Object
(
[preferences_changed] => 1579526094
[user_login] => devesh
[profile_picture] => http://localhost:8888/wordpress/wp-content/themes/wp_lms/assets/images/avatar.jpg
[birthday] =>
)
[tags] => Array
(
[0] => 93
[1] => 92
[2] => 15
[3] => 12
[4] => 11
[5] => 9
)
[files] => Array
(
)
)
[2] => stdClass Object
(
[data] => stdClass Object
(
[email] => admin#gmail.com
[first_name] => veronica
[last_name] =>
[user_id] => 1
[owner_id] => 0
[optin_status] => 0
[date_created] => 2020-01-20 13:14:54
[ID] => 2
[gravatar] => http://localhost:8888/wordpress/wp-content/uploads/avatars/1/5dc525d984494-bpfull.jpg
[age] =>
)
[meta] => stdClass Object
(
[preferences_changed] => 1579526094
[user_login] => admin
[profile_picture] => http://localhost:8888/wordpress/wp-content/uploads/avatars/1/5dc525d984494-bpfull.jpg
[birthday] =>
)
[tags] => Array
(
[0] => 94
[1] => 92
[2] => 15
[3] => 14
[4] => 13
[5] => 9
)
[files] => Array
(
)
)
)
[status] => success
)
and now I am trying to fetch "email" from the contacts->id->data->email so for that i am using this code. I am trying to loop the things by which i can fetch email from all the ids present. and in the ids section, there is data and inside data the email is present so how can i fetch all emails from all the ids.
foreach ((Array)$body->contacts as $id => $values) {
foreach ($values as $data => $value) {
$emails = array('email'=>$value->email);
return $emails;
}
}
but it return only single data:
Array
(
[email] => veer#gmail.com
)
i want to fetch all the email like that:
Array
(
[0] => Array
(
[emails] => veer#gmail.com
)
[1] => Array
(
[emails] => singh.pratibha1432#gmail.com
)
[2] => Array
(
[emails] => singh.dev1432#gmail.com
)
[3] => Array
(
[emails] => admin#gmail.com
)
)
how can I achieve this?
Define null array and in foreach run store every email in that variable.
$emails = array();
foreach ((Array)$body->contacts as $id => $values) {
foreach ($values as $data => $value) {
$emails[] = array('emails'=>$contact->data->email;);
}
}
return $emails;
I get this array in response from webservice, so How do I read it in a foreach cycle? or some easy way to read it. The [group] they are more than 12 [id]
there is.
Array
( [response] => Array (
[single] => Array ( [parameters] => Array ( [0] => Array ( [name] => msgCode [value] => 0101 )
[1] => Array ( [name] => msgDesc [value] => OK )
[2] => Array ( [name] => status [value] => 1)
[3] => Array ( [name] => message [value] => Normal) ) )
[group] => Array ( [id] => N4BD767 [parameters] => Array ( [0] => Array ( [name] => idFee [value] => 000 ) echo
[1] => Array ( [name] => typeFee [value] => Cuota)
[2] => Array ( [name] => entryDate [value] => 2014-12-17T14:06:47-03:00 )
[3] => Array ( [name] => expirationDate [value] => 2015-12-05T00:00:00-03:00)
[4] => Array ( [name] => amountOrigin [value] => 221980)
[5] => Array ( [name] => surcharges [value] => 1856)
[6] => Array ( [name] => entity [value] => ONLINE)
[7] => Array ( [name] => feeStatus [value] => inicial )
[8] => Array ( [name] => tranNumber [value] => 27) ) ) ) )
Okay guys, this is what I did and it work fine. With no foreach.
$output = array();
array_walk_recursive($result, function($item,$key) use (&$output){
array_push($output,$key,$item);
});
echo var_dump($output);
I want to count the number of likes (and comments) on a Facebook post. But when there's more than 25 likes on a post, the results are paginated like that:
stdClass Object ( [data] => Array ( [0] => stdClass Object ( [id] => 802020049912534 [name] => Sagar Lama ) [1] => stdClass Object ( [id] => 590020061101130 [name] => Sheguey Squaad ) [2] => stdClass Object ( [id] => 696207733846316 [name] => Ossama Barcalona ) [3] => stdClass Object ( [id] => 1901218896769448 [name] => Gab Alviola ) [4] => stdClass Object ( [id] => 407069606150738 [name] => Thongloy Thichanon ) [5] => stdClass Object ( [id] => 947084195347741 [name] => Thomas Vujić ) [6] => stdClass Object ( [id] => 1614068872196230 [name] => เพื่à¸à¸™à¸à¸¹à¹€à¸ˆà¹‡à¸š ชีวิตมึงจบ ) [7] => stdClass Object ( [id] => 1431328280529413 [name] => Ali-Bilal Dernek ) [8] => stdClass Object ( [id] => 855095404568872 [name] => Henry Feng ) [9] => stdClass Object ( [id] => 937460179628960 [name] => Rex Lu ) [10] => stdClass Object ( [id] => 1627484497506023 [name] => Röbërt GärÄïâ ) [11] => stdClass Object ( [id] => 971640236220159 [name] => Reyjoel Jimeno ) [12] => stdClass Object ( [id] => 1644471585776456 [name] => Og Catwalk ) [13] => stdClass Object ( [id] => 1652037021731712 [name] => Rafik Benchibane ) [14] => stdClass Object ( [id] => 499561140196412 [name] => Lizy Smith ) [15] => stdClass Object ( [id] => 10207169299621544 [name] => Ngai Lam ) [16] => stdClass Object ( [id] => 505507699598850 [name] => Ëlloï Gonzalez Sanchez ) [17] => stdClass Object ( [id] => 802078276575586 [name] => Armando Sanchez ) [18] => stdClass Object ( [id] => 10205977306769395 [name] => Fredy Santizo ) [19] => stdClass Object ( [id] => 811409772291367 [name] => Malik Davis ) [20] => stdClass Object ( [id] => 842376069172521 [name] => Isak Renee Martinez ) [21] => stdClass Object ( [id] => 915675738506394 [name] => Kimora Stephens Davis ) [22] => stdClass Object ( [id] => 916531748393520 [name] => Isabel Averill ) [23] => stdClass Object ( [id] => 941049645918044 [name] => Mathias Tenezaca Mendez ) [24] => stdClass Object ( [id] => 1452189318420627 [name] => Andrew Hernandez ) ) [paging] => stdClass Object ( [cursors] => stdClass Object ( [after] => MTQ1MjE4OTMxODQyMDYyNw== [before] => ODAyMDIwMDQ5OTEyNTM0 ) [next] => https://graph.facebook.com/v2.3/15087023444_10153196470723445/likes?access_token=xxxxxxxxxxxxxxxxxxx&limit=25&after=MTQ1MjE4OTMxODQyMDYyNw%3D%3D ) )
How can I count likes and comments on a given post without asking the Facebook API too much. I'm using the PHP SDK
You can either use paging to go through all Likes, or you can use the summary parameter to get a total count:
/post-id?fields=likes.summary(true)
Part of the response:
"summary": {
"total_count": 393,
"can_like": true,
"has_liked": false
}
I am trying to create a tree inside a select box with an unlimited number of parent and children.
and my select box (html part) should be Access and I have given a diagram below.
I have a "categories" array which has two main arrays with an unlimited number of nodes (parent-child). The two main array names are [scope] => selectboxFirst and [scope] => selectboxsecond. There is a parentid which starts with 4000. I cannot hard code parentid in the code. The arrays come from a decoded json string.
I don't know how to loop through this array and show both arrays in a separate select box.
In the example array I have posted, there are two main arrays inside one array but they can also come in other arrays. I don't know how to display this in two select box as parent child.
All the [scope] => selectboxFirst categories will display in first select box and [scope] => selectboxsecond categories in second select box.
Array
(
[status] => Array
(
[message] => Good
[code] => 200
)
[categories] => Array
(
[0] => Array
(
[scope] => selectboxFirst
[categories] => Array
(
[0] => Array
(
[id] => 4001
[parentId] => 4000
[name] => Access
[children] => Array
(
[0] => Array
(
[id] => 4010
[parentId] => 4001
[name] => mine
[type] => Reference
)
[1] => Array
(
[id] => 4011
[parentId] => 4001
[name] => yours
[type] => Reference
)
)
[type] => Reference
)
[1] => Array
(
[id] => 4002
[parentId] => 4000
[name] => Communication
[children] => Array
(
[0] => Array
(
[id] => 4015
[parentId] => 4002
[name] => Physician
[children] => Array
(
[0] => Array
(
[id] => 4016
[parentId] => 4015
[name] => Helps blsys Understand
[type] => Reference
)
[1] => Array
(
[id] => 4017
[parentId] => 4015
[name] => Listens
[type] => Reference
)
)
[type] => Reference
)
[1] => Array
(
[id] => 4021
[parentId] => 4002
[name] => Clinical ps
[children] => Array
(
[0] => Array
(
[id] => 4022
[parentId] => 4021
[name] => yours ps/datasssss
[type] => Reference
)
)
[type] => Reference
)
[2] => Array
(
[id] => 4024
[parentId] => 4002
[name] => etc
[type] => Reference
)
)
[type] => Reference
)
[2] => Array
(
[id] => 4003
[parentId] => 4000
[name] => Office
[children] => Array
(
[0] => Array
(
[id] => 4026
[parentId] => 4003
[name] => Facilities/Environment
[children] => Array
(
[0] => Array
(
[id] => 4027
[parentId] => 4026
[name] => Noise
[type] => Reference
)
[1] => Array
(
[id] => 4028
[parentId] => 4026
[name] => Lighting
[type] => Reference
)
)
[type] => Reference
)
[1] => Array
(
[id] => 4032
[parentId] => 4003
[name] => Office ps
[children] => Array
(
[0] => Array
(
[id] => 4033
[parentId] => 4032
[name] => mine ps
[type] => Reference
)
)
[type] => Reference
)
[2] => Array
(
[id] => 4035
[parentId] => 4003
[name] => Billing
[type] => Reference
)
)
[type] => Reference
)
[3] => Array
(
[id] => 4004
[parentId] => 4000
[name] => Outgo
[children] => Array
(
[0] => Array
(
[id] => 4037
[parentId] => 4004
[name] => Diagnosis
[type] => Reference
)
[1] => Array
(
[id] => 4038
[parentId] => 4004
[name] => Quality of Procedure
[type] => Reference
)
)
[type] => Reference
)
[4] => Array
(
[id] => 4005
[parentId] => 4000
[name] => Loyalty
[children] => Array
(
[0] => Array
(
[id] => 4039
[parentId] => 4005
[name] => Likely to also
[type] => Reference
)
)
[type] => Reference
)
)
)
[1] => Array
(
[scope] => selectboxSecond
[categories] => Array
(
[0] => Array
(
[id] => 4251
[parentId] => 4250
[name] => Communication with datas
[children] => Array
(
[0] => Array
(
[id] => 4262
[parentId] => 4251
[name] => respect?
[type] => Reference
)
[1] => Array
(
[id] => 4263
[parentId] => 4251
[name] => you?
[type] => Reference
)
)
[type] => Reference
)
[1] => Array
(
[id] => 4252
[parentId] => 4250
[name] => Communication with Doctors
[children] => Array
(
[0] => Array
(
[id] => 4266
[parentId] => 4252
[name] => you with courtesy
[type] => Reference
)
)
[type] => Reference
)
[2] => Array
(
[id] => 4253
[parentId] => 4250
[name] => Responsiveness of data ps
[children] => Array
(
[0] => Array
(
[id] => 4269
[parentId] => 4253
[name] => During
[type] => Reference
)
[1] => Array
(
[id] => 4270
[parentId] => 4253
[name] => How
[type] => Reference
)
)
[type] => Reference
)
[3] => Array
(
[id] => 4254
[parentId] => 4250
[name] => Pain Management
[children] => Array
(
[0] => Array
(
[id] => 4271
[parentId] => 4254
[name] => During
[type] => Reference
)
)
[type] => Reference
)
[4] => Array
(
[id] => 4255
[parentId] => 4250
[name] => Communication about Medicines
[children] => Array
(
[0] => Array
(
[id] => 4274
[parentId] => 4255
[name] => During this data stay
[type] => Reference
)
[1] => Array
(
[id] => 4275
[parentId] => 4255
[name] => Before giving you
[type] => Reference
)
[2] => Array
(
[id] => 4276
[parentId] => 4255
[name] => Before giving you
[type] => Reference
)
)
[type] => Reference
)
[5] => Array
(
[id] => 4256
[parentId] => 4250
[name] => Discharge Information
[children] => Array
(
[0] => Array
(
[id] => 4277
[parentId] => 4256
[name] => During this data stay the data?
[type] => Reference
)
[1] => Array
(
[id] => 4278
[parentId] => 4256
[name] => After you left the data
[type] => Reference
)
[2] => Array
(
[id] => 4279
[parentId] => 4256
[name] => During this data stay
[type] => Reference
)
)
[type] => Reference
)
[6] => Array
(
[id] => 4257
[parentId] => 4250
[name] => Care Transition
[children] => Array
(
[0] => Array
(
[id] => 4280
[parentId] => 4257
[name] => During this data stay
[type] => Reference
)
)
[type] => Reference
)
[7] => Array
(
[id] => 4258
[parentId] => 4250
[name] => Cleanliness of data Environment
[children] => Array
(
[0] => Array
(
[id] => 4282
[parentId] => 4258
[name] => During this data?
[type] => Reference
)
)
[type] => Reference
)
[8] => Array
(
[id] => 4259
[parentId] => 4250
[name] => Quietness of data Environment
[children] => Array
(
[0] => Array
(
[id] => 4283
[parentId] => 4259
[name] => During this data?
[type] => Reference
)
)
[type] => Reference
)
[9] => Array
(
[id] => 4260
[parentId] => 4250
[name] => also the data
[children] => Array
(
[0] => Array
(
[id] => 4284
[parentId] => 4260
[name] => Would you?
[type] => Reference
)
)
[type] => Reference
)
[10] => Array
(
[id] => 4261
[parentId] => 4250
[name] => Overall Rating of data
[children] => Array
(
[0] => Array
(
[id] => 4285
[parentId] => 4261
[name] => Using any number
[type] => Reference
)
)
[type] => Reference
)
)
)
)
)
the output shoiuld be something like for eg: The ids should come inside selectbox along with option
Access
Electronics
--Keyboards
--Phones
----HTC DESIRE Z
----HTC ONE X
-------Camera
-------Battety
Food
--Fruits
----Apple
----Watermelon
--Vegetables
----Carrot
----Onion
**inside select box . for eg: <select><option value='4001'>Access</option></select>**
I have so far this much but it displays everything .Still not getting
$json = json_decode($string,TRUE);
//echo "<pre>";
//print_r($json);
recursive($json,$level=1);
function recursive($array, $level = 1){
foreach($array as $value){
//If $value is an array.
if(is_array($value)){
//We need to loop through it.
recursive($value, $level + 1);
} else{
//It is not an array, so print it out.
echo str_repeat("-", $level), $value, '<br>';
}
}
}
The following code will recurse through your data structure and create the list of options. I will leave the creation of the select tag to you, since it is very simple. I've used PHP_EOL to add line breaks after each option to make the HTML code easier to read.
foreach ($data['categories'] as $c) {
# scope is $c['scope']
echo "Starting " . $c['scope'] . PHP_EOL;
# calling the function on the categories data
recurse($c['categories']);
echo "Finished " . $c['scope'] . PHP_EOL;
}
function recurse($arr, $level = 0){
# we have a numerically-indexed array. go through each item:
foreach ($arr as $n) {
# print out the item ID and the item name
echo '<option value="' . $n['id'] . '">'
. str_repeat("-", $level)
. $n['name']
. '</option>'
. PHP_EOL;
# if item['children'] is set, we have a nested data structure, so
# call recurse on it.
if (isset($n['children'])) {
# we have children: RECURSE!!
recurse( $n['children'], $level+1);
}
}
}
Output:
Starting selectboxFirst
<option value="4001">Access</option>
<option value="4010">-mine</option>
<option value="4011">-yours</option>
<option value="4002">Communication</option>
<option value="4015">-Physician</option>
<option value="4016">--Helps blsys Understand</option>
<option value="4017">--Listens</option>
<option value="4021">-Clinical ps</option>
<option value="4022">--yours ps/datasssss</option>
<option value="4024">-etc</option>
<option value="4003">Office</option>
<option value="4026">-Facilities/Environment</option>
<option value="4027">--Noise</option>
(etc.)
You can substitute the appropriate code for "Starting selectboxFirst/selectboxSecond".
I'm hoping someone can help me with my syntax. I think that's the issue. I'm trying to have the loop go through display the chosen object, but it's not working. Here is my code.
stdClass Object
(
[result_count] => 2
[total_count] => 2
[next_offset] => 2
[entry_list] => Array
(
[0] => stdClass Object
(
[id] => da2b0ab5-873b-d9a8-0fcd-5180871e57eb
[module_name] => Project
[name_value_list] => stdClass Object
(
[description] => stdClass Object
(
[name] => description
[value] => soccer is fun!!!
)
[priority] => stdClass Object
(
[name] => priority
[value] => Primary
)
[total_years_played_c] => stdClass Object
(
[name] => total_years_played_c
[value] => 5
)
[sport_c] => stdClass Object
(
[name] => sport_c
[value] => Soccer
)
[high_school_exp_c] => stdClass Object
(
[name] => high_school_exp_c
[value] => 2
)
[varsity_exp_c] => stdClass Object
(
[name] => varsity_exp_c
[value] => default
)
[hand_right_c] => stdClass Object
(
[name] => hand_right_c
[value] => 0
)
[hand_left_c] => stdClass Object
(
[name] => hand_left_c
[value] => 1
)
[foot_right_c] => stdClass Object
(
[name] => foot_right_c
[value] => 1
)
[foot_left_c] => stdClass Object
(
[name] => foot_left_c
[value] => 0
)
[specialskills_c] => stdClass Object
(
[name] => specialskills_c
[value] =>
)
[varsity_starter_c] => stdClass Object
(
[name] => varsity_starter_c
[value] => 0
)
)
)
[1] => stdClass Object
(
[id] => 2e805552-c887-2b2e-a9e5-518087f3f87a
[module_name] => Project
[name_value_list] => stdClass Object
(
[description] => stdClass Object
(
[name] => description
[value] => I bleed for hockey... I will teach your kid to put them on the boards and take no prisoners!
)
[priority] => stdClass Object
(
[name] => priority
[value] => Secondary
)
[total_years_played_c] => stdClass Object
(
[name] => total_years_played_c
[value] => 7
)
[sport_c] => stdClass Object
(
[name] => sport_c
[value] => Hockey
)
[high_school_exp_c] => stdClass Object
(
[name] => high_school_exp_c
[value] => 3
)
[varsity_exp_c] => stdClass Object
(
[name] => varsity_exp_c
[value] => 2
)
[hand_right_c] => stdClass Object
(
[name] => hand_right_c
[value] => 0
)
[hand_left_c] => stdClass Object
(
[name] => hand_left_c
[value] => 1
)
[foot_right_c] => stdClass Object
(
[name] => foot_right_c
[value] => 1
)
[foot_left_c] => stdClass Object
(
[name] => foot_left_c
[value] => 0
)
[specialskills_c] => stdClass Object
(
[name] => specialskills_c
[value] =>
)
[varsity_starter_c] => stdClass Object
(
[name] => varsity_starter_c
[value] => 0
)
)
)
)
[relationship_list] => Array
(
)
)
Here is the PHP that I am using. For now, I'm just wanting to echo the name of sport_c (one should be Hockey, the other should be Soccer)
<?
foreach($response->entry_list as $sports) {
if(!empty($sports->name_value_list->sport_c)) {
$sport_list = $sports->name_value_list->sport_c;
$sport_name = $sport_list->value;
}
}
echo $sport_name;
?>
Put the echo statement within the foreach loop:
foreach($response->entry_list as $sports) {
if(!empty($sports->name_value_list->sport_c)) {
$sport_list = $sports->name_value_list->sport_c;
$sport_name = $sport_list->value;
}
echo $sport_name;
}