Trying to understand ArrayHelper::getValue

I am struggling to understand the use of the ArrayHelper::getValue.

I am starting to think that I completely mis-understand what it does…

I am getting a set of values from my DB like this…




seasonId     count

   1           9

   2           6



What I want to do is use the value of ‘seasonId’ to get the value of ‘count’.

I need to perform this type of action in many places, and as a quick and dirty hack,

I have used a foreach loop to scan the array. Probably not the best way to do this… :huh:

I have two related sql query results wrapped in another array

Here is the result of print_r




Array

(

    [seatstotal] => Array

        (

            [0] => Array

                (

                    [seasonId] => 1

                    [total] => 22

                )


            [1] => Array

                (

                    [seasonId] => 2

                    [total] => 26

                )


        )


    [seatsfilled] => Array

        (

            [0] => Array

                (

                    [seasonId] => 1

                    [count] => 9

                )


            [1] => Array

                (

                    [seasonId] => 2

                    [count] => 6

                )


        )


)

1



[also, I don’t know what that ‘1’ at the end means…]

I make two loops through this code…




                $fill_str = 'seatsfilled.' . $season['seasonId'] . '.count';

                $total_str = 'seatstotal.' . $season['seasonId'] . '.total';


                echo '<pre>';

                echo print_r( $fill_str );

                echo ' -- ';

                echo print_r( $total_str );

                echo '</pre>';


                $filled = ArrayHelper::getValue($seasoncounts , $fill_str );

                $total = ArrayHelper::getValue($seasoncounts , $total_str );



And I end up with these results…




seatsfilled.1.count1 -- seatstotal.1.total1

61 -- 261


seatsfilled.2.count1 -- seatstotal.2.total1

1 -- 1



It looks like I am retreiving the [1]=>Array values and skipping the [0]=>Array values. But I don’t know if I just don’t know how to use getValue(), or if I am using the wrong function.

Thanks

-John

You’re overcomplicating it. The call is:


public static mixed getValue ( $array, $key, $default = null )

http://www.yiiframework.com/doc-2.0/yii-helpers-basearrayhelper.html#getValue()-detail

The first param is the array, the second param is the key.

So you’d need to set the seasonId as the keys, either manually or using a helper function.

I have tried many variations of this, but either get the default value returned or not a nummber that I want.

I want to feed in a sessionId number an get back either the value of count or value of total. the indexes of [0] and [1] have to be traversed somehow… usually this is what is traversed by foreach(). I don’t have time to dig through the source, so I will just hack another traversal routine.

The docs on this don’t cover my usage.

If you really need to use that array structure you could try and use ArrayHelper::map.


$new = [

    'seatstotal' => ArrayHelper::map($arr['seatstotal'], 'seasonId', 'total'),

    'seatsfilled' => ArrayHelper::map($arr['seatsfilled'], 'seasonId', 'count'),

];


print_r($new);

Where $arr is your original array and $new is your new array. Results in:


Array

(

    [seatstotal] => Array

        (

            [1] => 22

            [2] => 26

        )


    [seatsfilled] => Array

        (

            [1] => 9

            [2] => 6

        )


)

If you want to get the total seats filled for seasonId=2, just use:


var_dump($new['seatsfilled'][2]); // int(6)