473,770 Members | 4,355 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 3610
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("intv al", $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($numbe rs);
?>
--
--= 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("intv al", $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
4602
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 Math:::Matrix I could not realy find much and the ::Matrix module seemed to be more that I needed...
6
2413
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 abc 2 abc 3
34
4176
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 "push_back" a copy of this into a vector V. This is repeated many times in an iterative process. Ok whenever I "push_back" a copy of Class A, I also want to assign a pointer contained in an exisiting instance of a Class B to this
23
31089
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. I looked in the string.h file but I didn't find that kind of function (that string library is more than 7 years old) so I decided to write that function on myself. It seems to work although when I'm trying to do printf's between those actions I...
5
15490
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
10798
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
1981
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 example will make it clearer! ;-) $array1 = array(array(1,1,1), array(1,1,1), array(1,1,1)); $array2 = array(array(2,2,2), array(0,0,0), array(3,3,3)); ... So is there a clever / fast way to add up the values of the two arrays above to return the...
11
3166
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 = 0; $row < count($arr); $row++) {
6
6561
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
9602
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, people are often confused as to whether an ONU can Work As a Router. In this blog post, we’ll explore What is ONU, What Is Router, ONU & Router’s main usage, and What is the difference between ONU and Router. Let’s take a closer look ! Part I. Meaning of...
0
9439
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 effortlessly switch the default language on Windows 10 without reinstalling. I'll walk you through it. First, let's disable language synchronization. With a Microsoft account, language settings sync across devices. To prevent any complications,...
0
10071
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 tapestry of website design and digital marketing. It's not merely about having a website; it's about crafting an immersive digital experience that captivates audiences and drives business growth. The Art of Business Website Design Your website is...
1
10017
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 Update option using the Control Panel or Settings app; it automatically checks for updates and installs any it finds, whether you like it or not. For most users, this new feature is actually very convenient. If you want to control the update process,...
0
9882
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 protocol has its own unique characteristics and advantages, but as a user who is planning to build a smart home system, I am a bit confused by the choice of these technologies. I'm particularly interested in Zigbee because I've heard it does some...
0
8905
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, and deployment—without human intervention. Imagine an AI that can take a project description, break it down, write the code, debug it, and then launch it, all on its own.... Now, this would greatly impact the work of software developers. The idea...
1
7431
isladogs
by: isladogs | last post by:
The next Access Europe User Group meeting will be on Wednesday 1 May 2024 starting at 18:00 UK time (6PM UTC+1) and finishing by 19:30 (7.30PM). In this session, we are pleased to welcome a new presenter, Adolph Dupré who will be discussing some powerful techniques for using class modules. He will explain when you may want to use classes instead of User Defined Types (UDT). For example, to manage the data in unbound forms. Adolph will...
1
3987
by: 6302768590 | last post by:
Hai team i want code for transfer the data from one system to another through IP address by using C# our system has to for every 5mins then we have to update the data what the data is updated we have to send another system
3
2832
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 can significantly impact your brand's success. BSMN Consultancy, a leader in Website Development in Toronto offers valuable insights into creating effective websites that not only look great but also perform exceptionally well. In this comprehensive...

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.