473,396 Members | 1,608 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,396 software developers and data experts.

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.
Aug 6 '07 #1
5 1344
On 6 Sie, 15:54, zebul0n <zebu...@free.frwrote:
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

Aug 6 '07 #2
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
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
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.
Aug 6 '07 #5
On Aug 6, 9:54 am, zebul0n <zebu...@free.frwrote:
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.

Aug 7 '07 #6

This thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

16
by: Japcuh | last post by:
How do you write self modifying code in Java? Japcuh (Just Another Perl C Unix Hacker) http://www.catb.org/~esr/faq/hacker-howto.htm#what_is ..0. ...0 000
1
by: JW | last post by:
Hi, Below is a description of what I would like to do. Would someone tell me if it is possible and if so, how? I have an array (presumably 'large') that is mallocced in a C function and its...
2
by: Joey Martin | last post by:
I am modifying someone else's code and need help with an array: Can someone explain to me what this is doing? PedArray = Split(T_ped,CHR(13)) I know this is creating an array and splitting the...
12
by: pvinodhkumar | last post by:
1) char* p = "Plato"; p = 'r'; // runtime error 2) char c = "Plato"; c = 'i';// ok.Why no runtime here?Why is the contradiction? cout << c << endl;
6
by: MackS | last post by:
Hello everyone I am faced with the following problem. For the first time I've asked myself "might this actually be easier to code in C rather than in python?", and I am not looking at device...
11
by: GaryB | last post by:
Hi Guys, I've been battling with this one for hours - I hope that you can help me! My code modifies the <aon a page, from a standard document link into a link with a tailored onclick event. ...
56
by: subramanian100in | last post by:
The standard allows that we can copy strings onto arg, arg, etc. Why is it allowed ? What can be the maximum length of such a string that is copied ?
1
by: Joe Cool | last post by:
I am attempting to add a function to an application I am working on to modify the JPEG Comment in a Jpeg image file. I can retrieve the JPEG Comment with no problem. The problem is modifying it....
6
by: Arnshea | last post by:
(apologies for the crosspost) I'm working with an MFC based COM object. From C# I'd like to be able to call a method on the COM object that takes a string array and modifies the contents. Is...
6
by: John [H2O] | last post by:
I would like to write a function to write variables to a file and modify a few 'counters'. This is to replace multiple instances of identical code in a module I am writing. This is my approach:...
0
by: ryjfgjl | last post by:
In our work, we often receive Excel tables with data in the same format. If we want to analyze these data, it can be difficult to analyze them because the data is spread across multiple Excel files...
0
BarryA
by: BarryA | last post by:
What are the essential steps and strategies outlined in the Data Structures and Algorithms (DSA) roadmap for aspiring data scientists? How can individuals effectively utilize this roadmap to progress...
1
by: nemocccc | last post by:
hello, everyone, I want to develop a software for my android phone for daily needs, any suggestions?
1
by: Sonnysonu | last post by:
This is the data of csv file 1 2 3 1 2 3 1 2 3 1 2 3 2 3 2 3 3 the lengths should be different i have to store the data by column-wise with in the specific length. suppose the i have to...
0
by: Hystou | last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can...
0
Oralloy
by: Oralloy | last post by:
Hello folks, I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>". The problem is that using the GNU compilers,...
0
jinu1996
by: jinu1996 | last post by:
In today's digital age, having a compelling online presence is paramount for businesses aiming to thrive in a competitive landscape. At the heart of this digital strategy lies an intricately woven...
0
by: Hystou | last post by:
Overview: Windows 11 and 10 have less user interface control over operating system update behaviour than previous versions of Windows. In Windows 11 and 10, there is no way to turn off the Windows...
0
tracyyun
by: tracyyun | last post by:
Dear forum friends, With the development of smart home technology, a variety of wireless communication protocols have appeared on the market, such as Zigbee, Z-Wave, Wi-Fi, Bluetooth, etc. Each...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.