PHP If switch for a multidimensional array with an Unknown
-
14-07-2021 - |
题
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;
解决方案
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.
其他提示
If you just want to do something if subarray element is with some value , then you must actually look into array_search()
不隶属于 StackOverflow