0

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.

Industrial Themes
  • 557
  • 2
  • 9
  • 26
  • 1
    You will have to loop over `$arr['EE']` and test each value whether `$val['P']` and `$val['T']` equals what you expect, one way or another. There's no direct access possible, unless you re-index the array that way or build a secondary index in which you look up the direct index. – deceze Mar 06 '18 at 14:31
  • This is the route I'm taking in the mean time. You have confirmed that I'm not crazy in my thinking. – Industrial Themes Mar 06 '18 at 14:58

1 Answers1

2

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;
Eddie
  • 26,593
  • 6
  • 36
  • 58
  • Do you think this would be faster than using deceze's method of just looping over $arr['EE'] each time and evaluating for those values? – Industrial Themes Mar 06 '18 at 15:01
  • Based on this, https://stackoverflow.com/questions/18144782/performance-of-foreach-array-map-with-lambda-and-array-map-with-static-function – Eddie Mar 06 '18 at 15:12
  • If you want faster perforamce, you can use the classic `foreach`. I added an example – Eddie Mar 06 '18 at 15:12