arrayUnshiftAssoc() public static method

Prepend an assoc array item as first entry for a given array.
public static arrayUnshiftAssoc ( array &$arr, string $key, mixed $val ) : array
$arr array The array where the value should be prepend
$key string The new array key
$val mixed The value for the new key
return array
Ejemplo n.º 1
0
 public function addField($pointer, $field, array $options = [])
 {
     if ($this->hasField($pointer, $field)) {
         return false;
     }
     $options = ArrayHelper::merge(['name' => null, 'i18n' => false, 'alias' => null, 'plugins' => [], 'i18n' => false, 'extraField' => false], $options);
     // can not unshift non array value, create array for this pointer.
     if (empty($this->_config[$pointer])) {
         $this->_config[$pointer] = [];
     }
     $this->_config[$pointer] = ArrayHelper::arrayUnshiftAssoc($this->_config[$pointer], $field, $options);
     return true;
 }
Ejemplo n.º 2
0
 public function testUnshiftAssoc()
 {
     $arr = ['foo' => 'bar', 'nokey'];
     $unshift = ArrayHelper::arrayUnshiftAssoc($arr, 'before', 'the-value');
     $this->assertEquals(3, count($unshift));
     $i = 0;
     foreach ($unshift as $k => $v) {
         if ($i == 0) {
             $this->assertEquals('the-value', $unshift[$k]);
         }
         $i++;
     }
 }