0

What I want to do

here is the main 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
                    )

            )               
    )

now from this array I want array of year (by key) like:

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

5 Answers 5

2

Loop through your array and assign those years to a new array with their keys intact.

$years=array();
foreach($yourArray as $key=>$value)
{
   $years[$key]=$value["Culture"]["year"];
}
$years = array_unique($years);
print_r($years);
Sign up to request clarification or add additional context in comments.

1 Comment

+1 for using $key=>$value instead of just $value
1

You can first loop through the array with a foreach loop and then use array_unique to get the array of years:

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

Demo!

Comments

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

The new array years will now hold all the years in the old array. array_unique will get rid of all duplicate years.

Comments

1

My way is to use array-walk that takes anonymous function to fill the array, the solution will be in only one line of code.

1 Comment

but we have to create filter function, so ultimately its the same,anyway thanks and upvoted yr answer
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);

2 Comments

what does using array_values twice achieve? Also, this is the same as the above answers.
yes Mr D, your code is right and array_values is here for re-index array, but am changing it, thanks for your answer

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.