php数组输出其中索引为a的值,PHP-根据值查找数组索引

这篇博客讨论了如何在PHP中高效地遍历高度嵌套的数组,查找指定字段`f2`中具有特定值`this`的索引。通过示例代码展示了使用`foreach`循环检查每个子数组的`f2`字段,从而找到目标索引0的方法。
摘要由CSDN通过智能技术生成

我正在尝试根据存储在其中的值来查找索引.

通常这很容易,但是我正在使用的数组是高度嵌套的.每个索引0,1,2都有字段f1,f2,f3.我试图找到哪个索引0,1,2在其f2字段中具有存储在其中的值.在这种情况下,它的索引为0.所以这就是我要寻找的输出. PHP中有一个巧妙的技巧可以有效地做到这一点吗?

$somearray[0][f1] = "not this";

$somearray[0][f2] = "this";

$somearray[0][f3] = "not this";

$somearray[1][f1] = "not this";

$somearray[1][f2] = "not this";

$somearray[1][f3] = "not this";

$somearray[2][f1] = "not this";

$somearray[2][f2] = "not this";

$somearray[2][f3] = "not this";

解决方法:

In this case, it’s index 0. So that’s

the output I’m looking for.

$somearray[0]['f1'] = "not this";

$somearray[0]['f2'] = "this";

$somearray[0]['f3'] = "not this";

$somearray[1]['f1'] = "not this";

$somearray[1]['f2'] = "not this";

$somearray[1]['f3'] = "not this";

$somearray[2]['f1'] = "not this";

$somearray[2]['f2'] = "not this";

$somearray[2]['f3'] = "not this";

foreach($somearray as $key => $value)

{

if($value['f2'] === 'this')

{

echo $key; // find out the key

}

}

输出:

0

标签:arrays,php

来源: https://codeday.me/bug/20191209/2097161.html

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值