简体   繁体   中英

Return a sub array with two specific key/value pairs

Consider the following array returned from an 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
                    )
            )
    )

I want to get the C value from the sub array where P is 1.5 and T is 9. Obviously if I knew this would always be in sub array with index 5 I could just do this:

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

But 5 will not always be the index of that particular array. So I'm looking for something more along the lines of:

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

This is within a loop that processes thousands of these arrays, so performance is definitely a consideration. In other words, I don't want to do a bunch of nested loops to find that value - I'm looking for a built-in PHP function for this (or a combination thereof) if it exists.

You can also use 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;

This will result to:

2.13

Other option: You can use 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>";

This will result to

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

)

So accessing C is $result[0]["C"]

Doc: array_filter


Or for performance, you can use foreach loop

$p = 1.5;
$t = 9;

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

echo $result;

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM