dosrmo0442 2018-06-20 12:15
浏览 80

如何使用特定列名作为数组索引获取特定列的结果?

I have an array , I want to get result by passing currency parameter . E.g $array is the variable containing array.

I want to get by this way $array['EUR']['rate']

How can i get by this way. Please Help

Array
(
    [0] => Array
        (
            [id] => 1
            [name] => Andorra
            [code] => AD
            [currency] => EUR
            [phone] => +376
            [rate] => 5727.21
        )

[1] => Array
    (
        [id] => 2
        [name] => United Arab Emirates
        [code] => AE
        [currency] => AED
        [phone] => +971
        [rate] => 24341.9
    )
)

I want to get

  • 写回答

3条回答 默认 最新

  • dougai6464 2018-06-20 12:19
    关注
    foreach ($array as $currency) {
        if ($currency['currency'] == 'EUR') {
            echo $currency['rate'];
        }
    }
    

    You may want to format the array differently to be able to access data directly without looping. So you could use currency as key.

    You can reformat the array like this:

    $newArray = [];
    foreach ($array as $currency) {
        $newArray[$currency['currency']] = $currency;
    }
    

    Then access $newArray['EUR']['rate']

    评论

报告相同问题?

悬赏问题

  • ¥30 线性代数的问题,我真的忘了线代的知识了
  • ¥15 有谁能够把华为matebook e 高通骁龙850刷成安卓系统,或者安装安卓系统
  • ¥188 需要修改一个工具,懂得汇编的人来。
  • ¥15 livecharts wpf piechart 属性
  • ¥20 数学建模,尽量用matlab回答,论文格式
  • ¥15 昨天挂载了一下u盘,然后拔了
  • ¥30 win from 窗口最大最小化,控件放大缩小,闪烁问题
  • ¥20 易康econgnition精度验证
  • ¥15 msix packaging tool打包问题
  • ¥28 微信小程序开发页面布局没问题,真机调试的时候页面布局就乱了