2016-03-06 104 views
0

我想通過這個數據對象在我的視圖中循環。但是,我收到以下錯誤:Trying to get property of non-object。這是我第一次遇到渲染數組/對象的問題......也許這僅僅是星期日的情況......但是我不能得到這個輸出結果foreach。幫幫我? 編輯:我將鍵/值「search_type」添加到返回的數組 - 我認爲這可能會導致問題?PHP - Foreach對象

$results = $twitter->get("users/search", ["q" => $term, "count" => $num, "include_entities" => 'false']); 
$results['search_type'] = 'people'; 

回報:

{ 
    "0": { 
     "id": 1108217089, 
     "id_str": "1108217089", 
     "name": "Marketing", 
     "screen_name": "SocialMedia246", 
     "location": "Canada", 
     "description": "Marketing|PR|Brand Consultant.Lover of Music|Fashion & life.A dynamic young entrepreneur with an unconventional out of the box approach to marketing & business.", 
    }, 
    "1": { 
     ... 
    }, 
    search_type: "people" 

的foreach:

@foreach ($search_results as $result => $profile) 
    <div class="large-3 columns">    
     {{ $profile->id }} 
    </div> 
@endforeach 

最小化vardump,爲search_results:

array:21 [▼ 
    0 => {#433 ▶} 
    1 => {#435 ▶} 
    2 => {#449 ▶} 
    3 => {#455 ▶} 
    4 => {#461 ▶} 
    5 => {#468 ▶} 
    6 => {#472 ▶} 
    7 => {#479 ▶} 
    8 => {#492 ▶} 
    9 => {#505 ▶} 
    10 => {#513 ▶} 
    11 => {#526 ▶} 
    12 => {#531 ▶} 
    13 => {#538 ▶} 
    14 => {#542 ▶} 
    15 => {#548 ▶} 
    16 => {#555 ▶} 
    17 => {#560 ▶} 
    18 => {#564 ▶} 
    19 => {#577 ▶} 
    "search_type" => "people" 
] 
+2

你可以做$ search_results一個的var_dump並保證其的對象,而不是原始JSON字符串? –

+0

剛剛做過 - 這是一個數組 –

+0

將'$ profile-> id'更改爲'$ profile ['id']' –

回答

1

我相信你沒有在文件中讀取正確。

請嘗試以下循環:

<?php 
    $sInFile = 'in3.json'; 
    $sOutFile = 'out3.json'; 

    $sRaw  = file_get_contents($sInFile); 
    $aData = json_decode($sRaw, 1); 

    // $aData = array(); 
    // $aTmp     = array(); 
    // $aTmp[ 'id' ]   = '1108217089'; 
    // $aTmp[ 'id_str' ]  = '1108217089'; 
    // $aTmp[ 'name' ]  = 'Marketing'; 
    // $aTmp[ 'screen_name' ] = 'SocialMedia246'; 
    // $aTmp[ 'location' ] = 'Canada'; 
    // $aTmp[ 'description' ] = 'Marketing'; 
    // $aData[] = (object) $aTmp; 

    // $aTmp     = array(); 
    // $aTmp[ 'id' ]   = '1108217089'; 
    // $aTmp[ 'id_str' ]  = '1108217089'; 
    // $aTmp[ 'name' ]  = 'Marketing'; 
    // $aTmp[ 'screen_name' ] = 'SocialMedia246'; 
    // $aTmp[ 'location' ] = 'Canada'; 
    // $aTmp[ 'description' ] = 'Marketing'; 
    // $aData[] = (object) $aTmp; 


    foreach ($aData as $result => $profile) 
    { 
     var_dump($result); 
     var_dump($profile); 
     var_dump($profile->id); 
    } 
    var_dump($aData); 

    $sNewJson = json_encode($aData); 
    var_dump($sNewJson); 
    file_put_contents($sOutFile, $sNewJson); 

?> 
1

你的問題是,你正在試圖從字符串peopleid財產。

+0

是的,我正要寫......它沒有意義。我將從這個數組中完全刪除search_type鍵 - 省去麻煩。在別處我的邏輯中工作。 –

0

好吧,@potfur在他的評論中提出了一個很好的觀點。我只是將結果保存到people陣列中,而不是像這樣添加密鑰search_type。省去我的麻煩和頭痛。感謝您的幫助,雖然傢伙。這是我應該接受的一個錯誤。

$results['people'] = $twitter->get("users/search", ["q" => $term, "count" => $num, "include_entities" => 'false']); 

vardump是現在(這使得很多更有意義):

array:1 [▼ 
    "people" => array:20 [▶] 
]