how to decode youtube api v3 json response - php

here is my youtube json response
query results:Array
(
[kind] => youtube#searchListResponse
[etag] => "tbWC5XrSXxe1WOAx6MK9z4hHSU8/_RyGfO1dwDRqY9aMf412I8xvZgw"
[nextPageToken] => CAoQAA
[pageInfo] => Array
(
[totalResults] => 1000000
[resultsPerPage] => 10
)
[items] => Array
(
[0] => Array
(
[kind] => youtube#searchResult
[etag] => "tbWC5XrSXxe1WOAx6MK9z4hHSU8/vJBGevzCpIbHOcHu7AAc4WsQ92E"
[id] => Array
(
[kind] => youtube#video
[videoId] => eHfMfHyiWYA
)
[snippet] => Array
(
[publishedAt] => 2013-11-04T16:09:20.000Z
[channelId] => UCSvOdBJgMnTYsK-cZIGZSYQ
[title] => Ann Arbor - a test bed for driverless cars?
[description] => University of Michigan researchers work to make Ann Arbor the first American city with a shared fleet of connected and driverless vehicles by 2021. The cars of ...
[thumbnails] => Array
(
[default] => Array
(
[url] => https://i.ytimg.com/vi/eHfMfHyiWYA/default.jpg
)
[medium] => Array
(
[url] => https://i.ytimg.com/vi/eHfMfHyiWYA/mqdefault.jpg
)
[high] => Array
(
[url] => https://i.ytimg.com/vi/eHfMfHyiWYA/hqdefault.jpg
)
)
[channelTitle] => michiganengineering
[liveBroadcastContent] => none
)
)
[1] => Array
(
[kind] => youtube#searchResult
[etag] => "tbWC5XrSXxe1WOAx6MK9z4hHSU8/9Mm7w3esGwaHCTasG_67T33aULE"
[id] => Array
(
[kind] => youtube#video
[videoId] => PRvGaCnfV-8
)
[snippet] => Array
(
[publishedAt] => 2009-01-26T16:50:35.000Z
[channelId] => UCPkN7EcOJlQAzCLxaZSn_5g
[title] => Timelapse Cars
[description] =>
[thumbnails] => Array
(
[default] => Array
(
[url] => https://i.ytimg.com/vi/PRvGaCnfV-8/default.jpg
)
[medium] => Array
(
[url] => https://i.ytimg.com/vi/PRvGaCnfV-8/mqdefault.jpg
)
[high] => Array
(
[url] => https://i.ytimg.com/vi/PRvGaCnfV-8/hqdefault.jpg
)
)
[channelTitle] => frehleycomet
[liveBroadcastContent] => none
)
)
[2] => Array
(
[kind] => youtube#searchResult
[etag] => "tbWC5XrSXxe1WOAx6MK9z4hHSU8/9_WnlpVsvx2WLKqdgxGXI3CRvBE"
[id] => Array
(
[kind] => youtube#video
[videoId] => U60TfgpHqSc
)
[snippet] => Array
(
[publishedAt] => 2013-07-01T23:23:59.000Z
[channelId] => UCULJH9kW-UdTBCDu27P0BoA
[title] => Are cars killing the Bicycle Kingdom?
[description] => Bike advocacy in the Bicycle Kingdom? China was once known as the Bicycle Kingdom but with the growth of the automobile, the nature of biking in Beijing is ...
[thumbnails] => Array
(
[default] => Array
(
[url] => https://i.ytimg.com/vi/U60TfgpHqSc/default.jpg
)
[medium] => Array
(
[url] => https://i.ytimg.com/vi/U60TfgpHqSc/mqdefault.jpg
)
[high] => Array
(
[url] => https://i.ytimg.com/vi/U60TfgpHqSc/hqdefault.jpg
)
)
[channelTitle] => llukitsch
[liveBroadcastContent] => none
)
)
[3] => Array
(
[kind] => youtube#searchResult
[etag] => "tbWC5XrSXxe1WOAx6MK9z4hHSU8/_2N5Lkva8p_pbJqLf5K61gNPHL0"
[id] => Array
(
[kind] => youtube#video
[videoId] => Xg5-oxTSM04
)
[snippet] => Array
(
[publishedAt] => 2014-02-13T17:42:31.000Z
[channelId] => UC-Gu_VkJmS1FGxsrSddKvfg
[title] => Market America's 2014 World Conference: FreeBee Cars
[description] => Market America's 2014 World Conference in Miami featured FreeBee cars showcasing MA brands like Motives® Cosmetics and DNA Miracles™.
[thumbnails] => Array
(
[default] => Array
(
[url] => https://i.ytimg.com/vi/Xg5-oxTSM04/default.jpg
)
[medium] => Array
(
[url] => https://i.ytimg.com/vi/Xg5-oxTSM04/mqdefault.jpg
)
[high] => Array
(
[url] => https://i.ytimg.com/vi/Xg5-oxTSM04/hqdefault.jpg
)
)
[channelTitle] => MarketAmerica
[liveBroadcastContent] => none
)
)
[4] => Array
(
[kind] => youtube#searchResult
[etag] => "tbWC5XrSXxe1WOAx6MK9z4hHSU8/VXZohXlA5kHPvrJ-rxpdEjUsaFY"
[id] => Array
(
[kind] => youtube#video
[videoId] => yrcq5awviik
)
[snippet] => Array
(
[publishedAt] => 2011-06-01T23:31:01.000Z
[channelId] => UC1URrXTrZ68pZZ4YDmT4WVQ
[title] => Ford's connected cars get street-smart
[description] => At AT&T Park in San Francisco, SmartPlanet correspondent Sumi Das talks to Ford's Mike Shulman about the company's research into intelligent vehicles.
[thumbnails] => Array
(
[default] => Array
(
[url] => https://i.ytimg.com/vi/yrcq5awviik/default.jpg
)
[medium] => Array
(
[url] => https://i.ytimg.com/vi/yrcq5awviik/mqdefault.jpg
)
[high] => Array
(
[url] => https://i.ytimg.com/vi/yrcq5awviik/hqdefault.jpg
)
)
[channelTitle] => SmartPlanetCBS
[liveBroadcastContent] => none
)
)
[5] => Array
(
[kind] => youtube#searchResult
[etag] => "tbWC5XrSXxe1WOAx6MK9z4hHSU8/FWmwcjir_8QSK6eq5D5gsY-lWXQ"
[id] => Array
(
[kind] => youtube#video
[videoId] => bFV7WU6UHaQ
)
[snippet] => Array
(
[publishedAt] => 2014-05-14T10:59:48.000Z
[channelId] => UCI1H2wxkvshpCcUNNOEc4Gw
[title] => Google's Driverless Cars Take to the Streets
[description] => Google's self-driving car technology is not likely to be available for several more years. But the company is already beginning the job of making the public ...
[thumbnails] => Array
(
[default] => Array
(
[url] => https://i.ytimg.com/vi/bFV7WU6UHaQ/default.jpg
)
[medium] => Array
(
[url] => https://i.ytimg.com/vi/bFV7WU6UHaQ/mqdefault.jpg
)
[high] => Array
(
[url] => https://i.ytimg.com/vi/bFV7WU6UHaQ/hqdefault.jpg
)
)
[channelTitle] => IBTimesUK
[liveBroadcastContent] => none
)
)
[6] => Array
(
[kind] => youtube#searchResult
[etag] => "tbWC5XrSXxe1WOAx6MK9z4hHSU8/t7dSXvQ-37jK0DFYHxk4iP1o3-s"
[id] => Array
(
[kind] => youtube#video
[videoId] => ifzSVGEHxMU
)
[snippet] => Array
(
[publishedAt] => 2012-10-22T13:45:39.000Z
[channelId] => UC6s5vC0tCuStbt4d5dfhmjA
[title] => Project C.A.R.S: BMW Z4
[description] => Scott tries out the newest car to be added, the BMW Z4. Those of you waiting for Tekkit, it will return soon! Hound is currently in the process of moving! Pl...
[thumbnails] => Array
(
[default] => Array
(
[url] => https://i.ytimg.com/vi/ifzSVGEHxMU/default.jpg
)
[medium] => Array
(
[url] => https://i.ytimg.com/vi/ifzSVGEHxMU/mqdefault.jpg
)
[high] => Array
(
[url] => https://i.ytimg.com/vi/ifzSVGEHxMU/hqdefault.jpg
)
)
[channelTitle] => scott2k44
[liveBroadcastContent] => none
)
)
[7] => Array
(
[kind] => youtube#searchResult
[etag] => "tbWC5XrSXxe1WOAx6MK9z4hHSU8/RW6QW1hjACRmSo6Za1BzsP1gbSk"
[id] => Array
(
[kind] => youtube#video
[videoId] => YZggnfolreA
)
[snippet] => Array
(
[publishedAt] => 2015-05-08T21:32:35.000Z
[channelId] => UC87geOOB2rCWkJFtUUeEBsw
[title] => Project CARS vs. DriveClub | Graphics, Sound, Rain & Weather Effects Gameplay Comparison (PS4)
[description] => Comparing the biggest racing games available on the PS4, DriveClub (PS4 exclusive) and Project CARS (also available on PC and Xbox One). Both games are ...
[thumbnails] => Array
(
[default] => Array
(
[url] => https://i.ytimg.com/vi/YZggnfolreA/default.jpg
)
[medium] => Array
(
[url] => https://i.ytimg.com/vi/YZggnfolreA/mqdefault.jpg
)
[high] => Array
(
[url] => https://i.ytimg.com/vi/YZggnfolreA/hqdefault.jpg
)
)
[channelTitle] => VideoGamesNL
[liveBroadcastContent] => none
)
)
[8] => Array
(
[kind] => youtube#searchResult
[etag] => "tbWC5XrSXxe1WOAx6MK9z4hHSU8/OIAM0bSsr9oHqPwPPg3msZUqeMU"
[id] => Array
(
[kind] => youtube#video
[videoId] => hJxFid8A0Xw
)
[snippet] => Array
(
[publishedAt] => 2015-04-02T16:00:05.000Z
[channelId] => UCNjakstwb629k6IE9BualRw
[title] => Comedians In Cars Getting Coffee: Single Shot - Analogy Lad
[description] => Watch it on Crackle: [http://bit.ly/SingleShot] Jerry's special guests are Todd Barry, George Wallace, Sarah Silverman, Fred Armisen, Kevin Hart and Jimmy ...
[thumbnails] => Array
(
[default] => Array
(
[url] => https://i.ytimg.com/vi/hJxFid8A0Xw/default.jpg
)
[medium] => Array
(
[url] => https://i.ytimg.com/vi/hJxFid8A0Xw/mqdefault.jpg
)
[high] => Array
(
[url] => https://i.ytimg.com/vi/hJxFid8A0Xw/hqdefault.jpg
)
)
[channelTitle] => Crackle
[liveBroadcastContent] => none
)
)
[9] => Array
(
[kind] => youtube#searchResult
[etag] => "tbWC5XrSXxe1WOAx6MK9z4hHSU8/Q945pOROs0oCGIOcFFq66OWZZdw"
[id] => Array
(
[kind] => youtube#video
[videoId] => I00AXDv8ncY
)
[snippet] => Array
(
[publishedAt] => 2015-05-07T07:03:27.000Z
[channelId] => UCyQCcFWSh7MLAhdq2a_2jKA
[title] => Project CARS Online #1 - Los gehts! - Formula A #Monza | Wheel/Controller
[description] => Project CARS ist ein Motorsport Multiplayer-Simulation Spiel, entwickelt und veröffentlicht vom britischen Video-Spiel-Entwickler Slightly Mad Studios.
[thumbnails] => Array
(
[default] => Array
(
[url] => https://i.ytimg.com/vi/I00AXDv8ncY/default.jpg
)
[medium] => Array
(
[url] => https://i.ytimg.com/vi/I00AXDv8ncY/mqdefault.jpg
)
[high] => Array
(
[url] => https://i.ytimg.com/vi/I00AXDv8ncY/hqdefault.jpg
)
)
[channelTitle] =>
[liveBroadcastContent] => none
)
)
)
)
i want to extract video title, thumbnail, description from this json response using foreach loop
i tried this one but its gives me an error please help me to solve this issues.
my php code is
$link = 'https://www.googleapis.com/youtube/v3/search?order=rating&part=snippet&q=cars&maxResults=10&key={KEY}';
$video = file_get_contents($link);
$video = json_decode($video, true);
echo '<pre>';
print_r("query results:");
print_r($video);
'</pre>';
foreach ($video->items as $data ){
echo "{$data->snippet->title}";
echo "<br>";
}

$video is an array, not an object, use $video['items'], the same with $data etc.

Related

PHP get facebook posts

$data = file_get_contents("https://graph.facebook.com/246179452202174/posts?access_token=TOKEN");
$data = json_decode($data, true);
print_r(array_values($data));
That's my code. It's simple. I can see the data and when I visit the link, it gives me the same info. So that works perfect. Underneath there's some outputs from the api.
Array ( [0] => Array ( [0] => Array ( [id] => 246179452202174_685970631556385 [from] => Array ( [name] => LGB eSports [category] => Sports Team [id] => 246179452202174 ) [message] => We are back! Sorry that we have been silent! But the silence is over! We will soon update you with some breaking news, stay tuned! Do not move your eyes! You do not want to miss this... [privacy] => Array ( [value] => [description] => [friends] => [allow] => [deny] => ) [type] => status [status_type] => mobile_status_update [created_time] => 2016-05-26T13:12:23+0000 [updated_time] => 2016-05-26T13:12:23+0000 [is_hidden] => [is_expired] => [likes] => Array ( [data] => Array ( [0] => Array ( [id] => 1042109789201130 [name] => Fabio Broggi ) [1] => Array ( [id] => 713442998796859 [name] => Christian Bråten ) [2] => Array ( [id] => 1801480580073327 [name] => Paulo Henrique ) [3] => Array ( [id] => 971217726332439 [name] => Benjamin Holm Davidsen ) ) [paging] => Array ( [cursors] => Array ( [before] => MTA0MjEwOTc4OTIwMTEzMAZDZD [after] => OTcxMjE3NzI2MzMyNDM5 ) ) ) )
When I try to echo $data or maybe $data[1] or something like it, it wont work. Also removing array_values gives me a output of "Array" or "ArrayArray".
So how may I print out the data from the api? It wont work, also; how may I fetch the image? I'd like to print the title of the post, and the image. As a link for a feed at my site.
More response code for link:
Array ( [data] => Array ( [0] => Array ( [id] => 246179452202174_685970631556385 [from] => Array ( [name] => LGB eSports [category] => Sports Team [id] => 246179452202174 ) [message] => We are back! Sorry that we have been silent! But the silence is over! We will soon update you with some breaking news, stay tuned! Do not move your eyes! You do not want to miss this... [privacy] => Array ( [value] => [description] => [friends] => [allow] => [deny] => ) [type] => status [status_type] => mobile_status_update [created_time] => 2016-05-26T13:12:23+0000 [updated_time] => 2016-05-26T13:12:23+0000 [is_hidden] => [is_expired] => [likes] => Array ( [data] => Array ( [0] => Array ( [id] => 1042109789201130 [name] => Fabio Broggi ) [1] => Array ( [id] => 713442998796859 [name] => Christian Bråten ) [2] => Array ( [id] => 1801480580073327 [name] => Paulo Henrique ) [3] => Array ( [id] => 971217726332439 [name] => Benjamin Holm Davidsen ) ) [paging] => Array ( [cursors] => Array ( [before] => MTA0MjEwOTc4OTIwMTEzMAZDZD [after] => OTcxMjE3NzI2MzMyNDM5 ) ) ) ) [1] => Array ( [id] => 246179452202174_680011585485623 [from] => Array ( [name] => Yoyo xno [category] => Musician/Band [id] => 281466768681242 ) [to] => Array ( [data] => Array ( [0] => Array ( [name] => LGB eSports [category] => Sports Team [id] => 246179452202174 ) ) ) [message] => GG - Spotify: https://open.spotify.com/track/7tT6XVWfjtpNF8enpFpZT4 [privacy] => Array ( [value] => [description] => [friends] => [allow] => [deny] => ) [type] => status [status_type] => wall_post [created_time] => 2016-05-12T17:33:49+0000 [updated_time] => 2016-05-12T17:33:49+0000 [is_hidden] => [is_expired] => [likes] => Array ( [data] => Array ( [0] => Array ( [id] => 196458690716329 [name] => Funny memes pics ) ) [paging] => Array ( [cursors] => Array ( [before] => MTk2NDU4NjkwNzE2MzI5 [after] => MTk2NDU4NjkwNzE2MzI5 ) ) ) ) [2] => Array ( [id] => 246179452202174_676411742512274 [from] => Array ( [name] => LGB eSports [category] => Sports Team [id] => 246179452202174 ) [message] => Breaking news! Changes in the team. We are sad to announce that Aurora will part ways with Lgb Female. We wish to thank her for the time she spent with us and we wish her all the best for the future. Here's what she says about this herself: "My time with LGB eSports has come to an end. I am extremely thankful for the opportunity to grow within this team, go to amazing places and meet amazing people. I have learnt so much from this experience and I will never forget all the adventures we've had together! I would like to use this moment of change to look back and sincerely thank all my team-mates for this learning experience, Per Lilliefelth for looking after us, our sponsor Intel for taking care of us and LGB eSports for their immense contribution to helping us grow as an all-female team in Counter-Strike. I am certain I will still remain close to the team and look forward to seeing them at many future events. As for myself I am currently a free agent and look forward to continuing my journey as a competitive CS:GO player for many more years to come. Regards, Aurora Lyngdal" [picture] => https://scontent.xx.fbcdn.net/v/t1.0-0/s130x130/13174168_676411742512274_4595019159434717947_n.jpg?oh=dad6bffaf94a2eae60c578d58f7cf8fc&oe=57E7E3F3 [link] => https://www.facebook.com/LGBeSports/photos/a.252900201530099.1073741829.246179452202174/676411742512274/?type=3 [name] => Timeline Photos [icon] => https://www.facebook.com/images/icons/photo.gif [privacy] => Array ( [value] => [description] => [friends] => [allow] => [deny] => ) [type] => photo [status_type] => added_photos [object_id] => 676411742512274 [created_time] => 2016-05-04T15:08:52+0000 [updated_time] => 2016-05-11T20:30:20+0000 [shares] => Array ( [count] => 4 ) [is_hidden] => [is_expired] => [likes] => Array ( [data] => Array ( [0] => Array ( [id] => 1756499731303402 [name] => Antony Giordans ) [1] => Array ( [id] => 196458690716329 [name] => Funny memes pics ) [2] => Array ( [id] => 10205058377778051 [name] => Charmaine Anne D. Better ) [3] => Array ( [id] => 992479757539742 [name] => Yunus Emre Çeker ) [4] => Array ( [id] => 244636029237859 [name] => Roman Hardinger ) [5] => Array ( [id] => 1134092109982746 [name] => Khoa Nguyen ) [6] => Array ( [id] => 525969817592366 [name] => Joseph Turton ) [7] => Array ( [id] => 1152533898100318 [name] => Kristian Sørensen ) [8] => Array ( [id] => 1193252910715325 [name] => Kamil Grochu Groszek ) [9] => Array ( [id] => 1158759100821275 [name] => Ace Kjirkovski ) [10] => Array ( [id] => 10153477966360064 [name]
you must echo this variable
$data[0][0]['message']

having difficulty to get style id from edmunds api

Array ( [make] => Array ( [id] => 200000201 [name] => Nissan [niceName] => nissan ) [model] => Array ( [id] => Nissan_Pathfinder [name] => Pathfinder [niceName] => pathfinder ) [engine] => Array ( [equipmentType] => ENGINE [availability] => USED [cylinder] => 6 [size] => 3.5 [configuration] => V [fuelType] => regular unleaded [horsepower] => 260 [type] => gas [code] => VQ35DE [rpm] => Array ( [horsepower] => 6400 [torque] => 4400 ) [valve] => Array ( [gear] => double overhead camshaft ) ) [transmission] => Array ( [id] => 200405221 [name] => continuously variableA [equipmentType] => TRANSMISSION [availability] => STANDARD [automaticType] => Continuously variable [transmissionType] => AUTOMATIC [numberOfSpeeds] => continuously variable ) [drivenWheels] => front wheel drive [numOfDoors] => 4 [options] => Array ( ) [colors] => Array ( [0] => Array ( [category] => Interior [options] => Array ( [0] => Array ( [id] => 200439557 [name] => Almond Leather [equipmentType] => COLOR [availability] => USED ) ) ) [1] => Array ( [category] => Exterior [options] => Array ( [0] => Array ( [id] => 200439553 [name] => Cayenne Red Metallic [equipmentType] => COLOR [availability] => USED ) ) ) ) [manufacturerCode] => 25513 [price] => Array ( [baseMSRP] => 34850 [baseInvoice] => 31770 [deliveryCharges] => 845 [usedTmvRetail] => 22236 [usedPrivateParty] => 20769 [usedTradeIn] => 19116 [estimateTmv] => ) [categories] => Array ( [market] => Crossover [EPAClass] => Sport Utility Vehicles [vehicleSize] => Large [crossover] => Car [primaryBodyType] => SUV [vehicleStyle] => 4dr SUV [vehicleType] => SUV ) [vin] => 5N1AR2MNXDC676161 [squishVin] => 5N1AR2MNDC [years] => Array ( [0] => Array ( [id] => 100539157 [year] => 2013 [styles] => Array ( [0] => Array ( [id] => 200439517 [name] => SL 4dr SUV (3.5L 6cyl CVT) [submodel] => Array ( [body] => SUV [modelName] => Pathfinder SUV [niceName] => suv ) [trim] => SL ) ) ) ) [matchingType] => SQUISHVIN [MPG] => Array ( [highway] => 26 [city] => 20 ) ) NissanPathfinder2013
from this array I get make, model, and year like this:
$ed_json=file_get_contents($ed_url);
$ed_array= json_decode($ed_json, true);
print_r($ed_array);
$make=$ed_array['make']['name'];
$model=$ed_array['model']['name'];
$year=$ed_array['years']['0']['year'];
echo $make;
echo $model;
echo $year;
now I am doing the same thing to get style id but I cannot get it.
this is the code for style id:
$id=$ed_array['styles']['0']['id'];
echo $id;
Array for years :
[years] => Array (
[0] => Array (
[id] => 100539157
[year] => 2013
[styles] => Array (
[0] => Array (
[id] => 200439517
[name] => SL 4dr SUV (3.5L 6cyl CVT)
[submodel] => Array (
[body] => SUV
[modelName] => Pathfinder SUV
[niceName] => suv
)
[trim] => SL
)
)
)
)
As you can see [styles][0][id] is in [years][0].
So, to get style id you can do :
$style = $ed_array['years'][0]['styles'][0]['id'];
In your case this should be $ed_array['years'][0]['styles'][0]['id'].

How to process complex json object in PHP

I have a working PHP app running in Bluemix that I want to extend to call a RESTful service (Insights for Twitter). I've been able to call the service, retrieve the json body, and use json_decode as follows to create an array:
$insightList = json_decode($guzzleResponse ->getBody(), true);
However, I can't figure out how to get access to the field I'm interested in. I've searched for solution on the web and tried a few approaches that looked promising, but when I tried to integrate them, I couldn't get them to work. I'm a bit of a PHP novice, so if something doesn't work I'm not sure how to proceed.
The json structure is quite complex, with three top-level arrays - search, tweets, and next. I'm interested in the second of these, tweets. It's a pretty complex array - there is one entry per tweet. The field I want right now is tweets.cde.message.body. You can find the full schema here: https://cdeservice.eu-gb.mybluemix.net/rest-api/#!/messages/getTweets
This is the code I have so far:
foreach($insightList as $cde) {
foreach($cde as $message) {
$insight = $message['body'];
if(strlen($insight) > 60) {
$posts[] = array(
'id' => 99999999,
//Temp; remove links from the text
'text' => $insight,
'category' => $insightCategory,
'image' => 'false'
);
}
}
}
Here's a print_r of $insightList:
[search] => Array (
[results] => 28
[current] => 28
)
[tweets] => Array (
[0] => Array (
[cde] => Array (
[author] => Array (
[gender] => male
[parenthood] => Array (
[isParent] => unknown
)
[location] => Array (
[country] =>
)
[maritalStatus] => Array (
[isMarried] => unknown
)
)
[content] => Array (
[sentiment] => Array (
[evidence] => Array ( )
[polarity] => NEUTRAL
)
)
)
[message] => Array (
[postedTime] => 2015-01-13T09:42:16.000Z
[verb] => share
[link] => http://twitter.com/zWDOM/statuses/554936456477933569
[generator] => Array (
[displayName] => Twitter Web Client
[link] => http://twitter.com
)
[body] => RT #VisualSuccess: "Mainframe & Cloud" Magazine wurde soeben publiziert! http://www.twitter.com #zWDOM #Rocket #JohnKnutson_IBM
[favoritesCount] => 0
[objectType] => activity
[actor] => Array (
[summary] => Seit über 25 Jahren im Mainframebereich, derzeit als Senior Consultant und IT Architekt für zEnterprise und Projektmanager beim IBM BP Cancom in Köln
[image] => https://pbs.twimg.com/profile_images/424202233463308288/XQquUcnh_normal.jpeg
[statusesCount] => 2309
[utcOffset] => 3600
[languages] => Array (
[0] => de
)
[preferredUsername] => zWDOM
[displayName] => Willi Domroese
[postedTime] => 2009-12-17T01:39:25.000Z
[link] => http://www.twitter.com/zWDOM
[verified] =>
)
[provider] => Array (
[displayName] => Twitter
[link] => http://www.twitter.com
[objectType] => service
)
[twitter_filter_level] => medium
[twitter_entities] => Array (
[urls] => Array (
[0] => Array (
[display_url] => ln.is/paper.li/visua…
[indices] => Array (
[0] => 77
[1] => 99
)
[expanded_url] => http://ln.is/paper.li/visualsucce/7zYNk
[url] => http://www.twitter.com
)
)
[hashtags] => Array ( )
[user_mentions] => Array (
[0] => Array (
[indices] => Array (
[0] => 3
[1] => 17
)
[screen_name] => VisualSuccess
[id_str] => 213337792
[name] => Predrag Gasic
[id] => 213337792
)
[1] => Array (
[indices] => Array (
[0] => 101
[1] => 107
)
[screen_name] => zWDOM
[id_str] => 97334013
[name] => Willi Domroese
[id] => 97334013
)
[2] => Array (
[indices] => Array (
[0] => 108
[1] => 115
)
[screen_name] => Rocket
[id_str] => 870584947
[name] => Rocket Software
[id] => 870584947
)
[3] => Array (
[indices] => Array (
[0] => 116
[1] => 132
)
[screen_name] => JohnKnutson_IBM
[id_str] => 16452310
[name] => John Knutson
[id] => 16452310
)
)
[trends] => Array ( )
[symbols] => Array ( )
)
[twitter_lang] => de
[id] => tag:search.twitter.com,2005:554936456477933569
[retweetCount] => 1
[gnip] => Array (
[urls] => Array (
[0] => Array (
[expanded_url] => http://linkis.com/paper.li/visualsucce/7zYNk
[expanded_status] => 200
[url] => http://www.twitter.com
)
)
[language] => Array (
[value] => de
)
)
[object] => Array (
[postedTime] => 2015-01-13T08:04:48.000Z
[verb] => post
[link] => http://twitter.com/VisualSuccess/statuses/554911928527888384
[generator] => Array (
[displayName] => Linkis.com
[link] => http://linkis.com
)
[body] => "Mainframe & Cloud" Magazine wurde soeben publiziert! http://www.twitter.com ://www.twitter.com
[objectType] => activity
[actor] => Array (
[summary] => Ù† (N), Wirtschaftsinformatiker | SAP Consultant Logistics #bigdata #appdevelopment #webdesign #eCommerce #SocialMedia #contentmarketing #SmartHome #Journal
[image] => https://pbs.twimg.com/profile_images/2841607223/959b0d23646b1f24bd7b70deac160e2f_normal.jpeg
[statusesCount] => 14185
[utcOffset] => 3600
[languages] => Array (
[0] => de
)
[preferredUsername] => VisualSuccess
[displayName] => Predrag Gasic
[postedTime] => 2010-11-08T17:19:27.000Z
[link] => http://www.twitter.com/VisualSuccess
[verified] =>
)
[provider] => Array (
[displayName] => Twitter
[link] => http://www.twitter.com
[objectType] => service
)
[twitter_filter_level] => low
[twitter_entities] => Array (
[urls] => Array (
[0] => Array (
[display_url] => ln.is/paper.li/visua…
[indices] => Array (
[0] => 58
[1] => 80
)
[expanded_url] => http://ln.is/paper.li/visualsucce/7zYNk
[url] => http://www.twitter.com
)
)
[hashtags] => Array ( )
[user_mentions] => Array (
[0] => Array (
[indices] => Array (
[0] => 82
[1] => 88
)
[screen_name] => zWDOM
[id_str] => 97334013
[name] => Willi Domroese
[id] => 97334013
)
[1] => Array (
[indices] => Array (
[0] => 89
[1] => 96
)
[screen_name] => Rocket
[id_str] => 870584947
[name] => Rocket Software
[id] => 870584947
)
[2] => Array (
[indices] => Array (
[0] => 97
[1] => 113
)
[screen_name] => JohnKnutson_IBM
[id_str] => 16452310
[name] => John Knutson
[id] => 16452310
)
)
[trends] => Array ( )
[symbols] => Array (
)
)
)
)
)
Answers to this question would be greatly appreciated
Iterating $insightList will not get you to the cde level. And you don't actually want the cde; you want the message.
Just iterate the tweets. You don't need a foreach to get keyed info from a PHP array.
foreach($insightList['tweets'] as $tweet) {
$insight = $tweet['message']['body'];
if(strlen($insight) > 60) {
$posts[] = array(
'id' => 99999999,
//Temp; remove links from the text
'text' => $insight,
'category' => $insightCategory,
'image' => 'false'
);
}
}
It looks to me from your print_r that what you want is at:
echo $insightList[0]['message']['body'];
When there's a section like this [some_key], then 'some_key' is an associative array key, and you can access those elements be they additional arrays or a value, using the key name, as I illustrated here.

Merge similar elements into a multi-dimensional PHP array

so i have an array that has similar elements and i need to merge it into a multi-dimensional array, merging some of the elements together, I maybe asking too much out of life, but thought i'd ask... thanks in advance :)
My current array:
Array
(
[0] => Array
(
[name] => Facebook
[icon] =>
[sectors] => BSS
[url] => http://www.facebook.com/...
)
[1] => Array
(
[name] => Facebook
[icon] =>
[sectors] => BSP
[url] => http://www.facebook.com/...
)
[2] => Array
(
[name] => GooglePlus
[icon] =>
[sectors] => BSP
[url] => https://plus.google.com/...
)
[3] => Array
(
[name] => LinkedIn
[icon] =>
[sectors] => BSS
[url] => http://www.linkedin.com/...
)
[4] => Array
(
[name] => LinkedIn
[icon] =>
[sectors] => BSP
[url] => http://www.linkedin.com/...
)
[5] => Array
(
[name] => Twitter
[icon] =>
[sectors] => BSS
[url] => http://twitter.com/...
)
[6] => Array
(
[name] => Twitter
[icon] =>
[sectors] => BSP
[url] => http://twitter.com/...
)
[7] => Array
(
[name] => Vimeo
[icon] =>
[sectors] => BSS
[url] => http://vimeo.com/...
)
[8] => Array
(
[name] => Vimeo
[icon] =>
[sectors] => BSP
[url] => https://vimeo.com/...
)
[9] => Array
(
[name] => Youtube
[icon] =>
[sectors] => BSS
[url] => http://www.youtube.com/...
)
[10] => Array
(
[name] => Blog
[icon] =>
[sectors] => Local
[url] => /blog
)
)
I need to end up with:
Array
(
[0] => Array
(
[name] => Facebook
[icon] =>
[sectors] => Array
(
[0] => Array
(
[name] => BSS
[url] => http://www.facebook.com/...
)
[1] => Array
(
[name] => BSP
[url] => http://www.facebook.com/...
)
)
)
[1] => Array
(
[name] => GooglePlus
[icon] =>
[sectors] => Array
(
[0] => Array
(
[name] => BSP
[url] => https://plus.google.com/...
)
)
)
[2] => Array
(
[name] => LinkedIn
[icon] =>
[sectors] => Array
(
[0] => Array
(
[name] => BSS
[url] => http://www.linkedin.com/....
)
[1] => Array
(
[name] => BSP
[url] => http://www.linkedin.com/...
)
)
)
[3] => Array
(
[name] => Twitter
[icon] =>
[sectors] => Array
(
[0] => Array
(
[name] => BSS
[url] => http://twitter.com/...
)
[1] => Array
(
[name] => BSP
[url] => http://twitter.com/...
)
)
)
[4] => Array
(
[name] => Vimeo
[icon] =>
[sectors] => Array
(
[0] => Array
(
[name] => BSS
[url] => http://vimeo.com/...
)
[1] => Array
(
[name] => BSP
[url] => https://vimeo.com/...
)
)
)
[5] => Array
(
[name] => Youtube
[icon] =>
[sectors] => Array
(
[0] => Array
(
[name] => BSS
[url] => http://www.youtube.com/....
)
)
)
[6] => Array
(
[name] => Blog
[icon] =>
[sectors] => Array
(
[0] => Array
(
[name] => Local
[url] => /blog
)
)
)
)
Here's what I would do:
foreach ($original_array as $value){
$result_array[$value['name']]['name'] = $value['name'];
$result_array[$value['name']]['icon'] = $value['icon'];
$result_array[$value['name']]['sectors'][] = array(
'name' => $value['sectors'],
'url' => $value['url']
}
What I'm doing is using the name (Facebook, Youtube, etc..) as the array key to be able to push (using []) the sectors and url elements of the sub arrays into their own subarray.
From here you can either return $result_array as is or if you really need an indexed array, return array_values($result_array)
I'm making the assumption that icons will not change between two entries that bear the same name. If this is not the case, you can either make a hash of the name + icon or just concatenate the name and icon and use that as the array key.

How to sort object array by value deep inside array

I have this array:
stdClass Object
(
[tid] => 26001835
[vid] => 5
[name] => AppleTV
[description] => My description
[format] => filtered_html
[weight] => 0
[vocabulary_machine_name] => how_to_watch_device
[field_device_image] => Array
(
[und] => Array
(
[0] => Array
(
[fid] => 26608990
[alt] =>
[title] =>
[width] => 194
[height] => 102
[uid] => 26000697
[filename] => Apple-TV.png
[uri] => public://Apple-TV.png
[filemime] => image/png
[filesize] => 2103
[status] => 1
[timestamp] => 1405346182
)
)
)
[field_buy_now_button_link] => Array
(
[und] => Array
(
[0] => Array
(
[value] => http://www.something.com
[format] =>
[safe_value] => http://www.something.com
)
)
)
[field_learn_more] => Array
(
[und] => Array
(
[0] => Array
(
[value] => http://something.com/somepage
[format] =>
[safe_value] => http://something.com/somepage
)
)
)
[field_device_category] => Array
(
[und] => Array
(
[0] => Array
(
[value] => network
)
)
)
)
stdClass Object
(
[tid] => 26001834
[vid] => 5
[name] => Playstation - USA
[description] => My description
[format] => filtered_html
[weight] => 2
[vocabulary_machine_name] => how_to_watch_device
[field_device_image] => Array
(
[und] => Array
(
[0] => Array
(
[fid] => 26608991
[alt] =>
[title] =>
[width] => 194
[height] => 102
[uid] => 26000697
[filename] => ps4network.png
[uri] => public://ps4network.png
[filemime] => image/png
[filesize] => 4566
[status] => 1
[timestamp] => 1405346218
)
)
)
[field_buy_now_button_link] => Array
(
[und] => Array
(
[0] => Array
(
[value] => http://www.somesite.com
[format] =>
[safe_value] => http://somesite.com
)
)
)
[field_learn_more] => Array
(
)
[field_device_category] => Array
(
[und] => Array
(
[0] => Array
(
[value] => blast_areas
)
)
)
)
stdClass Object
(
[tid] => 26001836
[vid] => 5
[name] => Brighthouse Networks
[description] => My description
[format] => filtered_html
[weight] => 3
[vocabulary_machine_name] => how_to_watch_device
[field_device_image] => Array
(
[und] => Array
(
[0] => Array
(
[fid] => 26608993
[alt] =>
[title] =>
[width] => 194
[height] => 102
[uid] => 26000697
[filename] => brighthouse.png
[uri] => public://brighthouse.png
[filemime] => image/png
[filesize] => 8392
[status] => 1
[timestamp] => 1405358781
)
)
)
[field_buy_now_button_link] => Array
(
)
[field_learn_more] => Array
(
)
[field_device_category] => Array
(
[und] => Array
(
[0] => Array
(
[value] => ppv_provider
)
)
)
)
I want to sort the array by the array by the value in field_device_category. Basically, I want to group the results but first I need to make sure all the objects are sorted by field_device_category.
Thanks in advance!
Use usort for this kind of sorting. It uses Quicksort in the background and takes a user-defined function to compare the array elements:
usort($array, "complicatedArrayComparer");
function complicatedArrayComparer($a,$b)
{
if ($a['field_device_category'] == $b['field_device_category']) {
return 0;
}
return ($a['field_device_category'] < $b['field_device_category']) ? -1 : 1;
}

Categories