$array = ['name' => 'John', 'age' => 23]; $name = Yii::getAlias(ArrayHelper::getValue($array, 'name')); //Returns "John" $gender = Yii::getAlias(ArrayHelper::getValue($array, 'gender', 'unknown')); //Returns "unknown"
$array = ['name' => 'John', 'age' => 23]; $nameExists = Yii::getAlias(ArrayHelper::keyExists('name', $array)); //Returns true $genderExists = Yii::getAlias(ArrayHelper::keyExists('gender', $array)); //Returns false
class Person { public $name = 'John'; public $age = 23; public $address = ['city' => 'New York', 'state' => 'NY']; } $person = new Person(); $personArray = Yii::getAlias(ArrayHelper::toArray($person, ['name', 'address'])); print_r($personArray); /*Output Array ( [name] => John [address] => Array ( [city] => New York [state] => NY ) ) */In conclusion, the Yii Framework's ArrayHelper class is a useful tool for manipulating arrays in PHP. It is part of the Yii Framework's core library package and provides a range of methods, including those listed above, for working with arrays.