简体   繁体   中英

How to get array element by passing another array element ?

I have an array follow

[0] => Array
 (
    [month] => Oct
    [amount] => 1200.00
 )

and how do i get [amount] by passing [month]

You don't. Two options:

  1. Loop:

     foreach ($array as $i) { if ($i['month'] == 'Oct') { echo $i['amount']; } } 
  2. Index the data by month:

     $array = array_combine(array_map(function($i) { return $i['month']; }, $array), $array); echo $array['Oct']['amount']; 
foreach ($arr as $k => $v) {
    if ($v['month'] == $needleMonth) {
        echo $v['amount'] . ' - that`s it';
        break;
    }
}

您必须循环数组并每次测试是否需要的月份== $ arrayElement [$ i] ['month']

foreach ($arr as $k=>$v) {
    if ($v['month']=='Oct') {
        echo $v['amount'];
    }
}
$selectedMonth = 'Oct';

foreach($yourArray as $child){

    if($child['month'] == $selectedMonth){
        echo $child['amount'];
    }

}

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM