Connecting Tech Pros Worldwide Help | Site Map

array help

kurrent@gmail.com
Guest
 
Posts: n/a
#1: Oct 17 '06
i'm still new to php and programming and was hoping to get some help on
a few questions i haven't had success in answering myself. I
successfully created my first very simple script to accomplish a task I
wanted, but not completely. see my code below:

<?php

//my first ever php program simply designed to automatically adjust new
prices of my properties

//current property prices
$prices = array('250000', '250000', '350000');

//percentage of how much to add to current prices
$increase_rate = '0.015'; // 1.15 %

//add new percentage to each price
foreach ($prices as &$newprice) {
$newprice = $newprice + ($newprice * $increase_rate);
}

//print out all new prices
for ($key = 0; $key < 3; $key++) {
print "$prices[$key]<br />";
}

?>

(forgive me if these questions seem completely common sense or stated
clearly in the php manual. I have done reading into these all day and
have gave up and so here I am)

HELP QUESTIONS:
---------------


1.) How would I make a copy of the old prices array that
was manipulated?

e.g. -echo the following structure:
set an array of values, then do a function to
manipulate the values, then show the old
values in comparison of the new values
(basically, show that $253,750 used to be
$250,000)


2.) a) What if i have commas in my prices by default?

e.g. = array('349,502' '234,995');

b) How would I go about processing comma values
received via a form that a user entered or from .CSV
file that looked like (132,500, 123,340, 345,959,
244,459)?
c) How would I add a comma into my printed outputed values?

3.) There must be a better way to print out all the values
from an array but constructing a loop came to mind and
beats doing it one by one. Is there better way?

.:[ ikciu ]:.
Guest
 
Posts: n/a
#2: Oct 17 '06

re: array help


Hmm Uzytkownik <kurrent@gmail.comwrote:
Quote:
1.) How would I make a copy of the old prices array that
was manipulated?
$copy = $prices;
Quote:
2.) a) What if i have commas in my prices by default?
str_replace(',','.',$newprice) + 9....
Quote:
>
b) How would I go about processing comma values
received via a form that a user entered or from .CSV
file that looked like (132,500, 123,340, 345,959,
244,459)?
str_replace(',','.',$newprice) + 9....

Quote:
c) How would I add a comma into my printed outputed values?
str_replace('.',',',$newprice) + 9....

Quote:
3.) There must be a better way to print out all the values
from an array but constructing a loop came to mind and
beats doing it one by one. Is there better way?
you can do it this way what you do


--
~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
Ikciu | gg: 718845 | yahoo: ikciu_irsa | www: www.e-irsa.pl

2be || !2be $this =mysql_query();


Rik
Guest
 
Posts: n/a
#3: Oct 18 '06

re: array help


kurrent@gmail.com wrote:
Quote:
i'm still new to php and programming and was hoping to get some help
on a few questions i haven't had success in answering myself. I
successfully created my first very simple script to accomplish a task
I wanted, but not completely. see my code below:
>
<?php
>
//my first ever php program simply designed to automatically adjust
new prices of my properties
>
//current property prices
$prices = array('250000', '250000', '350000');
>
//percentage of how much to add to current prices
$increase_rate = '0.015'; // 1.15 %
>
//add new percentage to each price
foreach ($prices as &$newprice) {
$newprice = $newprice + ($newprice * $increase_rate);
}
>
//print out all new prices
for ($key = 0; $key < 3; $key++) {
print "$prices[$key]<br />";
}
>
Quote:
>>
<pre>
<?php
$prices = array('250000', '250000', '350000');
$increase_rate = '0.015';

function increase_price(&$item,$key,$rate){
$item = number_format(floatval(str_replace(',','',$item)) * (1 +
$rate),0,'',',');
}
$oldprices = $prices;
array_walk($prices,'increase_price',$increase_rate );
$changes = array_map(null,$oldprices,$prices);
print_r($changes);
?>
</pre>
Quote:
e.g. -echo the following structure:
set an array of values, then do a function to
manipulate the values, then show the old
values in comparison of the new values
(basically, show that $253,750 used to be
$250,000)
print_r() will show you the arrays, if it's just to check for the
programmer, for a form/UI offcourse it will be better to give it some sort
of layout, vprintf() is very handy for this.
Quote:
2.) a) What if i have commas in my prices by default?
>
e.g. = array('349,502' '234,995');
Removed by str_replace();
Quote:
b) How would I go about processing comma values
received via a form that a user entered or from .CSV
file that looked like (132,500, 123,340, 345,959,
244,459)?
str_replace could be used for this, but if you're not absolutely sure about
your data, I'd use $price = preg_replace('/[^0-9]/','',$price); (on the
condition you never use decimals, otherwise you might want to use
'/[^0-9\.]/' instead).
Quote:
c) How would I add a comma into my printed outputed values?
number_format(), or maybe in this case money_format();
Quote:
3.) There must be a better way to print out all the values
from an array but constructing a loop came to mind and
beats doing it one by one. Is there better way?
Why not a foreach() loop there?
Anyway, print_r() and var_dump() are your friends while building, for a
form/html-page for other users you almost certainly will have to use a loop
or a str_repeat()/vprintf() combo.

Grtz,
--
Rik Wasmus


kurrent@gmail.com
Guest
 
Posts: n/a
#4: Oct 18 '06

re: array help



Rik wrote:
Quote:
kurrent@gmail.com wrote:
Quote:
i'm still new to php and programming and was hoping to get some help
on a few questions i haven't had success in answering myself. I
successfully created my first very simple script to accomplish a task
I wanted, but not completely. see my code below:

<?php

//my first ever php program simply designed to automatically adjust
new prices of my properties

//current property prices
$prices = array('250000', '250000', '350000');

//percentage of how much to add to current prices
$increase_rate = '0.015'; // 1.15 %

//add new percentage to each price
foreach ($prices as &$newprice) {
$newprice = $newprice + ($newprice * $increase_rate);
}

//print out all new prices
for ($key = 0; $key < 3; $key++) {
print "$prices[$key]<br />";
}
Quote:
>
<pre>
<?php
$prices = array('250000', '250000', '350000');
$increase_rate = '0.015';
>
function increase_price(&$item,$key,$rate){
$item = number_format(floatval(str_replace(',','',$item)) * (1 +
$rate),0,'',',');
}
$oldprices = $prices;
array_walk($prices,'increase_price',$increase_rate );
$changes = array_map(null,$oldprices,$prices);
print_r($changes);
?>
</pre>
>
Quote:
e.g. -echo the following structure:
set an array of values, then do a function to
manipulate the values, then show the old
values in comparison of the new values
(basically, show that $253,750 used to be
$250,000)
>
print_r() will show you the arrays, if it's just to check for the
programmer, for a form/UI offcourse it will be better to give it some sort
of layout, vprintf() is very handy for this.
>
Quote:
2.) a) What if i have commas in my prices by default?

e.g. = array('349,502' '234,995');
>
Removed by str_replace();
>
Quote:
b) How would I go about processing comma values
received via a form that a user entered or from .CSV
file that looked like (132,500, 123,340, 345,959,
244,459)?
>
str_replace could be used for this, but if you're not absolutely sure about
your data, I'd use $price = preg_replace('/[^0-9]/','',$price); (on the
condition you never use decimals, otherwise you might want to use
'/[^0-9\.]/' instead).
>
Quote:
c) How would I add a comma into my printed outputed values?
>
number_format(), or maybe in this case money_format();
>
Quote:
3.) There must be a better way to print out all the values
from an array but constructing a loop came to mind and
beats doing it one by one. Is there better way?
>
Why not a foreach() loop there?
Anyway, print_r() and var_dump() are your friends while building, for a
form/html-page for other users you almost certainly will have to use a loop
or a str_repeat()/vprintf() combo.
>
Grtz,
--
Rik Wasmus

Thanks you very much for your detailed response Rik. You're
explanations are going to keep me busy tonight learning them throughly
and help me out a great deal in the process. Much appreciated for you
to take the time to answer these questions!

Closed Thread


Similar PHP bytes