2012-02-04 129 views
0

我有這樣的功能:從多個值的陣列創建一個對象

function getPostInfo($query, $fields, $type, $limit, $since, $until, $lang, $stopwords, $identifier) { 
$url = file_get_contents('https://graph.facebook.com/search?q='.spaces($query).'&fields='.$fields.'&limit='.$limit.'&until='.$until); 
$j = json_decode($url); 
    foreach($j->data as $v) { 
    if ($v->type == $type) { 
     $author_id = $v->from->id; 
     $original_id = $v->id; 
     $post_url = getPostUrl($original_id, $author_id); 
     //$description = stopWords($v->message); 
     $description = $v->message; 
     $pub_date = $v->created_time; 
     $post[]= array(
     'author_id' => $author_id, 
     'orginal_id' => $original_id, 
     'post_url' => $post_url, 
     'descritpion' => $description, 
     'pub_date' => $pub_date 
     ); 
    } 
} 
return (object)$post; 
} 

如果我請例如:

$post = getPostInfo($query, $fields, $type, $limit, $since, $until, $lang, $stopwords, $identifier); 

echo $post->pub_date;` 

它返回空..

我試圖剝離函數中$ post數組的[];在這種情況下,它只返回一個值。

相反,它應該是r E打開更多的價值,因爲對象應在上述功能的foreach循環獲得更多的價值..

但它不會發生..

我想也與正常陣列工作,沒有對象,這樣的:

$post = getPostInfo($query, $fields, $type, $limit, $since, $until, $lang, $stopwords, $identifier); 

echo $post['pub_date']; 

在這種情況下,它總是返回NULL ..

我做錯了嗎?

如何知道我得到的東西應該包含更多的值,以便將更多值分配給數組並在需要時將其返回。這就像一個數組的數組...

我有點糊塗..

我喜歡對象的解決方案,但如果有任何其他方式,則更好。

這是我從數組$後得到一個例子:

array(5) { [0]=> array(5) { ["author_id"]=> string(10) "1319929690" ["orginal_id"]=> string(24) "1319929690_2736172406396" ["post_url"]=> string(77) "https://www.facebook.com/permalink.php?story_fbid=2736172406396&id=1319929690" ["descritpion"]=> string(773) "It was only a matter of time. # How do they serve alcoholic drinks on Italian cruise ships? - On the rocks # What vegetables do you get with dinner on Italian cruise ships? - Leeks # What's the fastest way to get off an Italian cruise ship? - Follow the captain # When the captain of the ill fated Costa Concordia was asked if he knew where he was going he replied "off course." # So the captain of the Costa Concordia will soon be in the dock. That's more than can be said for his ship. # The Costa Concordia is probably the most expensive thing to go down in Italy since Berlusconi's last hooker. # What's the difference between the Italian economy and the stricken cruise liner Costa Concordia? Nothing - The bottoms dropped out of both." ["pub_date"]=> string(24) "2012-02-04T14:36:54+0000" } [1]=> array(5) { ["author_id"]=> string(10) "1721122272" ["orginal_id"]=> string(24) "1721122272_1862454417365" ["post_url"]=> string(77) "https://www.facebook.com/permalink.php?story_fbid=1862454417365&id=1721122272" ["descritpion"]=> string(1968) "Thách thà ng nà o quánh tao 1. MỸ: “Tao muốn đánh thằng nà o, là tao đánh thằng đó. Ngoà i ra, tao bao tiá»n súng!†2. NATO: “Mỹ đánh thằng nà o, tao đánh thằng đó!â€. 3. NGA: “Thằng nà o bật tao, tao cắt dầu lá»­a!â€. 4. ISRAEL: “thằng nà o ngấm ngầm muốn đánh tao, tao đánh thằng đó!â€. 5. NHẬT: “thằng nà o đánh tao, tao sẽ bảo Mỹ đánh thằng đó. Nếu chúng mà y không ngừng tấn công, tao cho Maria Ozawa nghỉ việc!â€. 6. TRUNG QUá»C: “Thằng nà o gần tao, tao đánh thằng đó!â€. 7. ÄÀI LOAN: “Thằng nà o đòi đánh tao, tao bảo báo chí chá»­i thằng đó!â€. 8. NAM HÀN: “Thằng nà o định đánh tao, tao tập trận vá»›i thằng Mỹ!â€. 9. BẮC HÀN: “Thằng nà o là m tao bá»±c, tao sẽ đánh thằng Nam Hà n!â€. 10. Berlusconi (ITALIA): “Thằng nà o oánh tao, tao… ngủ vá»›i vợ thằng đó!â€. 11. SINGAPORE : “Thằng nà o đánh tao?!Chắc đếk thằng nà o rảnh mà đi đánh tao!â€. 12. IRAQ : “Thằng nà o đánh tao thì cứ đánh cho đã, chừng nà o mệt thì tá»± Ä‘á»™ng vá»!â€. 13. ARAP SAUDI : “Thằng nà o đánh tao, tao mua thằng đó!†14. Billaden: “Thằng nà o đánh tao, tao khủng bố thằng Mỹ!â€. 15. Liên Hiệp Quốc: “Tao dán cái mác… vùng cấm bay lên thằng nà o, chúng mà y úp sá»t thằng đấy cho tao!â€. 16. CUBA : “Thằng nà o oánh tao, tao cho Việt Nam má»™t mình canh giữ thế giá»›i!â€. 17. VIỆT NAM: “Chá»— nà o có oánh nhau, tao bà y tá» quan ngại sâu sắc. Còn thằng nà o oánh tao, tao tuyên bố chủ quyá»n, tao cắt Ä‘iện luân phiên, sau đó tao… cá»±c lá»±c lên án!â€. 18. THỤY SĨ : Thằng nà o đánh tao, Tao khóa tà i khoản tiá»n quốc gia thằng đó. 19. PHILIPIN : †Thằng nà o đánh tao, muốn hứng bão qua mà đánh. =]]" ["pub_date"]=> string(24) "2012-02-04T13:50:47+0000" } [2]=> array(5) { ["author_id"]=> string(9) "594958865" ["orginal_id"]=> string(27) "594958865_10150583915793866" ["post_url"]=> string(80) "https://www.facebook.com/permalink.php?story_fbid=10150583915793866&id=594958865" ["descritpion"]=> string(717) "# How do they serve alcoholic drinks on Italian cruise ships? - On the rocks # What vegetables do you get with dinner on Italian cruise ships? - Leeks # What's the fastest way to get off an Italian cruise ship? - Follow the captain # When the captain of the ill fated Costa Concordia was asked if he knew where he was going he replied "off course." # So the captain of the Costa Concordia will soon be in the dock. That's more than can be said for his ship. # The Costa Concordia is probably the most expensive thing to go down in Italy since Berlusconi's last hooker. # What's the difference between the Italian economy and the stricken cruise liner Costa Concordia? Nothing - The bottoms dropped out of both." ["pub_date"]=> string(24) "2012-02-04T13:15:45+0000" } [3]=> array(5) { ["author_id"]=> string(10) "1561522855" ["orginal_id"]=> string(24) "1561522855_2516825610916" ["post_url"]=> string(77) "https://www.facebook.com/permalink.php?story_fbid=2516825610916&id=1561522855" ["descritpion"]=> string(410) "Monti "frainteso" sulla monotonia del posto fisso??? Come Berlusconi che smentiva le proprie dichiarazioni mezzora dopo averle fatte??? Ma fatemi il piacere!!! La cosa che più mi fa specie é assistere all'atteggiamento di tanti antiberlusconiani di maniera che ieri si stracciavano le vesti per ogni cazzata che proveniva da Arcore e che oggi sono pronti a digerire politiche ed esternazioni anche peggiori!!" ["pub_date"]=> string(24) "2012-02-04T12:46:50+0000" } [4]=> array(5) { ["author_id"]=> string(9) "749211731" ["orginal_id"]=> string(27) "749211731_10150528401901732" ["post_url"]=> string(80) "https://www.facebook.com/permalink.php?story_fbid=10150528401901732&id=749211731" ["descritpion"]=> string(265) "B. al Financial Times: "Mi farò da parte dalla politica attiva e non ho nessuna intenzione di candidarmi ancora a primo ministro". Bossi: "Se Berlusconi si ritira è risolto il problema". La solida alleanza parlamentare che ha governato l'Italia in questi 20 anni." ["pub_date"]=> string(24) "2012-02-04T11:57:05+0000" } } 

回答

0

你需要調用它像這樣

echo $post[0]["author_id"]; 
echo $post[1]["author_id"]; 

您也可以使用每個循環,如果你需要立即顯示所有值

foreach($post as $p){ 
    echo $p["author_id"]; 
} 
0

我會堅持陣列個人,所以返回與return $post;

對於不同的行,您必須回顯$post[0]['pub_date'];$post[1]['pub_date'];$post[2]['pub_date'];

或者你可以循環

foreach ($post as $post_values){ 
    echo $post_values['pub_date']; 
} 
0

嘗試這樣做服用點這樣

$post[] = (object) array(
      'author_id' => $author_id, 
      'orginal_id' => $original_id, 
      'post_url' => $post_url, 
      'description' => $description, 
      'pub_date' => $pub_date 
    );