如何获取数组中的特定键

时间:2015-09-11 10:36:44

标签: php arrays

我有这样的数组 而我想知道的是例如搜索此数组中的值9375,然后在此键中获取其他值。

Array
(
[0] => Array
    (
        [0] => Afghanistan
        [1] => 93
        [2] => 0.2
        [3] => 2015-06-09
    )

[1] => Array
    (
        [0] => Afghanistan Cdma Afghan Telecom
        [1] => 9375
        [2] => 0.22
        [3] => 2015-03-31
    )

[2] => Array
    (
        [0] => Afghanistan mobile
        [1] => 937
        [2] => 0.158
        [3] => 2015-03-31
    )

我试图获得这样的密钥,但它不起作用

$position = array_search('937', $array);

echo "<td>".$array[$position][0]."</td>";
echo "<td>".$array[$position][1]."</td>";
echo "<td>".$array[$position][2]."</td>";
echo "<td>".$array[$position][3]."</td>";

3 个答案:

答案 0 :(得分:3)

import QtQuick 2.0

QtObject
{
    property var myVariable
    function myFunction() { console.log("emitting signal"); mySignal() }
    signal mySignal
}

答案 1 :(得分:2)

var qualificationIsDifferent = options.qualification_id !== this.activeId;
var isNotActive = this.hasOwnProperty('activeId') && qualificationIsDifferent;
var shouldDoStuff = !this.initialized || isNotActive;

if ( shouldDoStuff ) { ... }

@sarikaya您的完整代码,如果您有任何疑问,请告诉我们。谢谢你

答案 2 :(得分:1)

array_filter非常适合获取大型数组并搜索它以返回仅包含与搜索匹配的值的新数组

$search = 9375;
$result = array_filter(
    $myArray,
    function ($value) use ($search) {
        return in_array($search, $value);
    }
);

如果您只在每个子阵列的第二个元素中查找搜索键:

$search = 9375;
$result = array_filter(
    $myArray,
    function ($value) use ($search) {
        return $value[1] == $search;
    }
);