472,958 Members | 2,228 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

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

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 3569
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
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
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
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
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
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
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
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
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
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.
2
by: DJRhino | last post by:
Was curious if anyone else was having this same issue or not.... I was just Up/Down graded to windows 11 and now my access combo boxes are not acting right. With win 10 I could start typing...
2
isladogs
by: isladogs | last post by:
The next Access Europe meeting will be on Wednesday 4 Oct 2023 starting at 18:00 UK time (6PM UTC+1) and finishing at about 19:15 (7.15PM) The start time is equivalent to 19:00 (7PM) in Central...
0
tracyyun
by: tracyyun | last post by:
Hello everyone, I have a question and would like some advice on network connectivity. I have one computer connected to my router via WiFi, but I have two other computers that I want to be able to...
2
by: giovanniandrean | last post by:
The energy model is structured as follows and uses excel sheets to give input data: 1-Utility.py contains all the functions needed to calculate the variables and other minor things (mentions...
4
NeoPa
by: NeoPa | last post by:
Hello everyone. I find myself stuck trying to find the VBA way to get Access to create a PDF of the currently-selected (and open) object (Form or Report). I know it can be done by selecting :...
0
isladogs
by: isladogs | last post by:
The next Access Europe meeting will be on Wednesday 1 Nov 2023 starting at 18:00 UK time (6PM UTC) and finishing at about 19:15 (7.15PM) Please note that the UK and Europe revert to winter time on...
3
by: nia12 | last post by:
Hi there, I am very new to Access so apologies if any of this is obvious/not clear. I am creating a data collection tool for health care employees to complete. It consists of a number of...
0
NeoPa
by: NeoPa | last post by:
Introduction For this article I'll be focusing on the Report (clsReport) class. This simply handles making the calling Form invisible until all of the Reports opened by it have been closed, when it...
0
isladogs
by: isladogs | last post by:
The next online meeting of the Access Europe User Group will be on Wednesday 6 Dec 2023 starting at 18:00 UK time (6PM UTC) and finishing at about 19:15 (7.15PM). In this month's session, Mike...

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.