Get Data from Array

Zeke Rich
Zeke Rich used Ask the Experts™
on
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

Comment
Watch Question

Do more with

Expert Office
EXPERT OFFICE® is a registered trademark of EXPERTS EXCHANGE®
Marco GasiFreelancer
Top Expert 2010

Commented:
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
Marco GasiFreelancer
Top Expert 2010

Commented:
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
Zeke RichOwner

Author

Commented:
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

Angular Fundamentals

Learn the fundamentals of Angular 2, a JavaScript framework for developing dynamic single page applications.

Marco GasiFreelancer
Top Expert 2010

Commented:
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>";
}
Zeke RichOwner

Author

Commented:
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

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

Open in new window

Most Valuable Expert 2011
Top Expert 2016
Commented:
If you echo '<pre>' before printing the decoded JSON object, your output will be easier to read.
Most Valuable Expert 2011
Top Expert 2016
Commented:
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
Zeke RichOwner

Author

Commented:
Did not work:   $requests->data->to['name'];
Zeke RichOwner

Author

Commented:
Thank you Ray_Paseur!

Commented:
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
Zeke RichOwner

Author

Commented:
@mcnute

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

neither worked.
Most Valuable Expert 2011
Top Expert 2016

Commented:
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!

Do more with

Expert Office
Submit tech questions to Ask the Experts™ at any time to receive solutions, advice, and new ideas from leading industry professionals.

Start 7-Day Free Trial