I am reading from an excel sheet through php code. There some fields are optional. when i use the library i get the result like this
Array
(
[1] => Test123
[2] => None
[3] => Booster
[6] => Yes
[7] => Yes
[8] => 1
[9] => Unknown
[10] => 21
[11] => http://unknowm.com
)
There you can see index 4 and 5 are missing because the values were not present in the excel file columns. Now i have to merge this array with another array which has named indexes. the length of the second array is 11. But when i merge both a php error is produced saying undefined index 4 and 5. i am doing the work dynamically and i am compelled to use all the indexes of second array. i wnt the first(coming from excel) array indexes which are missing to be filled with empty string before mergin it to the second one. How can i do this? i hope i have provided much information.
You can use
array_fillto create an array with all the keys you want the result to have and use the array addition operator to add those that are missing from the original array:See it in action.
If you need the result to be additionally ordered by key then also use
ksorton it.It’s worthwhile to mention that this technique works very well for non-integer keys too wherever you accept user input that may only partially override some defaults: