phpmultidimensional-arrayarrayiterator

PHP If switch for a multidimensional array with an Unknown


Just curious if there's a way to do this in a If switch

$account = array (
'{unkown value}' => 
    array (
      'accountID' => '4430290',
      'accountStatus' => '1',
      'parentAccountID' => '',
      'offerID' => '746',
      'billingModel' => '2.0',
    ),
);


if(array_pop($account)['billingModel'] == 'SomeValue') {
    // do stuff
}

I'm currently doing this, but thought there might be a better way

        $model = array_pop($accountInfo);
        if ($model['billingModel'] == 'someValue') return false;

Solution

  • Newer versions of php allow you to chain expressions like that. Examples:

    php 5.3: http://codepad.viper-7.com/4zgSxW

    php 5.4: http://codepad.viper-7.com/oGtKqc

    Both use the same code:

    <?php
    $array = array(range(1,5), range(6,10), range(11, 15));
    
    if(array_shift($array)[0] == 1){
        echo "hello";   
    }
    

    5.3 shows a parse error. 5.4 shows the expected output.