Prepend an assoc array item as first entry for a given array.
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; }
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++; } }