Connecting Tech Pros Worldwide Help | Site Map

array help

  #1  
Old October 17th, 2006, 10:25 PM
kurrent@gmail.com
Guest
 
Posts: n/a
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?

  #2  
Old October 17th, 2006, 10:55 PM
.:[ ikciu ]:.
Guest
 
Posts: n/a

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();


  #3  
Old October 18th, 2006, 12:25 AM
Rik
Guest
 
Posts: n/a

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


  #4  
Old October 18th, 2006, 01:45 AM
kurrent@gmail.com
Guest
 
Posts: n/a

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 Threads
Thread Thread Starter Forum Replies Last Post
Basic array help! comedydave answers 1 June 20th, 2007 06:41 PM
simple c array help needed ritchie answers 5 November 13th, 2005 11:22 PM
c++ array help aemazing@gmail.com answers 1 September 14th, 2005 06:55 AM
C++ ARRAY HELP!! aemazing@gmail.com answers 1 September 14th, 2005 06:25 AM
pointer and array help ali answers 5 July 22nd, 2005 06:27 PM