modifying an array
am 06.08.2007 15:54:15 von zebul0n
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.
Re: modifying an array
am 06.08.2007 16:04:24 von frodo2000
On 6 Sie, 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.
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
Re: modifying an array
am 06.08.2007 16:44:25 von zebul0n
> 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.
Re: modifying an array
am 06.08.2007 16:44:50 von gosha bine
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
Re: modifying an array
am 06.08.2007 17:29:22 von zebul0n
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 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.
>
>
>
Hello,
your example with array_fill is exactly what I needed.
Thank you.
Re: modifying an array
am 07.08.2007 17:48:39 von fssm2666
On Aug 6, 9:54 am, 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.
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[1]=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.