2013-10-07 56 views
0

我想做如何使用按鍵從陣列中獲取價值陣列

這裏什麼是主陣列

Array 
    (
     [0] => Array 
      (
       [Culture] => Array 
        (
         [id] => 8 
         [title] => test123 
         [description] => test123 
         [year] => 2012 
         [photo] => test123.JPG       
         [datetime] => 0000-00-00 00:00:00 
         [status] => 0 
        ) 

      ) 

     [1] => Array 
      (
       [Culture] => Array 
        (
         [id] => 9 
         [title] => here title 
         [description] => here title 
         [year] => 2012 
         [photo] => here.JPG       
         [datetime] => 0000-00-00 00:00:00 
         [status] => 0 
        ) 

      ) 

     [2] => Array 
      (
       [Culture] => Array 
        (
         [id] => 11 
         [title] => here title 2 
         [description] => here title 2 
         [year] => 2012 
         [photo] => here.JPG       
         [datetime] => 0000-00-00 00:00:00 
         [status] => 0 
        ) 

      ) 

     [3] => Array 
      (
       [Culture] => Array 
        (
         [id] => 12 
         [title] => here title 3 
         [description] => here title 3 
         [year] => 2013 
         [photo] => here.JPG       
         [datetime] => 0000-00-00 00:00:00 
         [status] => 0 
        ) 

      ) 

     [4] => Array 
      (
       [Culture] => Array 
        (
         [id] => 13 
         [title] => here title 4 
         [description] => here title 4 
         [year] => 2014 
         [photo] => here.JPG       
         [datetime] => 0000-00-00 00:00:00 
         [status] => 0 
        ) 

      ) 

     [5] => Array 
      (
       [Culture] => Array 
        (
         [id] => 14 
         [title] => here title 5 
         [description] => here title 5 
         [year] => 2015 
         [photo] => here.JPG       
         [datetime] => 0000-00-00 00:00:00 
         [status] => 0 
        ) 

      )    
    ) 

現在從這個數組我想今年的陣列(通過鍵) 像:

Array 
(
[0]=>2012 
[1]=>2013 
[2]=>2014 
[3]=>2015 
) 

回答

0

它的工作對我來說

foreach($cultures as $row) 
    { 
     $year[]=$row['Culture']['year']; 
    } 
    $year = array_unique($year); 
    $year = array_values($year); 
    echo "<pre>";print_r($year); 
+0

什麼使用array_values兩次實現?另外,這與上述答案相同。 –

+0

是D先生,你的代碼是正確的,array_values在這裏是重新索引數組,但是我正在改變它,謝謝你的回答 –

2

循環遍歷數組,並將這些年份指定給一個新數組,並且鍵完好無損。

$years=array(); 
foreach($yourArray as $key=>$value) 
{ 
    $years[$key]=$value["Culture"]["year"]; 
} 
$years = array_unique($years); 
print_r($years); 
+0

+1使用'$ key => $ value'而不是'$ value' – geomagas

1
$years = []; 
foreach($arr as $newarray){ 
    $years[] = $newarray['Culture']['year']; 
} 
$years = array_unique($years); 

新陣列多年來將持有舊陣列中的所有歲月。 array_unique將擺脫所有重複的年份。

1

您可以通過與foreach循環數組,然後使用array_unique獲得的年數組第一個循環:

$years = array(); 
foreach ($records as $record) { 
    $years[] = $record['Culture']['year']; 
} 
$years = array_unique($years); 

Demo!

1

我方法是使用array-walk,它使用匿名函數來填充數組,解決方案將僅在一行代碼。

+0

但我們必須創建過濾器函數,所以最終它的相同,無論如何感謝和upvoted年回答 –