By using this site, you agree to our updated Privacy Policy and our Terms of Use. Manage your Cookies Settings.
424,854 Members | 1,871 Online
Bytes IT Community
+ Ask a Question
Need help? Post your question and get tips & solutions from a community of 424,854 IT Pros & Developers. It's quick & easy.

adding to arrays

P: n/a
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
Share this Question
Share on Google+
7 Replies


P: n/a
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

P: n/a
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

P: n/a
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

P: n/a
> 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

P: n/a
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

P: n/a
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

P: n/a
Or you could do something like:

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

This discussion thread is closed

Replies have been disabled for this discussion.