2010-04-22 107 views
0

我有訪問數組中的值麻煩值,數組看起來像這樣,PHP的foreach從數組

Array 
(
    [0] => Array 
     (
      [id] => 1661 
      [code] => 849651318 
      [job_status] => 4 
      [looking_for] => Lorem ipsum 
      [keywords_education] => Derby University 
      [sector_id_csv] => 10,21,9,22,26 
      [last_job_title_1] => Programmer 
      [last_job_employer_1] => HBOS 
      [city] => Bury 
      [expected_salary_level] => LEVEL_2 
      [education_level] => COLLEGE 
      [job_looking_for] => 
      [is_contract] => Y 
      [is_permanent] => N 
      [is_temporary] => Y 
     ) 

) 
Array 
(
    [0] => Array 
     (
      [id] => 402 
      [code] => 849650059 
      [job_status] => 3 
      [looking_for] => Lorem ipsum 
      [keywords_education] => Paris College 
      [sector_id_csv] => 27,22,19,21,12 
      [last_job_title_1] => Programmer 
      [last_job_employer_1] => HSBC 
      [city] => Bury 
      [expected_salary_level] => LEVEL_2 
      [education_level] => COLLEGE 
      [job_looking_for] => 
      [is_contract] => N 
      [is_permanent] => Y 
      [is_temporary] => Y 
     ) 

) 
Array 
(
    [0] => Array 
     (
      [id] => 1653 
      [code] => 849651310 
      [job_status] => 3 
      [looking_for] => Lorem ipsum 
      [keywords_education] => Crewe University 
      [sector_id_csv] => 27,15,19,21,24 
      [last_job_title_1] => Programmer 
      [last_job_employer_1] => ICI 
      [city] => Bury 
      [expected_salary_level] => LEVEL_2 
      [education_level] => UNIVERSITY 
      [job_looking_for] => 
      [is_contract] => N 
      [is_permanent] => Y 
      [is_temporary] => Y 
     ) 

) 

我試圖獲取值的時候,我曾嘗試做以下,

foreach ($result as $rslt) { 
    echo $rslt->id; 
} 

我也試過,

foreach ($result as $rslt) { 
    $rslt['id']; 
} 

但這一切作品,我不知道爲什麼,誰能幫助?

回答

5

要指出的一些東西,希望澄清任何混淆,在你的第一個例子:

foreach ($result as $rslt) { 
    echo $rslt->id; 
} 

箭頭操作符(->)被濫用。它通常用於調用類對象上的方法,在您的情況下,$rslt將是類對象,而id將是一種方法,但情況並非如此。

在你的第二個例子,

foreach ($result as $rslt) { 
    $rslt['id']; 
} 

你幾乎擊中了要害,但你已經忘了打電話給echoprint輸出值到屏幕上。

此外,您$result陣列有一個子數組中的索引0,使需要改變

$rslt['id']; 

到,與echoprint語句一起。

echo $rslt[0]['id']; 
0

我敢肯定有人會p在接下來的0.3秒內收到答案,所以我只是試着用你的方法來幫助你。顯然你知道如何使用print_r(或var_dump)。你有沒有嘗試在你的數組中使用$ rslt?您似乎很難理解$ rslt是什麼。

+0

我在評論5年後,那該怎麼辦? – 2016-03-24 12:52:27