Solved

Get Data from Array

Posted on 2012-12-24
13
346 Views
Last Modified: 2012-12-25
I want to print the "to:name" and the "from:name" but i am having trouble extracting the data from the array.


Code:
  $requests = file_get_contents($request_url);

  //Print all outstanding app requests
  echo '<pre>';
  print_r($requests);
  echo '</pre>'; 

  print $requests->data->to->name; //not working

Open in new window



Output of print_r:

{"data":[{"id":"456332257759434_749673123","application":{"name":"Virtual Body Shots","namespace":"virtualbodyshots","id":"432338926835657"},"to":{"name":"Rich Ezeikiel","id":"749673123"},"from":{"name":"Kim Lee","id":"100000430620787"},"message":"Would you be interested in giving me a tequila body shot?","created_time":"2012-12-24T11:06:04+0000"},{"id":"319927994792678_749673123","application":{"name":"Virtual Body Shots","namespace":"virtualbodyshots","id":"432338926835657"},"to":{"name":"Rich Ezeikiel","id":"749673123"},"from":{"name":"Zekio Richie","id":"100004945084145"},"message":"Would you be interested in giving me a tequila body shot?","created_time":"2012-12-24T03:13:55+0000"}],"paging":{"previous":"https:\/\/graph.facebook.com\/749673123\/apprequests?access_token=432338926835657|LuUcubU7ml2mWX55yN8_ltrYe-Y&limit=50&since=1356347164&__paging_token=456332257759434_749673123&__previous=1","next":"https:\/\/graph.facebook.com\/749673123\/apprequests?access_token=432338926835657|LuUcubU7ml2mWX55yN8_ltrYe-Y&limit=50&until=1356318835&__paging_token=319927994792678_749673123"}}

Open in new window

0
Comment
Question by:zefex
  • 5
  • 3
  • 3
  • +1
13 Comments
 
LVL 31

Expert Comment

by:Marco Gasi
ID: 38719377
file_get_contents returns a string, not an array. You can try to use file (http://it2.php.net/manual/en/function.file.php), but keep in mind the array elements are the lines of the file

In addition, to print an element an array you can't use the object notation $result->element
but you have to use the array notation: $result['element']

Seeing your string, however, it seems rather complicate to format  as a multidimensional array: I think to have seen some inconsitence in its structure. You could check how that data are grabbed and put together: I think we can help you to create a better file, ready to be read by a function like a file or well formatted to be easily splitted into an array, but for this you should tell us how that file is built and from what data.

Cheers
0
 
LVL 31

Expert Comment

by:Marco Gasi
ID: 38719384
Looking better at your string, it is not inconsistent but it seems very difficult to convert it into an array:
{"data":
	[
		{
			"id":"456332257759434_749673123",
			"application":
			{
				"name":"Virtual Body Shots",
				"namespace":"virtualbodyshots",
				"id":"432338926835657"
			},
			"to": 
			{
				"name":"Rich Ezeikiel","id":"749673123"
			},
			"from": 
			{
				"name":"Kim Lee",
				"id":"100000430620787"
			},
			"message": "Would you be interested in giving me a tequila body shot?",
			"created_time":"2012-12-24T11:06:04+0000"
		},
		{
			"id":"319927994792678_749673123",
			"application":
			{
				"name":"Virtual Body Shots",
				"namespace":"virtualbodyshots",
				"id":"432338926835657"
			},
			"to":
			{
				"name":"Rich Ezeikiel",
				"id":"749673123"
			},
			"from":
			{
				"name":"Zekio Richie",
				"id":"100004945084145"
			},
			"message":"Would you be interested in giving me a tequila body shot?",
			"created_time":"2012-12-24T03:13:55+0000"
		}
	],
"paging":
	{
	"previous":"https:\/\/graph.facebook.com\/749673123\/apprequests?access_token=432338926835657|LuUcubU7ml2mWX55yN8_ltrYe-Y&limit=50&since=1356347164&__paging_token=456332257759434_749673123&__previous=1","next":"https:\/\/graph.facebook.com\/749673123\/apprequests?access_token=432338926835657|LuUcubU7ml2mWX55yN8_ltrYe-Y&limit=50&until=1356318835&__paging_token=319927994792678_749673123"
	}
}

Open in new window


If I'll can, I'll try to do it, but my family is waiting for me...

Cheers
0
 

Author Comment

by:zefex
ID: 38719389
Thank you for your support! That string is coming from the facebook API, i dont have any control over it. I just want to grab the names from the data. Thank you!

  //Get all app requests for user
  $request_url ="https://graph.facebook.com/" .
    $user_id . "/apprequests?" .
    $access_token;
  $requests = file_get_contents($request_url);

Open in new window

0
Courses: Start Training Online With Pros, Today

Brush up on the basics or master the advanced techniques required to earn essential industry certifications, with Courses. Enroll in a course and start learning today. Training topics range from Android App Dev to the Xen Virtualization Platform.

 
LVL 31

Expert Comment

by:Marco Gasi
ID: 38719400
I f you are only interested in getting "to" name you can try this:

$requests = file_get_contents($request_url);

preg_match_all('/(?<="to":\{"name":")[^"]*/simx', $requests, $matches);

foreach ($matches[0] as $match) {
    echo "$match<br>";
}
0
 

Author Comment

by:zefex
ID: 38719401
If i use json decode

$requests = json_decode($requests);
print_r($requests);

Open in new window


i get this:

stdClass Object ( [data] => Array ( [0] => stdClass Object ( [id] => 456332257759434_749673123 [application] => stdClass Object ( [name] => Virtual Body Shots [namespace] => virtualbodyshots [id] => 432338926835657 ) [to] => stdClass Object ( [name] => Rich Ezeikiel [id] => 749673123 ) [from] => stdClass Object ( [name] => Kim Lee [id] => 100000430620787 ) [message] => Would you be interested in giving me a tequila body shot? [created_time] => 2012-12-24T11:06:04+0000 ) [1] => stdClass Object ( [id] => 319927994792678_749673123 [application] => stdClass Object ( [name] => Virtual Body Shots [namespace] => virtualbodyshots [id] => 432338926835657 ) [to] => stdClass Object ( [name] => Rich Ezeikiel [id] => 749673123 ) [from] => stdClass Object ( [name] => Zekio Richie [id] => 100004945084145 ) [message] => Would you be interested in giving me a tequila body shot? [created_time] => 2012-12-24T03:13:55+0000 ) ) [paging] => stdClass Object ( [previous] => https://graph.facebook.com/749673123/apprequests?access_token=432338926835657|LuUcubU7ml2mWX55yN8_ltrYe-Y&limit=50&since=1356347164&__paging_token=456332257759434_749673123&__previous=1 [next] => https://graph.facebook.com/749673123/apprequests?access_token=432338926835657|LuUcubU7ml2mWX55yN8_ltrYe-Y&limit=50&until=1356318835&__paging_token=319927994792678_749673123 ) )

Open in new window

0
 
LVL 11

Expert Comment

by:mcnute
ID: 38719777
extract your json_decoded stdClass Object it like that:
//name:  
$request->data->to['name'];
//to:  
$request->data->from['name'];

Open in new window

0
 
LVL 109

Assisted Solution

by:Ray Paseur
Ray Paseur earned 500 total points
ID: 38720060
If you echo '<pre>' before printing the decoded JSON object, your output will be easier to read.
0
 
LVL 109

Accepted Solution

by:
Ray Paseur earned 500 total points
ID: 38720073
Please see: http://www.laprbass.com/RAY_temp_zefex.php

<?php // RAY_temp_zefex.php
error_reporting(E_ALL);
echo '<pre>';

// TEST DATA
$jso = <<<ENDJSO
{"data":[{"id":"456332257759434_749673123","application":{"name":"Virtual Body Shots","namespace":"virtualbodyshots","id":"432338926835657"},"to":{"name":"Rich Ezeikiel","id":"749673123"},"from":{"name":"Kim Lee","id":"100000430620787"},"message":"Would you be interested in giving me a tequila body shot?","created_time":"2012-12-24T11:06:04+0000"},{"id":"319927994792678_749673123","application":{"name":"Virtual Body Shots","namespace":"virtualbodyshots","id":"432338926835657"},"to":{"name":"Rich Ezeikiel","id":"749673123"},"from":{"name":"Zekio Richie","id":"100004945084145"},"message":"Would you be interested in giving me a tequila body shot?","created_time":"2012-12-24T03:13:55+0000"}],"paging":{"previous":"https:\/\/graph.facebook.com\/749673123\/apprequests?access_token=432338926835657|LuUcubU7ml2mWX55yN8_ltrYe-Y&limit=50&since=1356347164&__paging_token=456332257759434_749673123&__previous=1","next":"https:\/\/graph.facebook.com\/749673123\/apprequests?access_token=432338926835657|LuUcubU7ml2mWX55yN8_ltrYe-Y&limit=50&until=1356318835&__paging_token=319927994792678_749673123"}}
ENDJSO;

// DECODE INTO AN OBJECT
$obj = json_decode($jso);

// ACTIVATE THIS TO SEE THE ENTIRE OBJECT
// var_dump($obj);

// USE OOP NOTATION TO FIND INFORMATION IN THE OBJECT
foreach ($obj->data as $unit)
{
    // ACTIVATE THIS TO SEE THE UNIT OBJECT
    // var_dump($unit);
    $t = (string)$unit->to->name;
    $f = (string)$unit->from->name;
    echo PHP_EOL . "TO: $t, FROM: $f";
}

Open in new window

HTH, ~Ray
0
 

Author Comment

by:zefex
ID: 38720219
Did not work:   $requests->data->to['name'];
0
 

Author Closing Comment

by:zefex
ID: 38720221
Thank you Ray_Paseur!
0
 
LVL 11

Expert Comment

by:mcnute
ID: 38720234
It seams that expert exchange is about providing the prethought solution, without any effort on the authors side.
I prefer not to get the points instead of following such a policy. For me experts-exchange is providing the idea how to solve an issue, then the rest should be thought out and researched by the author, so he/she can learn something on the long run.

mcnute
0
 

Author Comment

by:zefex
ID: 38720249
@mcnute

I tried echo $requests->data->to['name'];
I tried print $requests->data->to['name'];

neither worked.
0
 
LVL 109

Expert Comment

by:Ray Paseur
ID: 38720292
Thanks for the points and Merry Christmas.  To the point about learning ideas about how to solve a problem (a very good point), the takeaway message might be to learn to use and love var_dump() -- once you can see the data, the programming usually writes itself!
0

Featured Post

Announcing the Most Valuable Experts of 2016

MVEs are more concerned with the satisfaction of those they help than with the considerable points they can earn. They are the types of people you feel privileged to call colleagues. Join us in honoring this amazing group of Experts.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Browsers only know CSS so your awesome SASS code needs to be translated into normal CSS. Here I'll try to explain what you should aim for in order to take full advantage of SASS.
JavaScript has plenty of pieces of code people often just copy/paste from somewhere but never quite fully understand. Self-Executing functions are just one good example that I'll try to demystify here.
The viewer will learn how to count occurrences of each item in an array.
The viewer will receive an overview of the basics of CSS showing inline styles. In the head tags set up your style tags: (CODE) Reference the nav tag and set your properties.: (CODE) Set the reference for the UL element and styles for it to ensu…

816 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question

Need Help in Real-Time?

Connect with top rated Experts

11 Experts available now in Live!

Get 1:1 Help Now