Solved

Get Data from Array

Posted on 2012-12-24
13
345 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
 
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
Is Your Active Directory as Secure as You Think?

More than 75% of all records are compromised because of the loss or theft of a privileged credential. Experts have been exploring Active Directory infrastructure to identify key threats and establish best practices for keeping data safe. Attend this month’s webinar to learn more.

 
LVL 108

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 108

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 108

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

Is Your Active Directory as Secure as You Think?

More than 75% of all records are compromised because of the loss or theft of a privileged credential. Experts have been exploring Active Directory infrastructure to identify key threats and establish best practices for keeping data safe. Attend this month’s webinar to learn more.

Question has a verified solution.

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

Suggested Solutions

This article covers the basics of the Sass, which is a CSS extension language. You will learn about variables, mixins, and nesting.
Shoutout to Emily Plummer (http://www.experts-exchange.com/members/eplummer26.html) for giving me this article! She did most of it, I just finished it up and posted it for her :)    Introduction In a previous article (http://www.experts-exchang…
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.
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…

911 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

23 Experts available now in Live!

Get 1:1 Help Now