簡體   English   中英

如何使用數組中的鍵獲取值數組

[英]how to get value array using key from array

我想做的事

這是主要的陣列

   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
)

遍歷數組,並將這些年份分配給鍵不變的新數組。

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

現在,新陣列年份將保留舊陣列中的所有年份。 array_unique將刪除所有重復的年份。

您可以先使用foreach循環foreach數組,然后使用array_unique獲得年份數組:

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

演示!

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

它為我工作

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

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM