如果一个数组默认为真,如何显示他的内容? [英] if an array default is true, how to display his content?

查看:153
本文介绍了如果一个数组默认为真,如何显示他的内容?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

如果我有以下结构的数组:

if I have an array with the following structure:

$currencies = Array (7)
   0 => Array (3)
       id => "11"
       code => "CHF"
       default => "0"
   1 => Array (3)
       id => "13"
       code => "USD"
       default => "0"
   2 => Array (3)
       id => "12"
       code => "EUR"
       default => "1"

我想找回'code'的时候,默认值是'1',是这样的:

I would like to retrieve the 'code' when default is equal to '1', something like:

if($currencies.default == 1 ){
$currency_code = EUR
}

这是我的本意只是一个粗略的例子,我不知道该怎么做。谁能帮我?

That was just a rough example of my intention, I have no idea how to do it. Can anyone help me?

推荐答案

这应该为你工作:

首先你去通过与 array_filter每个元素() 并筛选出所有这些子阵,不具有默认=> 1

在此只是简单的抢列 code 出比前滤波阵列的 array_column() ,如:

After this just simply grab the column code out of the filtered array from before with array_column(), e.g.

<?php

   $result = array_column(array_filter($currencies, function($v){
       return $v["default"] == 1;
   }), "code");

   print_r($result);

?>

输出:

Array
(
    [0] => EUR
)

这篇关于如果一个数组默认为真,如何显示他的内容?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆