473,387 Members | 1,624 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

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

array help

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?

Oct 17 '06 #1
3 1738
Hmm Uzytkownik <ku*****@gmail.comwrote:
1.) How would I make a copy of the old prices array that
was manipulated?
$copy = $prices;
2.) a) What if i have commas in my prices by default?
str_replace(',','.',$newprice) + 9....
>
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....

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

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();
Oct 17 '06 #2
Rik
ku*****@gmail.com wrote:
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 />";
}
>>
<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>
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.
2.) a) What if i have commas in my prices by default?

e.g. = array('349,502' '234,995');
Removed by str_replace();
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).
c) How would I add a comma into my printed outputed values?
number_format(), or maybe in this case money_format();
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
Oct 17 '06 #3

Rik wrote:
ku*****@gmail.com wrote:
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 />";
}
>
<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>
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.
2.) a) What if i have commas in my prices by default?

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

Removed by str_replace();
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).
c) How would I add a comma into my printed outputed values?

number_format(), or maybe in this case money_format();
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!

Oct 18 '06 #4

This thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

8
by: point | last post by:
Hi there.. I have the folowing array => property = hexonet => property = 2003-12-01 18:46:20.0 => property = hexonet => property = 2003-12-02 02:59:15.0 => property = hexonet =>...
2
by: Antti Nummiaho | last post by:
Consider the following javascript: var temp = new Array(new Array(0)) document.writeln(temp) temp = new Array(new Array(0,1)) document.writeln(temp) One would assume that it would print "0...
8
by: Gactimus | last post by:
I made the program below. It outputs the smallest number in the array. What I would like to know is how do I output the array location. I am at a loss. For example, since the smallest number in...
7
by: ritchie | last post by:
Hi all, I am new to this group and I have question that you may be able to help me with. I am trying to learn C but am currently stuck on this. First of all, I have a function for each sort...
5
by: ritchie | last post by:
Hi, I am writing to ask if anyone can see why my array is not being sorted correctly? It's an array of 4 elements(ints 1,2,3,4) but after calling the selection sort it comes back sorted as...
8
by: intrepid_dw | last post by:
Hello, all. I've created a C# dll that contains, among other things, two functions dealing with byte arrays. The first is a function that returns a byte array, and the other is intended to...
3
by: George | last post by:
Sub ExcelToListBox() Dim xRange As Object Dim ary Dim xValue As String xRange = oXL.Range("A1:A9") 'has letters A-H ary = xRange.value xValue = ary(3, 1) 'xValue = C...
3
by: inkexit | last post by:
I need help figuring out what is wrong with my code. I posted here a few weeks ago with some code about creating self similar melodies in music. The coding style I'm being taught is apparently a...
23
by: sandy | last post by:
I need (okay, I want) to make a dynamic array of my class 'Directory', within my class Directory (Can you already smell disaster?) Each Directory can have subdirectories so I thought to put these...
5
by: fluk | last post by:
Hi Guys, I hope someone can help me with this, because i'm getting crazy to find a good way to do that! This is what I got by querying a db. $arr1 = array("site", "description", "area1" ,...
0
by: taylorcarr | last post by:
A Canon printer is a smart device known for being advanced, efficient, and reliable. It is designed for home, office, and hybrid workspace use and can also be used for a variety of purposes. However,...
0
by: aa123db | last post by:
Variable and constants Use var or let for variables and const fror constants. Var foo ='bar'; Let foo ='bar';const baz ='bar'; Functions function $name$ ($parameters$) { } ...
0
by: ryjfgjl | last post by:
In our work, we often receive Excel tables with data in the same format. If we want to analyze these data, it can be difficult to analyze them because the data is spread across multiple Excel files...
0
by: emmanuelkatto | last post by:
Hi All, I am Emmanuel katto from Uganda. I want to ask what challenges you've faced while migrating a website to cloud. Please let me know. Thanks! Emmanuel
0
BarryA
by: BarryA | last post by:
What are the essential steps and strategies outlined in the Data Structures and Algorithms (DSA) roadmap for aspiring data scientists? How can individuals effectively utilize this roadmap to progress...
1
by: nemocccc | last post by:
hello, everyone, I want to develop a software for my android phone for daily needs, any suggestions?
0
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,...
0
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...
0
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...

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.