PHP - Add Item to Beginning of Associative Array

PHP - add item to beginning of associative array

You could use the union operator:

$arr1 = array('key0' => 'value0') + $arr1;

or array_merge.

Adding an item to an associative array

I think you want $data[$category] = $question;

Or in case you want an array that maps categories to array of questions:

$data = array();
foreach($file_data as $value) {
list($category, $question) = explode('|', $value, 2);

if(!isset($data[$category])) {
$data[$category] = array();
}
$data[$category][] = $question;
}
print_r($data);

How to insert an item at the beginning of an array in PHP?

Use array_unshift($array, $item);

$arr = array('item2', 'item3', 'item4');
array_unshift($arr , 'item1');
print_r($arr);

will give you

Array
(
[0] => item1
[1] => item2
[2] => item3
[3] => item4
)

Insert at start of array a key-value pair in php

This code takes your array noting that it's using integer keys for your array so these will always be the array order, so we convert them to string keys and this prevents the key's acting as the order of the array. we then create the a new array with your value and append all values from your existing array to the end.

note that the product ID is converted to a string again to prevent the integer key from ordering the array.

$ids = array(0 => "bla bla", 1 => "bla bla", 2 => "bla bla", 3 => "bla bla")
foreach($ids as $key => $val){
$key = "$key";
}
unset(current($ids));
$ids = array_merge(array("$product_id" => $catalog_tag), $ids);

how to insert item at the beginning of a associative array in php

Because you are using an associative array, you need to use array_merge to prepend the series data (i.e. data indexed by $name to the beginning of the $data[$d['playingDate']] array. This code will do what you want. Note that I've put the series code at the end to avoid having to check for $data[$d['playingDate']] being set as well as $data[$d['playingDate']][$name].

$data = [];
foreach($allmatches as $d){
$name = $d['div']['divisionName'];
$data[$d['playingDate']]['day']=$d['day'];
$data[$d['playingDate']]['month']=$d['month'];
$data[$d['playingDate']]['isToday']=$d['isToday'];
if (!isset($data[$d['playingDate']][$name])) $data[$d['playingDate']] = array_merge(array($name => array()), $data[$d['playingDate']]);
$data[$d['playingDate']][$name][]=$d;
}

I've created a small demo on 3v4l.org

Push item to associative array in PHP

$options['inputs']['name'] = $new_input['name'];

Add values to an associative array in PHP

Just add it like you would with a non-associative array:

$test = array('chemical' => 'asdasd', 'chemical_hazards' => 'ggggg'); //init
$test['solution'] = 'good';

How to insert element into arrays at specific position?

array_slice() can be used to extract parts of the array, and the union array operator (+) can recombine the parts.

$res = array_slice($array, 0, 3, true) +
array("my_key" => "my_value") +
array_slice($array, 3, count($array)-3, true);

This example:

$array = array(
'zero' => '0',
'one' => '1',
'two' => '2',
'three' => '3',
);
$res = array_slice($array, 0, 3, true) +
array("my_key" => "my_value") +
array_slice($array, 3, count($array) - 1, true) ;
print_r($res);

gives:


Array
(
[zero] => 0
[one] => 1
[two] => 2
[my_key] => my_value
[three] => 3
)

How to solve this php associative array push

Maybe this solve your problem:

$storedItem['produ'][] = [$size, $quantity];

Add element to PHP associative array in loop

Found it - I needed to use a reference to $model in order to update it

// Loop thru model entries
$modelList = json_decode($_POST["modelList"], TRUE);
foreach($modelList as &$model) {

(do some work)

// Add the new element
$model['dbID'] = $newID;
}
unset($model);


Related Topics



Leave a reply



Submit