Print data retrieved in JSON on a PHP - php

I'm trying to print a table with the list of Monitoring Groups from data retrieved from a REST API. The results come back in JSON. Below is the JSON data:
{
"code": 0,
"message": "success",
"data":
[
{
"group_id": "169839000000116001",
"display_name": "MTI Servers",
"description": "",
"monitors":
[
]
},
{
"group_id": "169839000000180001",
"display_name": "PRB Servers",
"description": "",
"monitors":
[
"169839000000179003",
"169839000000176013",
"169839000000175003",
"169839000000176007"
]
},
{
"group_id": "169839000000046270",
"display_name": "DB Servers",
"description": "",
"monitors":
[
"169839000000051011",
"169839000000047023",
"169839000000078001"
]
},
{
"group_id": "169839000000025200",
"display_name": "EXT Apps",
"description": "External Monitoring of Applications",
"monitors":
[
"169839000000025274",
"169839000000025377",
"169839000000025359",
"169839000000025369",
"169839000000025385",
"169839000000025226"
]
},
{
"group_id": "169839000000025109",
"display_name": "EXT Services",
"description": "External monitoring of services.",
"monitors":
[
"169839000000046165",
"169839000000025256",
"169839000000025168",
"169839000000025202",
"169839000000025189",
"169839000000025217",
"169839000000025265"
]
},
{
"group_id": "169839000000046015",
"display_name": "ZMB Servers",
"description": "",
"monitors":
[
"169839000000050017",
"169839000000050025",
"169839000000049001",
"169839000000050001",
"169839000000053019",
"169839000000051003",
"169839000000050009"
]
},
{
"group_id": "169839000000046282",
"display_name": "NWK Devices",
"description": "",
"monitors":
[
"169839000000082009",
"169839000000084077",
"169839000000084001",
"169839000000082229"
]
},
{
"group_id": "169839000000046013",
"display_name": "VBR Servers",
"description": "",
"monitors":
[
"169839000000047007"
]
},
{
"group_id": "169839000000054197",
"display_name": "LNX Servers",
"description": "",
"monitors":
[
"169839000000060483"
]
},
{
"group_id": "169839000000046020",
"display_name": "VSP Servers",
"description": "",
"monitors":
[
"169839000000060177",
"169839000000060170",
"169839000000060088",
"169839000000060095",
"169839000000060102",
"169839000000060109",
"169839000000054102"
]
},
{
"group_id": "169839000000046058",
"display_name": "WND Servers",
"description": "",
"monitors":
[
"169839000000066001",
"169839000000063119"
]
},
{
"group_id": "169839000000128001",
"display_name": "TPT Servers",
"description": "",
"monitors":
[
"169839000000143041",
"169839000000148017",
"169839000000127035",
"169839000000123003",
"169839000000126011",
"169839000000122011",
"169839000000129001",
"169839000000158028"
]
}
]
}
I want to print the Group ID, Name, Description and the list of monitor IDs that belong to the group. I'm using json_decode but I cant seem to read the correct variables from the array.
Here's the code I have so far:
`<?php
/**
* Connect to the Site API and extract the list of monitor groups
*/
// URL to fetch
$url = "https://www.mymonitorsite.com/api/monitor_groups";
// Initialize cURL session
$ch = curl_init($url);
// Option to Return the Result, rather than just true/false
curl_setopt($ch, CURLOPT_RETURNTRANSFER, true);
// Set Custom Headers
$headers = array(
'Authorization: Authtoken 12345678901234567890123456789012',
'Content-Type: application/json;charset=UTF-8',
'Accept: application/json; version=2.0',
);
// Option to set the custom headers
curl_setopt($ch, CURLOPT_HTTPHEADER, $headers);
// Perform the request, and save content to $result
$mongrps_json = curl_exec($ch);
// Close the cURL resource, and free up system resources!
curl_close($ch);
// Decode json data into a PHP Array
$mongrps_array = json_decode($mongrps_json, true);
?>
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="utf-8"/>
<title>Monitor Group Test</title>
</head>
<body>
<?php
// List the first monitor group here (test)
echo "Group ID = " . $mongrps_array->data[0]->group_id . "<br>";
echo "Group Name = " . $mongrps_array->data[0]->display_name . <br>";
?>
</body>
</html>
`
Here's a var_dump of the json_decode:
array(3) { ["code"]=> int(0) ["message"]=> string(7) "success" ["data"]=> array(12) { [0]=> array(4) { ["group_id"]=> string(18) "169839000000116001" ["display_name"]=> string(11) "MTI Servers" ["description"]=> string(0) "" ["monitors"]=> array(0) { } } [1]=> array(4) { ["group_id"]=> string(18) "169839000000180001" ["display_name"]=> string(11) "PRB Servers" ["description"]=> string(0) "" ["monitors"]=> array(4) { [0]=> string(18) "169839000000179003" [1]=> string(18) "169839000000176013" [2]=> string(18) "169839000000175003" [3]=> string(18) "169839000000176007" } } [2]=> array(4) { ["group_id"]=> string(18) "169839000000046270" ["display_name"]=> string(10) "DB Servers" ["description"]=> string(0) "" ["monitors"]=> array(3) { [0]=> string(18) "169839000000051011" [1]=> string(18) "169839000000047023" [2]=> string(18) "169839000000078001" } } [3]=> array(4) { ["group_id"]=> string(18) "169839000000025200" ["display_name"]=> string(8) "EXT Apps" ["description"]=> string(35) "External Monitoring of Applications" ["monitors"]=> array(6) { [0]=> string(18) "169839000000025274" [1]=> string(18) "169839000000025377" [2]=> string(18) "169839000000025359" [3]=> string(18) "169839000000025369" [4]=> string(18) "169839000000025385" [5]=> string(18) "169839000000025226" } } [4]=> array(4) { ["group_id"]=> string(18) "169839000000025109" ["display_name"]=> string(12) "EXT Services" ["description"]=> string(31) "External monitoring of services" ["monitors"]=> array(7) { [0]=> string(18) "169839000000046165" [1]=> string(18) "169839000000025256" [2]=> string(18) "169839000000025168" [3]=> string(18) "169839000000025202" [4]=> string(18) "169839000000025189" [5]=> string(18) "169839000000025217" [6]=> string(18) "169839000000025265" } } [5]=> array(4) { ["group_id"]=> string(18) "169839000000046015" ["display_name"]=> string(11) "ZMB Servers" ["description"]=> string(0) "" ["monitors"]=> array(7) { [0]=> string(18) "169839000000050017" [1]=> string(18) "169839000000050025" [2]=> string(18) "169839000000049001" [3]=> string(18) "169839000000050001" [4]=> string(18) "169839000000053019" [5]=> string(18) "169839000000051003" [6]=> string(18) "169839000000050009" } } [6]=> array(4) { ["group_id"]=> string(18) "169839000000046282" ["display_name"]=> string(11) "NWK Devices" ["description"]=> string(0) "" ["monitors"]=> array(4) { [0]=> string(18) "169839000000082009" [1]=> string(18) "169839000000084077" [2]=> string(18) "169839000000084001" [3]=> string(18) "169839000000082229" } } [7]=> array(4) { ["group_id"]=> string(18) "169839000000046013" ["display_name"]=> string(11) "VBR Servers" ["description"]=> string(0) "" ["monitors"]=> array(1) { [0]=> string(18) "169839000000047007" } } [8]=> array(4) { ["group_id"]=> string(18) "169839000000054197" ["display_name"]=> string(11) "LNX Servers" ["description"]=> string(0) "" ["monitors"]=> array(1) { [0]=> string(18) "169839000000060483" } } [9]=> array(4) { ["group_id"]=> string(18) "169839000000046020" ["display_name"]=> string(11) "VSP Servers" ["description"]=> string(0) "" ["monitors"]=> array(7) { [0]=> string(18) "169839000000060177" [1]=> string(18) "169839000000060170" [2]=> string(18) "169839000000060088" [3]=> string(18) "169839000000060095" [4]=> string(18) "169839000000060102" [5]=> string(18) "169839000000060109" [6]=> string(18) "169839000000054102" } } [10]=> array(4) { ["group_id"]=> string(18) "169839000000046058" ["display_name"]=> string(11) "WND Servers" ["description"]=> string(0) "" ["monitors"]=> array(2) { [0]=> string(18) "169839000000066001" [1]=> string(18) "169839000000063119" } } [11]=> array(4) { ["group_id"]=> string(18) "169839000000128001" ["display_name"]=> string(11) "TPT Servers" ["description"]=> string(0) "" ["monitors"]=> array(8) { [0]=> string(18) "169839000000143041" [1]=> string(18) "169839000000148017" [2]=> string(18) "169839000000127035" [3]=> string(18) "169839000000123003" [4]=> string(18) "169839000000126011" [5]=> string(18) "169839000000122011" [6]=> string(18) "169839000000129001" [7]=> string(18) "169839000000158028" } } } }

Use foreach loop to display the following data:
group_id
display_name
description and
monitors(as a comma separated list)
$mongrps_array = json_decode($mongrps_json, true);
foreach($mongrps_array['data'] as $arr){
foreach($arr as $key => $value){
if($key == "monitors"){
echo $key . ": " . implode(", ", $value) . "<br />";
}else{
echo $key . ": " . $value . "<br />";
}
}
echo "<br />";
}

I Just tried, should not be any issue to get the data you are looking for.
For example, to get the group_id of the first item in the array:
echo $decodedJson->data[0]->group_id;
Maybe you are trying to get the data as arrays? In this case, pass the parameter true to the json_decode function:
$decodedJson = json_decode($originalJson, true);
echo $decodedJson['data'][0]['group_id'];

Related

Group array items by multiple values and sum the groups [duplicate]

This question already has answers here:
Group multidimensional array data based on two column values and sum values of one column in each group
(5 answers)
Closed 7 months ago.
I have 2D array (showed bellow) - output of wp_query (Wordpress).
I need to group the arrays by index 0, 1, 2 values (lets think that the primary key is composed from these values and it should be unique in result table) and sum the index 3 values.
array(4) {
[0]=> string(6) "Team 1"
[1]=> string(6) "Jack"
[2]=> string(6) "Daniels"
[3]=> string(2) "10"
}
array(4) {
[0]=> string(6) "Team 1"
[1]=> string(3) "Jan"
[2]=> string(6) "Novak"
[3]=> string(2) "33"
}
array(4) {
[0]=> string(6) "Team 2"
[1]=> string(4) "John"
[2]=> string(3) "Doe"
[3]=> string(2) "11"
}
array(4) {
[0]=> string(6) "Team 2"
[1]=> string(4) "Jane"
[2]=> string(3) "Doe"
[3]=> string(2) "18"
}
array(4) {
[0]=> string(6) "Team 1"
[1]=> string(6) "Jack"
[2]=> string(6) "Daniels"
[3]=> string(1) "5"
}
array(4) {
[0]=> string(6) "Team 1"
[1]=> string(3) "Jan"
[2]=> string(6) "Novak"
[3]=> string(2) "33"
}
array(4) {
[0]=> string(6) "Team 2"
[1]=> string(4) "John"
[2]=> string(3) "Doe"
[3]=> string(2) "11"
}
array(4) {
[0]=> string(6) "Team 2"
[1]=> string(4) "Jane"
[2]=> string(3) "Doe"
[3]=> string(2) "18"
}
ETC...
Output should be someting like:
array(4) {
[0]=> string(6) "Team 1"
[1]=> string(6) "Jack"
[2]=> string(6) "Daniels"
[3]=> string(2) "15" (5+10)
}
array(4) {
[0]=> string(6) "Team 1"
[1]=> string(3) "Jan"
[2]=> string(6) "Novak"
[3]=> string(2) "66" (33+33)
}
array(4) {
[0]=> string(6) "Team 2"
[1]=> string(4) "John"
[2]=> string(3) "Doe"
[3]=> string(2) "22" (11+11)
}
array(4) {
[0]=> string(6) "Team 2"
[1]=> string(4) "Jane"
[2]=> string(3) "Doe"
[3]=> string(2) "36" (18+18)
}
}
I tried to do this by recursive for loops, but I cannot find the reason why is it not working properly.
$vysledna_tabulka = array();
$aktualni;
foreach($seznam_vsech_hracu_vsech_tymu as $radek){
if(empty($vysledna_tabulka)){
array_push($vysledna_tabulka,$radek);
} else{
foreach($vysledna_tabulka as $vysledny_radek){
if($vysledny_radek[0]==$radek[0] && $vysledny_radek[1]==$radek[1] && $vysledny_radek[2]==$radek[2]){
$vysledny_radek[3]+=$radek[3];
} else {
$aktualni = $radek;
}
}
array_push($vysledna_tabulka,$aktualni);
}
}
I think there should be a better way to do this. Is there anyone who could help me?
Thanks.
I would create a key from the first three values of your array elements and then create a new array using this key. Like this:
<?php
$teamArray = [
[
"Team 1",
"Jack",
"Daniels",
"10",
],
[
"Team 1",
"Jan",
"Novak",
"33",
],
[
"Team 2",
"John",
"Doe",
"11",
],
[
"Team 2",
"Jane",
"Doe",
"18",
],
[
"Team 1",
"Jack",
"Daniels",
"5",
],
[
"Team 1",
"Jan",
"Novak",
"33",
],
[
"Team 2",
"John",
"Doe",
"11",
],
[
"Team 2",
"Jane",
"Doe",
"18",
]
];
$result = [];
foreach ($teamArray as $team) {
$key = $team[0] . $team[1] . $team[2];
if (isset($result[$key])) {
$result[$key][3] += $team[3];
}
else {
$result[$key] = $team;
}
}
var_dump($result);
var_dump(array_values($result));
This gives you:
array(4) {
["Team 1JackDaniels"]=>
array(4) {
[0]=>
string(6) "Team 1"
[1]=>
string(4) "Jack"
[2]=>
string(7) "Daniels"
[3]=>
int(15)
}
["Team 1JanNovak"]=>
array(4) {
[0]=>
string(6) "Team 1"
[1]=>
string(3) "Jan"
[2]=>
string(5) "Novak"
[3]=>
int(66)
}
["Team 2JohnDoe"]=>
array(4) {
[0]=>
string(6) "Team 2"
[1]=>
string(4) "John"
[2]=>
string(3) "Doe"
[3]=>
int(22)
}
["Team 2JaneDoe"]=>
array(4) {
[0]=>
string(6) "Team 2"
[1]=>
string(4) "Jane"
[2]=>
string(3) "Doe"
[3]=>
int(36)
}
}
array(4) {
[0]=>
array(4) {
[0]=>
string(6) "Team 1"
[1]=>
string(4) "Jack"
[2]=>
string(7) "Daniels"
[3]=>
int(15)
}
[1]=>
array(4) {
[0]=>
string(6) "Team 1"
[1]=>
string(3) "Jan"
[2]=>
string(5) "Novak"
[3]=>
int(66)
}
[2]=>
array(4) {
[0]=>
string(6) "Team 2"
[1]=>
string(4) "John"
[2]=>
string(3) "Doe"
[3]=>
int(22)
}
[3]=>
array(4) {
[0]=>
string(6) "Team 2"
[1]=>
string(4) "Jane"
[2]=>
string(3) "Doe"
[3]=>
int(36)
}
}
If you need to remove the keys from your $result array you could use array_value as shown above.

PHP Convert multidimensional array key to be array multidimensional value

I have problem formatting array to become fancytree format, below array output from db:
array(44) {
[0]=>
array(3) {
["id"]=>
string(2) "35"
["title"]=>
string(28) "ROOT1"
["path"]=>
string(28) "ROOT1"
}
[1]=>
array(3) {
["id"]=>
string(2) "36"
["title"]=>
string(27) "SUB_A"
["path"]=>
string(56) "ROOT1>SUB_A"
}
[2]=>
array(3) {
["id"]=>
string(2) "39"
["title"]=>
string(12) "SUB_A_1"
["path"]=>
string(69) "ROOT1>SUB_A>SUB_A_1"
}
[3]=>
array(3) {
["id"]=>
string(2) "37"
["title"]=>
string(28) "SUB_A_2"
["path"]=>
string(85) "ROOT1>SUB_A>SUB_A_2"
}
[4]=>
array(3) {
["id"]=>
string(2) "40"
["title"]=>
string(30) "SUB_A_3"
["path"]=>
string(87) "ROOT1>SUB_A>SUB_A_3"
}
[5]=>
array(3) {
["id"]=>
string(2) "43"
["title"]=>
string(19) "SUB_A_4"
["path"]=>
string(76) "ROOT1>SUB_A>SUB_A_4"
}
[6]=>
array(3) {
["id"]=>
string(2) "41"
["title"]=>
string(12) "SUB_A_5"
["path"]=>
string(69) "ROOT1>SUB_A>SUB_A_5"
}
[7]=>
array(3) {
["id"]=>
string(1) "1"
["title"]=>
string(20) "ROOT2"
["path"]=>
string(20) "ROOT2"
}
[8]=>
array(3) {
["id"]=>
string(2) "30"
["title"]=>
string(37) "ROOT3"
["path"]=>
string(37) "ROOT3"
}
[9]=>
array(3) {
["id"]=>
string(2) "34"
["title"]=>
string(21) "SUB_B"
["path"]=>
string(59) "ROOT3>SUB_B"
}
[10]=>
array(3) {
["id"]=>
string(2) "31"
["title"]=>
string(15) "SUB_C"
["path"]=>
string(53) "ROOT3>SUB_C"
}
I want to format values of path become structured array, here is what I try:
function format(array $data) : array
{
$single = [];
// format to single data
foreach ($data as $value) {
$single[] = $value['path'];
}
$result = [];
foreach ($single as $path) {
$parts = explode('>', $path);
$section = &$result;
$sectionName = '';
foreach ($parts as $part) {
$sectionName = $part;
if (array_key_exists($sectionName, $section) === FALSE) {
$section[$sectionName] = [];
}
$section = &$section[$sectionName];
}
}
return $result;
}
result:
array(3) {
["ROOT1"]=>
array(1) {
["SUB_A"]=>
array(5) {
["SUB_A_1"]=>
array(0) {
}
["SUB_A_2"]=>
array(0) {
}
["SUB_A_3"]=>
array(0) {
}
["SUB_A_4"]=>
array(0) {
}
["SUB_A_5"]=>
array(0) {
}
}
}
["ROOT2"]=>
array(0) {
}
["ROOT3"]=>
array(2) {
["SUB_B"]=>
array(0) {
}
["SUB_C"]=>
array(0) {
}
}
i need to include values of id and title to each path as expected result:
array(3) {
[0]=>
array(3) {
["title"]=>
string(5) "ROOT1"
["id"]=>
int(35)
["children"]=>
array(1) {
[0]=>
array(3) {
["title"]=>
string(5) "SUB_A"
["id"]=>
int(36)
["children"]=>
array(5) {
[0]=>
array(2) {
["title"]=>
string(7) "SUB_A_1"
["id"]=>
int(39)
}
[1]=>
array(2) {
["title"]=>
string(7) "SUB_A_2"
["id"]=>
int(37)
}
[2]=>
array(2) {
["title"]=>
string(7) "SUB_A_3"
["id"]=>
int(40)
}
[3]=>
array(2) {
["title"]=>
string(7) "SUB_A_4"
["id"]=>
int(43)
}
[4]=>
array(2) {
["title"]=>
string(7) "SUB_A_5"
["id"]=>
int(41)
}
}
}
}
}
[1]=>
array(2) {
["title"]=>
string(5) "ROOT2"
["id"]=>
int(1)
}
[2]=>
array(3) {
["title"]=>
string(5) "ROOT3"
["id"]=>
int(30)
["children"]=>
array(2) {
[0]=>
array(2) {
["title"]=>
string(5) "SUB_B"
["id"]=>
int(34)
}
[1]=>
array(2) {
["title"]=>
string(5) "SUB_C"
["id"]=>
int(31)
}
}
}
}
I would be grateful for any help you are able to provide.
Here's how I would do it. Test
I used arrow functions to reduce the amount of lines and there are inline comments to explain what I did.
<?php
$data = [["id"=> "35", "title"=> "ROOT1", "path"=> "ROOT1"], ["id"=> "36", "title"=> "SUB_A", "path"=> "ROOT1>SUB_A"], ["id"=> "37", "title"=> "SUB_A_1", "path"=> "ROOT1>SUB_A>SUB_A_1"], ["id"=> "38", "title"=> "SUB_A_2", "path"=> "ROOT1>SUB_A>SUB_A_2"], ["id"=> "39", "title"=> "SUB_A_3", "path"=> "ROOT1>SUB_A>SUB_A_3"], ["id"=> "40", "title"=> "SUB_A_4", "path"=> "ROOT1>SUB_A>SUB_A_4"], ["id"=> "41", "title"=> "SUB_A_5", "path"=> "ROOT1>SUB_A>SUB_A_5"], ["id"=> "1", "title"=> "ROOT2", "path"=> "ROOT2"], ["id"=> "30", "title"=> "ROOT3", "path"=> "ROOT3"], ["id"=> "34", "title"=> "SUB_B", "path"=> "ROOT3>SUB_B"], ["id"=> "31", "title"=> "SUB_C", "path"=> "ROOT3>SUB_C"]];
// The path is defaulted to an empty string, so the regex would return the highest level elements only because they don't have any `>`
function get_children($data, $path = '')
{
$results = array_values(array_filter($data, fn($item) => preg_match("/^{$path}[^>]+$/", $item['path'])));
// This regex, uses the passed path (e.g ROOT1) and searches for elements with anything but another `>`, because `>` signifies an inner directory.
// So this would return elements with a path like `ROOT1>SUB_A, ROOT1>SUB_B` but not `ROOT1>SUB_A>SUB_A_1`
foreach($results as $i => $result) {
// then I iterate through the result to get the element's children
$children = array_values(get_children($data, "{$result['path']}>"));
if (count($children)) $results[$i]['children'] = $children;
unset($results[$i]['path']); // Removing the path key from the array
}
return $results;
}
$format = fn($data) => get_children($data);
print_r($format($data));

How to get the count of each field inside a group tab object

This is my object and I want to get the number of fields in each tab. I'm thinking of skipping the field if its type is a Tab then start counting the fields before the next Type of tab..
Anyway here is my object below:
array(49) {
[0]=>
array(12) {
["key"]=>
string(19) "field_596796ae0c4c4"
["label"]=>
string(7) "GENERAL"
["name"]=>
string(0) ""
["_name"]=>
string(0) ""
["type"]=>
string(3) "tab"
["order_no"]=>
int(0)
["instructions"]=>
string(0) ""
["required"]=>
int(0)
["id"]=>
string(10) "acf-field-"
["class"]=>
string(3) "tab"
["conditional_logic"]=>
array(3) {
["status"]=>
int(0)
["rules"]=>
array(1) {
[0]=>
array(3) {
["field"]=>
string(19) "field_5964e9dd6df85"
["operator"]=>
string(2) "=="
["value"]=>
string(0) ""
}
}
["allorany"]=>
string(3) "all"
}
["field_group"]=>
int(165)
}
[1]=>
array(18) {
["key"]=>
string(19) "field_5964e9206df82"
["label"]=>
string(5) "Brand"
["name"]=>
string(5) "brand"
["_name"]=>
string(5) "brand"
["type"]=>
string(8) "taxonomy"
["order_no"]=>
int(1)
["instructions"]=>
string(0) ""
["required"]=>
int(1)
["id"]=>
string(15) "acf-field-brand"
["class"]=>
string(8) "taxonomy"
["conditional_logic"]=>
array(3) {
["status"]=>
int(0)
["rules"]=>
array(1) {
[0]=>
array(3) {
["field"]=>
string(19) "field_5964e9dd6df85"
["operator"]=>
string(2) "=="
["value"]=>
string(7) "regular"
}
}
["allorany"]=>
string(3) "all"
}
["taxonomy"]=>
string(8) "pa_brand"
["field_type"]=>
string(6) "select"
["allow_null"]=>
int(0)
["load_save_terms"]=>
int(0)
["return_format"]=>
string(2) "id"
["field_group"]=>
int(165)
["multiple"]=>
int(0)
}
[2]=>
array(12) {
["key"]=>
string(19) "field_596796da0c4c5"
["label"]=>
string(6) "LAUNCH"
["name"]=>
string(0) ""
["_name"]=>
string(0) ""
["type"]=>
string(3) "tab"
["order_no"]=>
int(2)
["instructions"]=>
string(0) ""
["required"]=>
int(0)
["id"]=>
string(10) "acf-field-"
["class"]=>
string(3) "tab"
["conditional_logic"]=>
array(3) {
["status"]=>
int(0)
["rules"]=>
array(1) {
[0]=>
array(3) {
["field"]=>
string(19) "field_5964e9dd6df85"
["operator"]=>
string(2) "=="
["value"]=>
string(0) ""
}
}
["allorany"]=>
string(3) "all"
}
["field_group"]=>
int(165)
}
[3]=>
array(15) {
["key"]=>
string(19) "field_5964e99e6df84"
["label"]=>
string(20) "Announced (Globally)"
["name"]=>
string(18) "announced_globally"
["_name"]=>
string(18) "announced_globally"
["type"]=>
string(11) "date_picker"
["order_no"]=>
int(3)
["instructions"]=>
string(23) "Date globally announced"
["required"]=>
int(0)
["id"]=>
string(28) "acf-field-announced_globally"
["class"]=>
string(11) "date_picker"
["conditional_logic"]=>
array(3) {
["status"]=>
int(0)
["rules"]=>
array(1) {
[0]=>
array(3) {
["field"]=>
string(19) "field_5964e9dd6df85"
["operator"]=>
string(2) "=="
["value"]=>
string(7) "regular"
}
}
["allorany"]=>
string(3) "all"
}
["date_format"]=>
string(4) "mmyy"
["display_format"]=>
string(5) "MM yy"
["first_day"]=>
int(1)
["field_group"]=>
int(165)
}
or this object that I simplified and run in Javascript:
var productObject = [
{
"type":"tab",
"value":"GENERAL"
},
{
"type":"taxonomy",
"value":"56",
"label":"Brand"
},
{
"type":"tab",
"value":"LAUNCH"
},
{
"type":"text",
"name":"announced_ph",
"label":"Announced(Philippines)",
"value":"07072017"
},{
"type":"text",
"name":"announced_global",
"label":"Announced(Global)",
"value":"09092017"
},{
"type":"tab",
"value":"NETWORK"
},{
"type":"text",
"name":"sim_type",
"label":"SIM",
"value":"dualnano"
},{
"type":"text",
"name":"broadband_speed",
"label":"Broadband Speed",
"value":"LTE Cat6"
},{
"type":"text",
"name":"broadband_speed",
"label":"Broadband Speed",
"value":"LTE Cat6"
}
];
I need to know the number of fields/objects after an object with a type of "tab". Because I need to know it for the rowspan value of my table.
The ouput I'm thinking should be:
Tab "General" has 1 field
Tab "Launch" has 2 field
Tab "Network" has 3 field
Thanks!
Loop over the object, then just check if the type is equal to tab.
Something like if (productObject.type == 'tab').
If it is equal to tab, set a counter variable to 0, then just increment it until you reach the next tab value.
my_counter = 0
tab_type = ''
for po in productObject:
if po['type'] == 'tab':
my_counter = 0
tab_type = po['value']
my_counter += 1
Something like this.
Using the productObject, I'll based it on the key value
//Getting all "value" keys
var arr = $.map(productObject,function(k,v){
if(k.type == "tab"){
return k.value;
}
});
//Counting each duplicates
var map = arr.reduce(function(prev, cur) {
prev[cur] = (prev[cur] || 0) + 1;
return prev;
}, {});
//Returns JSON
console.log(JSON.stringify(map));
Sample Output:
{"GENERAL":1,"LAUNCH":1,"NETWORK":1}

Change structure of array to create JSON

I would like to change structure of my array to produce slightly different JSON file. I tried putting 'category' into extra array but it doesn't work.
In JSON 'category' should be in aditional []:
"categories": [{
"category": [{
"label": "N.12\/02"
}, {
"label": "Pn.13\/02"
}, {
"label": "Wt.14\/02"
}, {
"label": "\u015ar.15\/02"
}, {
"label": "Cz.16\/02"
}, {
"label": "Pt.17\/02"
}, {
"label": "So.18\/02"
}]
}],
instead of:
"categories": {
"category": [{
"label": "N.12\/02"
}, {
"label": "Pn.13\/02"
}, {
"label": "Wt.14\/02"
}, {
"label": "\u015ar.15\/02"
}, {
"label": "Cz.16\/02"
}, {
"label": "Pt.17\/02"
}, {
"label": "So.18\/02"
}]
},
I am using this code to create array and than JSON:
$arrData = array(
"chart" => array(
"caption" => "Number of visitors last week",
"subCaption" => "Bakersfield Central vs Los Angeles Topanga",
"captionFontSize" => "14",
"subcaptionFontSize" => "14",
"subcaptionFontBold" => "0",
"paletteColors" => "#0075c2,#1aaf5d,#FF5733,#33B5FF",
"bgcolor" => "#ffffff",
"showBorder" => "0",
"showShadow" => "0",
"showCanvasBorder" => "0",
"usePlotGradientColor" => "0",
"legendBorderAlpha" => "0",
"legendShadow" => "0",
"showAxisLines" => "0",
"showAlternateHGridColor" => "0",
"divlineThickness" => "1",
"divLineDashed" => "1",
"divLineDashLen" => "1",
"xAxisName" => "Day",
"showValues" => "0"
)
);
$actualCategory = array( $rowData[0][1], $rowData[0][2], $rowData[0][3], $rowData[0][4], $rowData[0][5], $rowData[0][6], $rowData[0][7]);
$a = array(
array( $rowData[2][1], $rowData[2][2], $rowData[2][3], $rowData[2][4], $rowData[2][5], $rowData[2][6], $rowData[2][7]),
array( $rowData[5][1], $rowData[5][2], $rowData[5][3], $rowData[5][4], $rowData[5][5], $rowData[5][6], $rowData[5][7]),
array( $rowData[8][1], $rowData[8][2], $rowData[8][3], $rowData[8][4], $rowData[8][5], $rowData[8][6], $rowData[8][7]),
array( $rowData[11][1], $rowData[11][2], $rowData[11][3], $rowData[11][4], $rowData[11][5], $rowData[11][6], $rowData[11][7])
);
$arrData['categories'] = array( 'category' => array());
foreach ($actualCategory as $value) {
array_push($arrData['categories']['category'],
array(
'label' => $value
)
);
}
$arrData['dataset'] = array();
$q = 1;
for($z = 0;$z <= 3; $z++){
$x[$z] = array('seriesname' => $rowData[$q][0], 'data' => array());
foreach ($a[$z] as $value) {
array_push($x[$z]['data'],
array(
'value' => $value
)
);
}
$q = $q + 3;
}
$arrData['dataset'] = $x;
$jsonEncodedData = json_encode($arrData);
At this moment I have this output:
{"chart":{"caption":"Number of visitors last week","subCaption":"Bakersfield Central vs Los Angeles Topanga","captionFontSize":"14","subcaptionFontSize":"14","subcaptionFontBold":"0","paletteColors":"#0075c2,#1aaf5d,#FF5733,#33B5FF","bgcolor":"#ffffff","showBorder":"0","showShadow":"0","showCanvasBorder":"0","usePlotGradientColor":"0","legendBorderAlpha":"0","legendShadow":"0","showAxisLines":"0","showAlternateHGridColor":"0","divlineThickness":"1","divLineDashed":"1","divLineDashLen":"1","xAxisName":"Day","showValues":"0"},"categories":{"category":[{"label":"N.12\/02"},{"label":"Pn.13\/02"},{"label":"Wt.14\/02"},{"label":"\u015ar.15\/02"},{"label":"Cz.16\/02"},{"label":"Pt.17\/02"},{"label":"So.18\/02"}]},"dataset":[{"seriesname":"IRDN","data":[{"value":"142.59"},{"value":"174.88"},{"value":"176.97"},{"value":"182.48"},{"value":"160.15"},{"value":"160.72"},{"value":"165.47"}]},{"seriesname":"SIRDN","data":[{"value":"148.81"},{"value":"197.29"},{"value":"202.27"},{"value":"211.93"},{"value":"177.87"},{"value":"179.37"},{"value":"177.69"}]},{"seriesname":"IRDN24","data":[{"value":"140.31"},{"value":"174.50"},{"value":"180.38"},{"value":"187.70"},{"value":"161.91"},{"value":"161.62"},{"value":"160.98"}]},{"seriesname":"IRDN 8.22","data":[{"value":"147.33"},{"value":"197.02"},{"value":"202.21"},{"value":"211.28"},{"value":"178.11"},{"value":"179.32"},{"value":"176.31"}]}]}
JSON file I need:
{"chart":{"caption":"Number of visitors last week","subCaption":"Bakersfield Central vs Los Angeles Topanga","captionFontSize":"14","subcaptionFontSize":"14","subcaptionFontBold":"0","paletteColors":"#0075c2,#1aaf5d,#FF5733,#33B5FF","bgcolor":"#ffffff","showBorder":"0","showShadow":"0","showCanvasBorder":"0","usePlotGradientColor":"0","legendBorderAlpha":"0","legendShadow":"0","showAxisLines":"0","showAlternateHGridColor":"0","divlineThickness":"1","divLineDashed":"1","divLineDashLen":"1","xAxisName":"Day","showValues":"0"},"categories":[{"category":[{"label":"N.12\/02"},{"label":"Pn.13\/02"},{"label":"Wt.14\/02"},{"label":"\u015ar.15\/02"},{"label":"Cz.16\/02"},{"label":"Pt.17\/02"},{"label":"So.18\/02"}]}],"dataset":[{"seriesname":"IRDN","data":[{"value":"142.59"},{"value":"174.88"},{"value":"176.97"},{"value":"182.48"},{"value":"160.15"},{"value":"160.72"},{"value":"165.47"}]},{"seriesname":"SIRDN","data":[{"value":"148.81"},{"value":"197.29"},{"value":"202.27"},{"value":"211.93"},{"value":"177.87"},{"value":"179.37"},{"value":"177.69"}]},{"seriesname":"IRDN24","data":[{"value":"140.31"},{"value":"174.50"},{"value":"180.38"},{"value":"187.70"},{"value":"161.91"},{"value":"161.62"},{"value":"160.98"}]},{"seriesname":"IRDN 8.22","data":[{"value":"147.33"},{"value":"197.02"},{"value":"202.21"},{"value":"211.28"},{"value":"178.11"},{"value":"179.32"},{"value":"176.31"}]}]}
Dump of my array:
array(3) { ["chart"]=> array(20) { ["caption"]=> string(28) "Number of visitors last week" ["subCaption"]=> string(42) "Bakersfield Central vs Los Angeles Topanga" ["captionFontSize"]=> string(2) "14" ["subcaptionFontSize"]=> string(2) "14" ["subcaptionFontBold"]=> string(1) "0" ["paletteColors"]=> string(31) "#0075c2,#1aaf5d,#FF5733,#33B5FF" ["bgcolor"]=> string(7) "#ffffff" ["showBorder"]=> string(1) "0" ["showShadow"]=> string(1) "0" ["showCanvasBorder"]=> string(1) "0" ["usePlotGradientColor"]=> string(1) "0" ["legendBorderAlpha"]=> string(1) "0" ["legendShadow"]=> string(1) "0" ["showAxisLines"]=> string(1) "0" ["showAlternateHGridColor"]=> string(1) "0" ["divlineThickness"]=> string(1) "1" ["divLineDashed"]=> string(1) "1" ["divLineDashLen"]=> string(1) "1" ["xAxisName"]=> string(3) "Day" ["showValues"]=> string(1) "0" } ["categories"]=> array(1) { ["category"]=> array(7) { [0]=> array(1) { ["label"]=> string(80) " N. 12/02 " } [1]=> array(1) { ["label"]=> string(81) " Pn. 13/02 " } [2]=> array(1) { ["label"]=> string(81) " Wt. 14/02 " } [3]=> array(1) { ["label"]=> string(82) " Śr. 15/02 " } [4]=> array(1) { ["label"]=> string(81) " Cz. 16/02 " } [5]=> array(1) { ["label"]=> string(81) " Pt. 17/02 " } [6]=> array(1) { ["label"]=> string(81) " So. 18/02 " } } } ["dataset"]=> array(4) { [0]=> array(2) { ["seriesname"]=> string(34) " IRDN " ["data"]=> array(7) { [0]=> array(1) { ["value"]=> string(43) " 142.59 " } [1]=> array(1) { ["value"]=> string(43) " 174.88 " } [2]=> array(1) { ["value"]=> string(43) " 176.97 " } [3]=> array(1) { ["value"]=> string(43) " 182.48 " } [4]=> array(1) { ["value"]=> string(43) " 160.15 " } [5]=> array(1) { ["value"]=> string(43) " 160.72 " } [6]=> array(1) { ["value"]=> string(43) " 165.47 " } } } [1]=> array(2) { ["seriesname"]=> string(80) " SIRDN " ["data"]=> array(7) { [0]=> array(1) { ["value"]=> string(61) " 148.81 " } [1]=> array(1) { ["value"]=> string(61) " 197.29 " } [2]=> array(1) { ["value"]=> string(61) " 202.27 " } [3]=> array(1) { ["value"]=> string(61) " 211.93 " } [4]=> array(1) { ["value"]=> string(61) " 177.87 " } [5]=> array(1) { ["value"]=> string(61) " 179.37 " } [6]=> array(1) { ["value"]=> string(61) " 177.69 " } } } [2]=> array(2) { ["seriesname"]=> string(74) " IRDN24 " ["data"]=> array(7) { [0]=> array(1) { ["value"]=> string(60) " 140.31 " } [1]=> array(1) { ["value"]=> string(60) " 174.50 " } [2]=> array(1) { ["value"]=> string(60) " 180.38 " } [3]=> array(1) { ["value"]=> string(60) " 187.70 " } [4]=> array(1) { ["value"]=> string(60) " 161.91 " } [5]=> array(1) { ["value"]=> string(60) " 161.62 " } [6]=> array(1) { ["value"]=> string(60) " 160.98 " } } } [3]=> array(2) { ["seriesname"]=> string(112) " IRDN 8.22 " ["data"]=> array(7) { [0]=> array(1) { ["value"]=> string(69) " 147.33 " } [1]=> array(1) { ["value"]=> string(69) " 197.02 " } [2]=> array(1) { ["value"]=> string(69) " 202.21 " } [3]=> array(1) { ["value"]=> string(69) " 211.28 " } [4]=> array(1) { ["value"]=> string(69) " 178.11 " } [5]=> array(1) { ["value"]=> string(69) " 179.32 " } [6]=> array(1) { ["value"]=> string(69) " 176.31 " } } } } }
My question is how to change my code do this?
You should use
$arrData['categories'] = array( array ('category' => array()));
instead of
$arrData['categories'] = array( 'category' => array());
It will works as you want to.

Parsing Facebook PHP API Response - Remastered / Recycled 05/02/2015

My Array;
{
"bio": "Testing Facebook Bio -> Stupidly forgot to set whilst trying to fetch this information testing my latest Facebook App!",
"movies": {
"data": [
{
"name": "Night At The Museum",
"id": "251922028320619"
},
{
"name": "Little Nicky",
"id": "112378985439799"
},
{
"genre": "Action / Adventure / Comedy / Family / Sci-Fi",
"name": "Back to the Future Trilogy",
"id": "141545972523915"
},
{
"genre": "Stupid",
"name": "Jackass",
"id": "21295715752"
},
{
"genre": "Comedy",
"name": "Mrs. Brown's Boys D'Movie",
"id": "217475368404328"
},
{
"genre": "Animation, Family",
"name": "Madagascar",
"id": "149800431712088"
},
{
"genre": "Animation",
"name": "Frozen",
"id": "482368755113431"
},
{
"genre": "Animation",
"name": "Toy Story",
"id": "10498014129"
},
{
"genre": "This is a gritty drama that develops into an uplifting story of triumph over adversity. It should appeal to a wide audience, both male and female.",
"name": "Breaking Free Film",
"id": "135991503231501"
},
{
"genre": "Animation",
"name": "The Lion King",
"id": "12393266550"
},
{
"genre": "Animation | Comedy | Family ",
"name": "Despicable Me",
"id": "117067844993952"
},
{
"genre": "Action / Adventure",
"name": "Thor",
"id": "113589202010624"
},
{
"name": "Scooby-Doo: The Movie",
"id": "306089742863765"
},
{
"genre": "Action / fairy tale",
"name": "Hansel & Gretel: Witch Hunters",
"id": "271965656164363"
},
{
"name": "Harry Potter",
"id": "156794164312"
},
{
"name": "Star Wars",
"id": "216676368377759"
},
{
"name": "Minion",
"id": "136787429687873"
},
{
"name": "Night at the Museum 2",
"id": "115126478502712"
},
{
"genre": "Animation, Holiday, Family",
"name": "The Nightmare Before Christmas",
"id": "173587329354820"
},
{
"name": "Pacific Rim",
"id": "439835889373123"
},
{
"genre": "Action Adventure",
"name": "Oblivion UK",
"id": "235958443193536"
},
{
"genre": "Animation, Family",
"name": "Shrek",
"id": "355374000182"
},
{
"name": "Scooby Doo: The Movie",
"id": "106352129401640"
},
{
"genre": "Epic Action-Adventure",
"name": "Dracula Untold",
"id": "332230740134829"
},
{
"name": "Simba",
"id": "27665751322"
}
],
"paging": {
"next": "https://graph.facebook.com/v2.2/663878750359892/movies?fields=genre,name&limit=25&offset=25&__after_id=enc_AezyAtvaN1UtcaRwF9kgU5Z5PIv07LU_3Wli-CRCkYEol9BoJJtn86fNGT4v-XsnG-o"
}
},
"id": "663878750359892"
}
I know to get the "Bio" from my array, however this is pretty much out there onthe internet how to get the first level information from the Facebook API Array passed on;
$FB_About_Bio = $graph->getProperty('bio');
However when it comes to calling for "movies";
$FB_About_Movies = $graph->getProperty('movies');
This produces an array of itself I believe of which I do not know how to do a loop to list all the movie titles for example
How would I get all the movie names?
Full Script:
<?php
/* INCLUSION OF LIBRARY FILEs*/
require_once( 'lib/Facebook/FacebookSession.php');
require_once( 'lib/Facebook/FacebookRequest.php' );
require_once( 'lib/Facebook/FacebookResponse.php' );
require_once( 'lib/Facebook/FacebookSDKException.php' );
require_once( 'lib/Facebook/FacebookRequestException.php' );
require_once( 'lib/Facebook/FacebookRedirectLoginHelper.php');
require_once( 'lib/Facebook/FacebookAuthorizationException.php' );
require_once( 'lib/Facebook/GraphObject.php' );
require_once( 'lib/Facebook/GraphUser.php' );
require_once( 'lib/Facebook/GraphSessionInfo.php' );
require_once( 'lib/Facebook/Entities/AccessToken.php');
require_once( 'lib/Facebook/HttpClients/FacebookCurl.php' );
require_once( 'lib/Facebook/HttpClients/FacebookHttpable.php');
require_once( 'lib/Facebook/HttpClients/FacebookCurlHttpClient.php');
/* USE NAMESPACES */
use Facebook\FacebookSession;
use Facebook\FacebookRedirectLoginHelper;
use Facebook\FacebookRequest;
use Facebook\FacebookResponse;
use Facebook\FacebookSDKException;
use Facebook\FacebookRequestException;
use Facebook\FacebookAuthorizationException;
use Facebook\GraphObject;
use Facebook\GraphUser;
use Facebook\GraphSessionInfo;
use Facebook\FacebookHttpable;
use Facebook\FacebookCurlHttpClient;
use Facebook\FacebookCurl;
/*PROCESS*/
//1.Stat Session
session_start();
//2.Use app id,secret and redirect url
$app_id = '000000000000000';
$app_secret = 'xxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxx';
$redirect_url='http://rafflebananza.com/Desktop/facebook.php';
//3.Initialize application, create helper object and get fb sess
FacebookSession::setDefaultApplication($app_id,$app_secret);
$helper = new FacebookRedirectLoginHelper($redirect_url);
$sess = $helper->getSessionFromRedirect();
//4. if fb sess exists echo name
if(isset($sess)){
//store the token in the php session
$_SESSION['fb_token']=$sess->getToken();
//create request object,execute and capture response
$request = new FacebookRequest($sess,'GET','/me');
// from response get graph object
$response = $request->execute();
$graph = $response->getGraphObject(GraphUser::classname());
// use graph object methods to get user details
$FB_id = $graph->getId();
$FB_First_Name = $graph->getProperty('first_name');
$FB_Middle_Name = $graph->getProperty('middle_name');
$FB_Last_Name = $graph->getProperty('last_name');
$FB_About_Bio = $graph->getProperty('bio');
$FB_image = 'https://graph.facebook.com/'.$id.'/picture?width=300';
$FB_email = $graph->getProperty('email');
$graphArray = $graph->asArray();
$movies = $graphArray['movies']['data'];
foreach ($movies as $movie) {
$name = $movie['name'];
}
// Echo Info To Page:
echo '<h1>'.$name.'</h1>';
echo '<table><tr><td><strong>First Name:</strong></td><td><input type="text" value="'.$FB_First_Name.'"></input></td></tr>';
echo "<tr><td><strong>Middle Name: </strong></td><td>$FB_Middle_Name</td></tr>";
echo "<tr><td><strong>Last Name: </strong></td><td>$FB_Last_Name <br</td></tr>";
echo "<tr><td><strong>E-Mail Address:</strong></td><td>$FB_email</td></tr>";
echo "<tr><td><strong>About You:</strong></td><td>$FB_About_Bio</td></tr></table>";
echo "<img src='FB_$image' /><br><br>";
// Logout Button
echo "<a href='".$logout."'><button>Logout</button></a>";
}else{
//else echo login
echo '<a href="'.$helper->getLoginUrl(array('email')).'" >Login with facebook</a>';
}
Update
user9418 asked on Stackoverflow "How to parse a facebook graph api response" and bhushya answer has given somewhat of an insight to how it should be done. Below is my latest progress whereas previously I was getting no data whatsoever;
New Snippet;
$user_profile = (new FacebookRequest(
$sess, 'GET', '/me/movies'
))->execute()->getGraphObject();
$movies = $user_profile->getProperty('data');
$movies_data = $movies->asArray();//this will do all job for you..
foreach($movies_data as $row){
var_dump($row);
}
Result:
object(stdClass)#28 (4) { ["category"]=> string(5) "Movie" ["name"]=>
string(19) "Night At The Museum" ["created_time"]=> string(24)
"2014-12-16T23:05:57+0000" ["id"]=> string(15) "251922028320619" }
object(stdClass)#29 (4) { ["category"]=> string(5) "Movie" ["name"]=>
string(12) "Little Nicky" ["created_time"]=> string(24)
"2014-12-14T02:35:10+0000" ["id"]=> string(15) "112378985439799" }
object(stdClass)#30 (4) { ["category"]=> string(5) "Movie" ["name"]=>
string(26) "Back to the Future Trilogy" ["created_time"]=> string(24)
"2014-10-06T04:12:32+0000" ["id"]=> string(15) "141545972523915" }
object(stdClass)#31 (4) { ["category"]=> string(5) "Movie" ["name"]=>
string(7) "Jackass" ["created_time"]=> string(24)
"2014-08-23T21:51:24+0000" ["id"]=> string(11) "21295715752" }
object(stdClass)#32 (4) { ["category"]=> string(5) "Movie" ["name"]=>
string(25) "Mrs. Brown's Boys D'Movie" ["created_time"]=> string(24)
"2014-08-06T10:16:10+0000" ["id"]=> string(15) "217475368404328" }
object(stdClass)#33 (4) { ["category"]=> string(5) "Movie" ["name"]=>
string(10) "Madagascar" ["created_time"]=> string(24)
"2014-07-09T22:16:05+0000" ["id"]=> string(15) "149800431712088" }
object(stdClass)#34 (4) { ["category"]=> string(5) "Movie" ["name"]=>
string(6) "Frozen" ["created_time"]=> string(24)
"2014-05-27T20:47:31+0000" ["id"]=> string(15) "482368755113431" }
object(stdClass)#35 (4) { ["category"]=> string(5) "Movie" ["name"]=>
string(9) "Toy Story" ["created_time"]=> string(24)
"2014-04-15T01:39:17+0000" ["id"]=> string(11) "10498014129" }
object(stdClass)#36 (4) { ["category"]=> string(5) "Movie" ["name"]=>
string(19) "Breaking Free Film" ["created_time"]=> string(24)
"2014-02-20T14:16:06+0000" ["id"]=> string(15) "135991503231501" }
object(stdClass)#37 (4) { ["category"]=> string(5) "Movie" ["name"]=>
string(13) "The Lion King" ["created_time"]=> string(24)
"2014-02-19T08:32:34+0000" ["id"]=> string(11) "12393266550" }
object(stdClass)#38 (4) { ["category"]=> string(5) "Movie" ["name"]=>
string(13) "Despicable Me" ["created_time"]=> string(24)
"2014-02-19T08:32:29+0000" ["id"]=> string(15) "117067844993952" }
object(stdClass)#39 (4) { ["category"]=> string(5) "Movie" ["name"]=>
string(4) "Thor" ["created_time"]=> string(24)
"2013-11-29T02:22:14+0000" ["id"]=> string(15) "113589202010624" }
object(stdClass)#40 (4) { ["category"]=> string(5) "Movie" ["name"]=>
string(21) "Scooby-Doo: The Movie" ["created_time"]=> string(24)
"2013-10-17T13:41:51+0000" ["id"]=> string(15) "306089742863765" }
object(stdClass)#41 (4) { ["category"]=> string(5) "Movie" ["name"]=>
string(30) "Hansel & Gretel: Witch Hunters" ["created_time"]=>
string(24) "2013-10-15T21:22:05+0000" ["id"]=> string(15)
"271965656164363" } object(stdClass)#42 (4) { ["category"]=> string(5)
"Movie" ["name"]=> string(12) "Harry Potter" ["created_time"]=>
string(24) "2013-10-09T21:29:08+0000" ["id"]=> string(12)
"156794164312" } object(stdClass)#43 (4) { ["category"]=> string(5)
"Movie" ["name"]=> string(9) "Star Wars" ["created_time"]=> string(24)
"2013-08-30T20:12:14+0000" ["id"]=> string(15) "216676368377759" }
object(stdClass)#44 (4) { ["category"]=> string(5) "Movie" ["name"]=>
string(6) "Minion" ["created_time"]=> string(24)
"2013-07-22T12:25:27+0000" ["id"]=> string(15) "136787429687873" }
object(stdClass)#45 (4) { ["category"]=> string(5) "Movie" ["name"]=>
string(21) "Night at the Museum 2" ["created_time"]=> string(24)
"2013-06-14T01:18:02+0000" ["id"]=> string(15) "115126478502712" }
object(stdClass)#46 (4) { ["category"]=> string(5) "Movie" ["name"]=>
string(30) "The Nightmare Before Christmas" ["created_time"]=>
string(24) "2013-06-08T11:11:36+0000" ["id"]=> string(15)
"173587329354820" } object(stdClass)#47 (4) { ["category"]=> string(5)
"Movie" ["name"]=> string(11) "Pacific Rim" ["created_time"]=>
string(24) "2013-05-16T19:30:24+0000" ["id"]=> string(15)
"439835889373123" } object(stdClass)#48 (4) { ["category"]=> string(5)
"Movie" ["name"]=> string(11) "Oblivion UK" ["created_time"]=>
string(24) "2013-02-14T23:02:31+0000" ["id"]=> string(15)
"235958443193536" } object(stdClass)#49 (4) { ["category"]=> string(5)
"Movie" ["name"]=> string(5) "Shrek" ["created_time"]=> string(24)
"2012-10-03T07:01:57+0000" ["id"]=> string(12) "355374000182" }
object(stdClass)#50 (4) { ["category"]=> string(5) "Movie" ["name"]=>
string(21) "Scooby Doo: The Movie" ["created_time"]=> string(24)
"2012-09-20T11:55:28+0000" ["id"]=> string(15) "106352129401640" }
object(stdClass)#51 (4) { ["category"]=> string(5) "Movie" ["name"]=>
string(14) "Dracula Untold" ["created_time"]=> string(24)
"2012-09-17T16:15:38+0000" ["id"]=> string(15) "332230740134829" }
object(stdClass)#52 (4) { ["category"]=> string(15) "Movie character"
["name"]=> string(5) "Simba" ["created_time"]=> string(24)
"2012-09-06T22:23:22+0000" ["id"]=> string(11) "27665751322" }
My full code now being the same with this implemented above my echoing to the page. I just need to learn now how to loop through and list only the name of each movie wrapping each one in a tag!
Option 1: Convert the graph object to an array using the asArray() method and parse as a normal array.
$graphArray = $graph->asArray();
$movies = $graphArray['movies']['data'];
foreach ($movies as $movie) {
$name = $movie['name'];
}
Option 2: get the data property and loop through that array, then get the genre, name, and id properties using the getProperty method.
$movies = $graph->getProperty('movies');
foreach ($movies as $movie) {
$name = $movie->getProperty('name');
}
Resolved Snippet
$FB_User_Interests_Movies = (new FacebookRequest(
$sess, 'GET', '/me/movies'
))->execute()->getGraphObject()->asArray();
foreach($FB_User_Interests_Movies['data'] as $key) {
echo $key->name.'<br />';
}
How I resolved
Many things are in the Facebook Graph API alike this so I expanded research to ask how to get user friends and whatnot. This lead me to a YouTube video titled "Facebook PHP SDK v4: Get List of Pages Liked by User | Part 10" which was enough to explain how to achieve my question!

Categories