2013-10-07 104 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年回答 –