I've been trying to push an item to an associative array like this:
$new_input['name'] = array( 'type' => 'text', 'label' => 'First name', 'show' => true, 'required' => true ); array_push($options['inputs'], $new_input);
However, instead of 'name' as the key in adds a number. Is there another way to do it?
Definition and Usage. The array_push() function inserts one or more elements to the end of an array. Tip: You can add one value, or as many as you like. Note: Even if your array has string keys, your added elements will always have numeric keys (See example below).
Given two array arr1 and arr2 and the task is to append one array to another array. Using array_merge function: This function returns a new array after merging the two arrays. $arr1 = array ( "Geeks" , "g4g" );
Associative arrays are used to store key value pairs. For example, to store the marks of different subject of a student in an array, a numerically indexed array would not be the best choice.
$options['inputs']['name'] = $new_input['name'];
Instead of array_push(), use array_merge()
It will merge two arrays and combine their items in a single array.
Example Code -
$existing_array = array('a'=>'b', 'b'=>'c'); $new_array = array('d'=>'e', 'f'=>'g'); $final_array=array_merge($existing_array, $new_array);
Its returns the resulting array in the final_array. And results of resulting array will be -
array('a'=>'b', 'b'=>'c','d'=>'e', 'f'=>'g')
Please review this link, to be aware of possible problems.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With