459,729 Members | 1,398 Online Need help? Post your question and get tips & solutions from a community of 459,729 IT Pros & Developers. It's quick & easy.

# modifying an array

 P: n/a Hello, I have an array : Array ( '2' ='27584.10', '3' ='1008.00', '6' =11393.55, '7' =888.12 ) which i need to modify to fill missing keys from 0 to 11 with zero value. Like this : Array ( '0' ='0', '1' ='0', '2' ='27584.10', '3' =1008.00', '4' => '0', '5' ='0', '6' ='11393.55', '7' ='888.12', '8' ='0', '9' => '0', '10' ='0', '11' ='0' ) but I couldn't find an array function to fill the missing keys with a zero value. Thank you. Aug 6 '07 #1
5 Replies

 P: n/a On 6 Sie, 15:54, zebul0n '0', '5' ='0', '6' ='11393.55', '7' ='888.12', '8' ='0', '9' => '0', '10' ='0', '11' ='0' ) but I couldn't find an array function to fill the missing keys with a zero value. Thank you. Maybe, you should try prepare own function, for example: \$array = array('2' ='27584.10', '3' ='1008.00', '6' =11393.55, '7' =888.12); for (\$i=0; \$i<12; \$i++){ if (!array_key_exists(\$i)) \$array[\$i] = 0; } Regards, Marcin Aug 6 '07 #2

 P: n/a Maybe, you should try prepare own function, for example: > \$array = array('2' ='27584.10', '3' ='1008.00', '6' =11393.55, '7' =888.12); for (\$i=0; \$i<12; \$i++){ if (!array_key_exists(\$i)) \$array[\$i] = 0; } Regards, Marcin Hello Marcin, Thank you ! I was indeed using your solution but didn't know how to fill the empty value. Instead of using : \$array[\$i] = 0; I was fighting with function like array_map. I was searching for complication, because I wanted to fill the empty values at the right key place. Putting the values at the end of the array was the solution. Now I just have to ksort() the array. Thank you for your help and pointed me to the right direction. Regards. Aug 6 '07 #3

 P: n/a On 06.08.2007 15:54 zebul0n wrote: Hello, I have an array : Array ( '2' ='27584.10', '3' ='1008.00', '6' =11393.55, '7' =888.12 ) which i need to modify to fill missing keys from 0 to 11 with zero value. Like this : Array ( '0' ='0', '1' ='0', '2' ='27584.10', '3' =1008.00', '4' => '0', '5' ='0', '6' ='11393.55', '7' ='888.12', '8' ='0', '9' => '0', '10' ='0', '11' ='0' ) but I couldn't find an array function to fill the missing keys with a zero value. Thank you. try \$b = \$a + array_fill(0, 12, 0); ksort(\$b); print_r(\$b); I'm curious why you need this. -- gosha bine makrell ~ http://www.tagarga.com/blok/makrell php done right ;) http://code.google.com/p/pihipi Aug 6 '07 #4

 P: n/a gosha bine a Ã©crit : On 06.08.2007 15:54 zebul0n wrote: >Hello,I have an array :Array ( '2' ='27584.10', '3' ='1008.00', '6' =11393.55, '7' =888.12 )which i need to modify to fill missing keys from 0 to 11 with zerovalue. Like this :Array ( '0' ='0', '1' ='0', '2' ='27584.10', '3' =1008.00', '4' =>'0', '5' ='0', '6' ='11393.55', '7' ='888.12', '8' ='0', '9' =>'0', '10' ='0', '11' ='0' )but I couldn't find an array function to fill the missing keys with a zerovalue.Thank you. try \$b = \$a + array_fill(0, 12, 0); ksort(\$b); print_r(\$b); I'm curious why you need this. Hello, your example with array_fill is exactly what I needed. Thank you. Aug 6 '07 #5

 P: n/a On Aug 6, 9:54 am, zebul0n '0', '5' ='0', '6' ='11393.55', '7' ='888.12', '8' ='0', '9' => '0', '10' ='0', '11' ='0' ) but I couldn't find an array function to fill the missing keys with a zero value. Thank you. Hello.... In that case, you are using a statement that create an Associative Array. This means that every key is an string. Maybe you must change and define the id as integer without the ' (\$array=value). Ok. Now, you can take some different way: create the array filled woth zero, and later change the values. Check on the PHP manual the information about array_fill() function. (If you want to iterate an array, use: while(list(\$a, \$b)=each(\$array)), where you can verify every position in the \$array, \$a is the key and \$b is the value) Bye. Felipe Silva. Chile. Aug 7 '07 #6

### This discussion thread is closed

Replies have been disabled for this discussion. 