Solved

Get Data from Array

Posted on 2012-12-24
13
350 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
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
  • 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
Creating Instructional Tutorials  

For Any Use & On Any Platform

Contextual Guidance at the moment of need helps your employees/users adopt software o& achieve even the most complex tasks instantly. Boost knowledge retention, software adoption & employee engagement with easy solution.

 
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 110

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 110

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 110

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

Industry Leaders: 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!

Question has a verified solution.

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

Build an array called $myWeek which will hold the array elements Today, Yesterday and then builds up the rest of the week by the name of the day going back 1 week.   (CODE) (CODE) Then you just need to pass your date to the function. If i…
Introduction This article is intended for those who are new to PHP error handling (https://www.experts-exchange.com/articles/11769/And-by-the-way-I-am-New-to-PHP.html).  It addresses one of the most common problems that plague beginning PHP develop…
Viewers will learn about the regular for loop in Java and how to use it. Definition: Break the for loop down into 3 parts: Syntax when using for loops: Example using a for loop:
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…

734 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