代码之家  ›  专栏  ›  技术社区  ›  Jagira

如何从该数组中提取值?

  •  2
  • Jagira  · 技术社区  · 14 年前
    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)
           )
    )    
    

    当我尝试

    foreach($stories as $story){ }
    

    它返回一个错误。

    更新:我正在尝试从tweetmeme中提取最新的故事。因为json输出很长,所以我缩短了它。:)

    php代码:

    $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 ) 
    
    7 回复  |  直到 14 年前
        1
  •  3
  •   Johann Philipp Strathausen    14 年前

    您只是忘记初始化变量$data。请在for循环之前包括这一行:

    $stories = $data['stories'];
    

    请记住,json解码本身不会创建局部变量!

        2
  •  2
  •   Mark Biek    14 年前

    你也可以这样做:

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

    如果包含子数组的键发生更改,或者数据中包含多个子数组的数据时,这会给您带来一些灵活性。

        3
  •  1
  •   vava    14 年前

    不确定它会返回什么错误,但您是否尝试过

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

    相反?

        4
  •  1
  •   TMN    14 年前

    需要引用数组所在的变量吗?有点像 foreach($json.stories as $story) ... ?

        5
  •  1
  •   Hans    14 年前

    foreach($无论你把你的数组命名为$story) {

    }

        6
  •  0
  •   Tobias    14 年前
    for ($i = 0; $i < count($array1['abc']); $i++)
    {
        $current = $array1['abc'][$i];
    }
    

    应该可以。

        7
  •  0
  •   Raz    14 年前

    尝试

    foreach ($data AS $story){
        if(is_array($story)) {
            foreach($story as $person) {
                print_r($person);
            }
        }
    }