2
Array ( [status] => success 
 [stories] => Array ( 
 [0] => Array ( [name] => John Doe 
 [age] => 23)
 [1] => Array ( [name] => John Doe_1
 [age] => 23)
 [2] => Array ( [name] => John Doe_2
 [age] => 23)
 )
) 

When I try

foreach($stories as $story){ }

It returns an error.

Update: I am trying to pull out the most recent stories from Tweetmeme. Since the json output was long, I shortened it. :)

The PHP Code:

$json=file_get_contents($url);
 $data=json_decode($json,true);
 print_r($data);
 foreach($stories as $story){
 $title = mysql_real_escape_string($story['title']);
 $url_temp = mysql_real_escape_string($story['url']);
 $tweets = intval($story['url_count']);
JSON Output:
Array ( [status] => success 
 [stories] => Array ( [0] => Array ( [title] => Steve Jobs has a Flash enabled iPad! [photo] [url] => http://thenextweb.com/shareables/2010/03/08/flash-ipad/ [media_type] => news [created_at] => 2010年03月08日 09:11:40 [url_count] => 151 [tm_link] => http://tweetmeme.com/story/688449947 [comment_count] => 0 [excerpt] => Steve Jobs has a Flash enabled iPad! http://tnw.to/15mht [photo] [alias] => http://ow.ly/1pVNX0 [retweet] => RT @tweetmeme Steve Jobs has a Flash enabled iPad! [photo] http://ow.ly/1pVNX0 ) [1] => Array ( [title] => Scientists reaffirm theory that giant asteroid killed dinosaurs - CNN.com [url] => http://www.cnn.com/2010/TECH/science/03/08/dinosaurs.asteroid/ [media_type] => news [created_at] => 2010年03月08日 08:12:37 [url_count] => 222 [tm_link] => http://tweetmeme.com/story/688253916 [comment_count] => 0 [excerpt] => (CNN) -- A team of scientists has agreed that a giant asteroid killed off dinosaurs and a majority of other species on Earth more than 65 million years ago. [thumbnail] => http://tweetmeme.s3.amazonaws.com/thumbs/688253916.jpg [alias] => http://ow.ly/1pVG7L [retweet] => RT @tweetmeme Scientists reaffirm theory that giant asteroid killed dinosaurs - CNN.com http://ow.ly/1pVG7L ) [2] => Array ( [title] => The New York Times is hiring 12 techies and a social media whiz [url] => http://venturebeat.com/2010/03/08/nyt-nytimes-hiring/ [media_type] => news [created_at] => 2010年03月08日 10:30:57 [url_count] => 199 [tm_link] => http://tweetmeme.com/story/688719545 [comment_count] => 1 [excerpt] => While pundits climb over each other to predict the death of The New York Times Company, the NYT is looking to hire at least a dozen full-time software engineers and Web designers, plus one social media marketing manager. One opening carries the sexy title of Creative Technologist.The jobs, located in New York City, will focus on expanding content distribution and advertising opportunities in... [thumbnail] => http://tweetmeme.s3.amazonaws.com/thumbs/688719545.jpg [alias] => http://bit.ly/bFqAm7 [retweet] => RT @tweetmeme The New York Times is hiring 12 techies and a social media whiz http://bit.ly/bFqAm7 ) 
Mark Biek
152k54 gold badges160 silver badges201 bronze badges
asked Mar 8, 2010 at 14:26
4
  • I made an error in the quesstion. It is "stories" not "abc". Corrected. :) Commented Mar 8, 2010 at 14:52
  • THE ERROR IS "Invalid argument supplied for foreach() in...." Commented Mar 8, 2010 at 14:55
  • You try to read from $stories but you never define it :-? Commented Mar 8, 2010 at 16:01
  • Gotcha. The correct statement should be foreach($data[stories] as $story){} Thanks for the help guys. Mark and Alvaro, you guys saved my ass. :) Commented Mar 8, 2010 at 16:19

7 Answers 7

3

You simply forgot to initialize the variable $data. Please include this line before the for-loop:

$stories = $data['stories'];

Keep in mind that json_decode itself does not create local variables!

answered Mar 8, 2010 at 18:03
Sign up to request clarification or add additional context in comments.

Comments

2

You could also do it like this:

foreach($stories as $story) {
 if(is_array($story)) {
 foreach($story as $person) {
 print "{$person['name']}, {$person['age']}<br />";
 }
 }
}

That gives you a bit of flexibility if the key containing the sub-array changes or if your data is ever going to have multiple sub-arrays with data in them.

answered Mar 8, 2010 at 14:35

4 Comments

That sounds like whatever variable you're passing to the foreach loop isn't really an array. Posting more code might be helpful. Can you post the code that generates the array?
The code is bulky, so i shortened it. Anyway I will try to post the code.
I am pulling the list of Recent Stories from Tweetmeme.
I think the problem is that your foreach() loop uses the variable $stories but you seem to be decoding the JSON data into the variable $data.
1

Not sure what error does it return, but have you tried

foreach($stories['abc'] as $story){ 
}

instead?

answered Mar 8, 2010 at 14:34

Comments

1

Do you need to reference the variable you have the array in? Something like foreach($json.stories as $story) ...?

answered Mar 8, 2010 at 15:37

Comments

1

foreach ( $whatever_you_named_your_array['stories'] as $story ) {

}

answered Mar 8, 2010 at 17:20

Comments

0
for ($i = 0; $i < count($array1['abc']); $i++)
{
 $current = $array1['abc'][$i];
}

That should do it.

answered Mar 8, 2010 at 14:31

Comments

0

try

foreach ($data AS $story){
 if(is_array($story)) {
 foreach($story as $person) {
 print_r($person);
 }
 }
}
answered Mar 8, 2010 at 16:06

Comments

Your Answer

Draft saved
Draft discarded

Sign up or log in

Sign up using Google
Sign up using Email and Password

Post as a guest

Required, but never shown

Post as a guest

Required, but never shown

By clicking "Post Your Answer", you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.