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
)
4

5 回答 5

2

循环遍历您的数组并将这些年份分配给一个新数组,并且它们的键完好无损。

$years=array();
foreach($yourArray as $key=>$value)
{
   $years[$key]=$value["Culture"]["year"];
}
$years = array_unique($years);
print_r($years);
于 2013-10-07T06:43:23.830 回答
1
$years = [];
foreach($arr as $newarray){
  $years[] = $newarray['Culture']['year'];
}
$years = array_unique($years);

新数组年份现在将保存旧数组中的所有年份。array_unique将摆脱所有重复的年份。

于 2013-10-07T06:44:17.417 回答
1

您可以先使用循环遍历数组,foreach然后使用array_unique获取年份数组:

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

演示!

于 2013-10-07T06:47:45.113 回答
1

我的方法是使用带有匿名函数的array-walk来填充数组,解决方案只需一行代码。

于 2013-10-07T07:24:36.430 回答
0

Its working for me

    foreach($cultures as $row)
    {
        $year[]=$row['Culture']['year'];
    }
    $year = array_unique($year);
    $year = array_values($year);
    echo "<pre>";print_r($year);
于 2013-10-07T06:56:45.590 回答