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; 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-
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.
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 =--
> 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.
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 =--
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.
Or you could do something like:
for ($i=0; $i<$numpoints; $i++) {
$array[] = array($chars[$i] + 0);
} This thread has been closed and replies have been disabled. Please start a new discussion. Similar topics |
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...
|
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...
|
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...
|
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....
|
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
| |
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
|
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...
|
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 =...
|
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.
|
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,...
|
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,...
| |
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...
|
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: 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,...
|
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...
|
by: adsilva |
last post by:
A Windows Forms form does not have the event Unload, like VB6. What one acts like?
|
by: muto222 |
last post by:
How can i add a mobile payment intergratation into php mysql website.
| |
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...
| |