返回具有两个特定键/值对的子数组

时间:2018-03-06 14:28:14

标签: php arrays

考虑从API返回的以下数组:

$arr = Array
    (
        [A] => 
        [C] => 
        [D] => 
        [EE] => Array
            (
                [0] => Array
                    (
                        [B] => 
                        [C] => 2.06
                        [O] => 
                        [P] => 0
                        [T] => 1
                    )

                [1] => Array
                    (
                        [B] => 
                        [C] => 2.56
                        [O] => 
                        [P] => 0
                        [T] => 2
                    )

                [2] => Array
                    (
                        [B] => 
                        [C] => 4.94
                        [O] => 
                        [P] => 0
                        [T] => 3
                    )

                [3] => Array
                    (
                        [B] => 
                        [C] => 1.42
                        [O] => 
                        [P] => 1
                        [T] => 9
                    )

                [4] => Array
                    (
                        [B] => 
                        [C] => 2.83
                        [O] => 
                        [P] => 1
                        [T] => 10
                    )

                [5] => Array
                    (
                        [B] => 
                        [C] => 2.13
                        [O] => 
                        [P] => 1.5
                        [T] => 9
                    )

                [6] => Array
                    (
                        [B] => 
                        [C] => 1.7
                        [O] => 
                        [P] => 1.5
                        [T] => 10
                    )
            )
    )

我想从子数组得到C值,其中P是1.5,T是9.显然,如果我知道这将总是在索引为5的子数组中,我可以这样做:

$arr['EE'][5]['C'];

但是5并不总是该特定数组的索引。所以我正在寻找更多的内容:

$arr['EE'][...P is 1.5 and T is 9...]['C'];

这是一个处理数千个这些数组的循环,所以性能绝对是一个考虑因素。换句话说,我不想做一堆嵌套循环来找到这个值 - 如果它存在,我正在为此(或其组合)寻找内置的PHP函数。

1 个答案:

答案 0 :(得分:2)

您也可以使用array_reduce

$arr = array
(
    "A" => "",
    "C" => "",
    "D" => "",
    "EE" => array
        (
            "0" => array
                (
                    "B" => "",
                    "C" => 2.06,
                    "O" => "",
                    "P" => 0,
                    "T" => 1,
                ),

            "1" => array
                (
                    "B" => "",
                    "C" => 2.56,
                    "O" => "",
                    "P" => 0,
                    "T" => 2,
                ),

            "2" => array
                (
                    "B" => "",
                    "C" => 4.94,
                    "O" => "",
                    "P" => 0,
                    "T" => 3,
                ),

            "3" => array
                (
                    "B" => "",
                    "C" => 1.42,
                    "O" => "",
                    "P" => 1,
                    "T" => 9,
                ),

            "4" => array
                (
                    "B" => "",
                    "C" => 2.83,
                    "O" => "",
                    "P" => 1,
                    "T" => 10,
                ),

            "5" => array
                (
                    "B" => "",
                    "C" => 2.13,
                    "O" => "",
                    "P" => 1.5,
                    "T" => 9,
                ),

            "6" => array
                (
                    "B" => "",
                    "C" => 1.7,
                    "O" => "",
                    "P" => 1.5,
                    "T" => 10,
                ),
        )
);

$p = 1.5;
$t = 9;

$result = array_reduce( $arr["EE"], function( $c, $v ) use ($p,$t) {
    if ( $v["P"] == $p && $v["T"] == $t ) $c = $v["C"];     
    return $c;
}, "" );

echo $result;

这将导致:

2.13

其他选项:您可以使用array_filter

$p = 1.5;
$t = 9;

$result = array_filter( $arr["EE"], function( $v ) use ($p,$t) {
    return $v["P"] == $p && $v["T"] == $t;      
} );

$result = array_values( $result );

echo "<pre>";
print_r( $result );
echo "</pre>";

这将导致

Array
(
    [0] => Array
        (
            [B] => 
            [C] => 2.13
            [O] => 
            [P] => 1.5
            [T] => 9
        )

)

因此访问C$result[0]["C"]

Doc:array_filter

或者为了提高性能,您可以使用foreach循环

$p = 1.5;
$t = 9;

$result = "";
foreach( $arr["EE"] as $v ) {
    if ( $v["P"] == $p && $v["T"] == $t ) $result = $v["C"];
}

echo $result;