0

I have an array that looks like this :

Array
(
    [2019] => Array
        (
            [2019] => Array
                (
                    [year] => 2019
                    [amount] => 3269.93
                    [type] => charge
                )

        )

    [2018] => Array
        (
            [2018] => Array
                (
                    [year] => 2018
                    [amount] => 219.25
                    [type] => payout
                )

        )

    [2017] => Array
        (
            [2017] => Array
                (
                    [year] => 2017
                    [amount] => 214.06
                    [type] => charge
                )

        )

)

I tried with array_search() but I don't think it works to accomplish what I want.

According to the manual, array_search() is used to search for a particular value in an array, and if the value is found then it returns its corresponding key. This takes in account that we know what the value we look for is and that we want to return the key. I want to do it in reverse. I know the key but I do not know the value and I want to return the value not the key.

I'm trying to do something similar to MYSQL "select amount where year='2017'" but with an array.

How can I accomplish this? Any idea?

Random
  • 81
  • 7
  • 1
    [The first answer should work in your case.](https://stackoverflow.com/questions/6661530/php-multidimensional-array-search-by-value) – Hugo Moran Aug 24 '19 at 02:17
  • @HugoMoran : According to the manual, `array_search` is used to search for a particular value in an array, and if the value is found then it returns its corresponding key. I want to do it in reverse. I know the key but I do not know the value. I'm trying to do something similar to `MYSQL "select amount where year='2017'"` but with an array. – Random Aug 24 '19 at 18:21

2 Answers2

0

You can filter the array with array_filter

array_filter($array,function ($v){
    return current($v)["amount"] == 3269.93;
});
LF00
  • 27,015
  • 29
  • 156
  • 295
0

I solved my problem using nested foreach. I'm sure there are smarter ways to accomplish end result but works for me.

Array

$data_array='Array
(
    [2019] => Array
        (
            [2019] => Array
                (
                    [year] => 2019
                    [amount] => 3269.93
                    [type] => charge
                )

        )

    [2018] => Array
        (
            [2018] => Array
                (
                    [year] => 2018
                    [amount] => 219.25
                    [type] => payout
                )

        )

    [2017] => Array
        (
            [2017] => Array
                (
                    [year] => 2017
                    [amount] => 214.06
                    [type] => charge
                )

        )

)';

end result for each

    foreach ($data_array as $key => $data_value) { 
    foreach ($data_value as $key => $value) { 

    $sales_year = $value['year'];
    $current_period = date('Y');

    if($sales_year !=$current_period){

     $current_sales_year = $sales_year+1;

    echo 'Previous Year : '.$sales_year
    echo  '<br>';

    echo 'Previous Sales : $'.$value['amount'];
    echo  '<br>';

    echo 'Current Year : '.$current_sales_year
    echo  '<br>';

//THIS FINDS THE AMOUNT(VALUE) OF THE KNOWN YEAR(KEY)
    echo 'Current Sales : $'$data_array[$current_sales_year][$current_sales_year]['amount'];
    echo  '<br>';

    }


    }
    }
Random
  • 81
  • 7