modifying an array

This is a discussion on modifying an array within the PHP Language forums, part of the PHP Programming Forums category; Hello, I have an array : Array ( '2' => '27584.10', '3' => '1008.00', '6' => 11393.55, '7' => ...


Go Back   Usenet Forums > PHP Programming Forums > PHP Language

FAQ Members List Calendar Search Today's Posts Mark Forums Read
  #1 (permalink)  
Old 08-06-2007
zebul0n
 
Posts: n/a
Default modifying an array

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.
Reply With Quote
  #2 (permalink)  
Old 08-06-2007
Marcin 'frodo2000' Molak
 
Posts: n/a
Default Re: modifying an array

On 6 Sie, 15:54, zebul0n <zebu...@free.fr> 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

Reply With Quote
  #3 (permalink)  
Old 08-06-2007
zebul0n
 
Posts: n/a
Default Re: modifying an array

> 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.
Reply With Quote
  #4 (permalink)  
Old 08-06-2007
gosha bine
 
Posts: n/a
Default Re: modifying an array

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
Reply With Quote
  #5 (permalink)  
Old 08-06-2007
zebul0n
 
Posts: n/a
Default Re: modifying an array

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.
Reply With Quote
  #6 (permalink)  
Old 08-07-2007
fssm2666
 
Posts: n/a
Default Re: modifying an array

On Aug 6, 9:54 am, zebul0n <zebu...@free.fr> 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.

Reply With Quote
Reply
Thread Tools Search this Thread
Search this Thread:

Advanced Search
Display Modes

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are Off
[IMG] code is Off
HTML code is Off
Trackbacks are On
Pingbacks are On
Refbacks are On




All times are GMT +1. The time now is 05:50 PM.


Powered by vBulletin® Version 3.7.3
Copyright ©2000 - 2008, Jelsoft Enterprises Ltd.
Content Relevant URLs by vBSEO 3.0.0