Go Premium for a chance to win a PS4. Enter to Win

x
?
Solved

Get Data from Array

Posted on 2012-12-24
13
Medium Priority
?
354 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
Technology Partners: We Want Your Opinion!

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!

 
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 111

Assisted Solution

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

Accepted Solution

by:
Ray Paseur earned 2000 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 111

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

What does it mean to be "Always On"?

Is your cloud always on? With an Always On cloud you won't have to worry about downtime for maintenance or software application code updates, ensuring that your bottom line isn't affected.

Question has a verified solution.

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

Preface This is the third article about the EE Collaborative Login Project. A Better Website Login System (http://www.experts-exchange.com/A_2902.html) introduces the Login System and shows how to implement a login page. The EE Collaborative Logiā€¦
This article discusses four methods for overlaying images in a container on a web page
This tutorial will teach you the core code needed to finalize the addition of a watermark to your image. The viewer will use a small PHP class to learn and create a watermark.
Learn how to create flexible layouts using relative units in CSS.  New relative units added in CSS3 include vw(viewports width), vh(viewports height), vmin(minimum of viewports height and width), and vmax (maximum of viewports height and width).
Suggested Courses

916 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