yii\helpers\BaseArrayHelper::isAssociative PHP Method

isAssociative() public static method

An array is associative if all its keys are strings. If $allStrings is false, then an array will be treated as associative if at least one of its keys is a string. Note that an empty array will NOT be considered associative.
public static isAssociative ( array $array, boolean $allStrings = true ) : boolean
$array array the array being checked
$allStrings boolean whether the array keys must be all strings in order for the array to be treated as associative.
return boolean whether the array is associative
    public static function isAssociative($array, $allStrings = true)
    {
        if (!is_array($array) || empty($array)) {
            return false;
        }
        if ($allStrings) {
            foreach ($array as $key => $value) {
                if (!is_string($key)) {
                    return false;
                }
            }
            return true;
        } else {
            foreach ($array as $key => $value) {
                if (is_string($key)) {
                    return true;
                }
            }
            return false;
        }
    }

Usage Example

コード例 #1
0
ファイル: Message.php プロジェクト: shershennm/yii2-sendgrid
 /**
  * @param string|array $paramValue ['email' => 'name'] or 'email'
  * @param string $paramType sendGrid var name like cc, bcc, to
  */
 private function addSingleParam($paramValue, $paramType)
 {
     $addFunction = 'add' . ucfirst($paramType);
     if (is_array($paramValue) && BaseArrayHelper::isAssociative($paramValue)) {
         $this->sendGridMessage->{$addFunction}(key($paramValue), current($paramValue));
     } else {
         $this->sendGridMessage->{$addFunction}($paramValue);
     }
 }