473,505 Members | 14,394 Online
Bytes | Software Development & Data Engineering Community
+ Post

Home Posts Topics Members FAQ

adding to arrays

I have an array that contains numbers in string elements , i want to convert
this to integers, i have made a for loop that uses the number of elements in
the array and then adds 0, thereby converting it to an integer.
//$chars is the array of strings

for ($i=0; $i<$numpoints; $i++) {
$array = array($chars[$i] + 0);
}

I dont know though how to add all these elements to the 1 array, it
replaces each element in the array with the new item,

thanks David;
Jul 17 '05 #1
7 3599
David wrote:
I have an array that contains numbers in string elements , i want to convert
this to integers, i have made a for loop that uses the number of elements in
the array and then adds 0, thereby converting it to an integer.
//$chars is the array of strings

for ($i=0; $i<$numpoints; $i++) {
$array = array($chars[$i] + 0);
}

I dont know though how to add all these elements to the 1 array, it
replaces each element in the array with the new item,

thanks David;

I don't completly understand your question!
If your problem is adding element to an array,you can use:

array_push ($name_of_array, value_to_add);

Say me if it is this the problem!
!ciao!
-pilu-
Jul 17 '05 #2
David wrote:
I have an array that contains numbers in string elements , i want to convert
this to integers,...
Why that? PHP is typeless. If you have a variable with a string like
$variable = "5", you'll get 10 if you echo $variable*2.
...i have made a for loop that uses the number of elements in
the array and then adds 0, thereby converting it to an integer.

for ($i=0; $i<$numpoints; $i++) {
$array = array($chars[$i] + 0);
}
argh...
Well, this loop doesn't make sense.
1. example of correct code:

foreach($array as $key => $arrayVal)
{
$array[$key] = $arrayVal + 0;
}

2. To convert Strings to numbers, you should use the function intval().
--> http://www.php.net/intval

3. It would be much more elegant without loop using a callback with
the function array_map().
--> http://www.php.net/array_map

Example using 2. and 3.:

$array = array_map("intval", $array);

4. As mentioned above... If there are only Numbers without any other
characters you don't need to convert it.
thanks David;


You're welcome.
Paul.
Jul 17 '05 #3
David wrote:
I have an array that contains numbers in string elements , i want to
convert this to integers,
PHP will readily treat strings as numbers where appropriate.
<?php echo "7"+"2"; ?> outputs 9
<?php $x = "7"+"2"; var_dump($x); ?> outputs int(9)

But, IMHO, it is good to have the variable type reflect what it's used
for.

i have made a for loop that uses the number of elements in
the array and then adds 0, thereby converting it to an integer.
I think it's better to use foreach() or a array function instead of the
for loop.

//$chars is the array of strings
$array = array(); // initialize (to a blank array) the variable
for ($i=0; $i<$numpoints; $i++) {
$array = array($chars[$i] + 0);
With this construct, $array is reset every time through the loop
// create a new element in $array and initialize it
$array[] = $chars[$i] + 0;
}

I dont know though how to add all these elements to the 1 array, it
replaces each element in the array with the new item,
Ah! you want the sum of all the numbers in $chars[]?

before the loop initialize a variable to hold that sum

$sum = 0;

and inside the loop keep adding to it

for () {
$sum .= (int)$chars[$i];
}

thanks David;


You're welcome

PS

using foreach()

<?php
$chars = array('13', '14', '0', '-15.67');

// initialize $numbers[]
$numbers = array();
foreach ($chars as $value) {
// add a element to $numbers
$numbers[] = (int)$value;
}

// check the results
var_dump($chars);
var_dump($numbers);
?>
--
--= my mail box only accepts =--
--= Content-Type: text/plain =--
--= Size below 10001 bytes =--
Jul 17 '05 #4
> Ah! you want the sum of all the numbers in $chars[]?
before the loop initialize a variable to hold that sum

$sum = 0;
and inside the loop keep adding to it

for () {
$sum .= (int)$chars[$i];
}


ehm... sum with the string operator?
I would use the addition operator "+" to adding values to another value...

But it's easier creating the sum of all array elements:
--> http://www.php.net/array_sum

PHP does have very very much array functions. You should have a look
there, if you're treating with arrays.

--> http://www.php.net/array

Greetz
Paul.
Jul 17 '05 #5
Paul 'piz' Wellner Bou wrote:
Ah! you want the sum of all the numbers in $chars[]?
before the loop initialize a variable to hold that sum

$sum = 0;
and inside the loop keep adding to it

for () {
$sum .= (int)$chars[$i];
}
ehm... sum with the string operator?


Oops

I would use the addition operator "+" to adding values to another value...
That is certainly much better to obtain the desired result :-)

Thanks for the correction (I promise I'll pay better attention next time
[just a small promise though])

But it's easier creating the sum of all array elements:
--> http://www.php.net/array_sum

PHP does have very very much array functions. You should have a look
there, if you're treating with arrays.

--> http://www.php.net/array


Arrays are wonderful!
Time spent there is definitely worth it.
--
--= my mail box only accepts =--
--= Content-Type: text/plain =--
--= Size below 10001 bytes =--
Jul 17 '05 #6
You are a star that worked perfectly, apologies for poor code, i shall
endeavour to think things through more carefully,

Cheers Dave

ps the reason for the whole changing strings to ints was that i have a
postgresql database that holds polygons, unfortunately php doesnt support
this datatype, infact it doesn't even support arrays, so i was getting a
long string, id worked out how to strip it into appropriate chunks, but that
was as far as my brain could go, thanks again


$array = array_map("intval", $array);

4. As mentioned above... If there are only Numbers without any other
characters you don't need to convert it.
thanks David;


You're welcome.
Paul.

Jul 17 '05 #7
Or you could do something like:

for ($i=0; $i<$numpoints; $i++) {
$array[] = array($chars[$i] + 0);
}
Jul 17 '05 #8

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

Similar topics

0
4577
by: Björn | last post by:
Hello, Starting to code a robust routine to add two arrays element by element $new = $a + $b ... I thought that this should already be done and searched CPAN for a sutiable module. Despite...
6
2401
by: Jamie Fryatt | last post by:
Hi everyone, here's what id like to do. I have a table with 2 fields, name and value I need to be able to add multiple records quickly, for example I need to add name value abc 1...
34
4128
by: Adam Hartshorne | last post by:
Hi All, I have the following problem, and I would be extremely grateful if somebody would be kind enough to suggest an efficient solution to it. I create an instance of a Class A, and...
23
31054
by: Rick | last post by:
Hi, This is probably simple byt when you never did pointers and being used to luxery strings like in Delphi or Visual Basic, C can get though. What I'm trying to do is to add chars to a string....
5
15469
by: Troy | last post by:
Hello, I have a dumb question. I have two array objects of type double with 12 elements in each object. I'd like to add the two objects but dont know how to. Any ideas? thanks
6
10782
by: Maheshkumar.R | last post by:
How i can store images in array @ runtime..? What are the possbile ways.. Thankz.. - Mähésh Kumär. R
1
1967
by: mtchatchez | last post by:
I have several 2-dimensional arrays which I want to sum the values of, returning an array that has for each of its values the sum from all the others (if that makes sense?) ... Okay ... maybe an...
11
3128
by: dennis.sprengers | last post by:
Consider the following multi-dimensional array: --------------------------- $arr = array( array(3, 5, 7, 9), array(2, 4, 6, 8), array(1, 3, 5, 7) ); function add_arrays($arr) { for ($row =...
6
6545
by: santiago | last post by:
I guess one cannot do this: arraytot = arraytot + arraydet; So, what's the trick to adding arrays like this? Thanks.
0
7216
marktang
by: marktang | last post by:
ONU (Optical Network Unit) is one of the key components for providing high-speed Internet services. Its primary function is to act as an endpoint device located at the user's premises. However,...
0
7303
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,...
1
7018
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
7471
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...
0
5613
agi2029
by: agi2029 | last post by:
Let's talk about the concept of autonomous AI software engineers and no-code agents. These AIs are designed to manage the entire lifecycle of a software development project—planning, coding, testing,...
0
3187
by: TSSRALBI | last post by:
Hello I'm a network technician in training and I need your help. I am currently learning how to create and manage the different types of VPNs and I have a question about LAN-to-LAN VPNs. The...
0
3176
by: adsilva | last post by:
A Windows Forms form does not have the event Unload, like VB6. What one acts like?
1
754
muto222
by: muto222 | last post by:
How can i add a mobile payment intergratation into php mysql website.
0
407
bsmnconsultancy
by: bsmnconsultancy | last post by:
In today's digital era, a well-designed website is crucial for businesses looking to succeed. Whether you're a small business owner or a large corporation in Toronto, having a strong online presence...

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.