473,748 Members | 2,326 Online
Bytes | Software Development & Data Engineering Community
+ Post

Home Posts Topics Members FAQ

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 1757
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.c om 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,$ra te){
$item = number_format(f loatval(str_rep lace(',','',$it em)) * (1 +
$rate),0,'',',' );
}
$oldprices = $prices;
array_walk($pri ces,'increase_p rice',$increase _rate);
$changes = array_map(null, $oldprices,$pri ces);
print_r($change s);
?>
</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.c om 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,$ra te){
$item = number_format(f loatval(str_rep lace(',','',$it em)) * (1 +
$rate),0,'',',' );
}
$oldprices = $prices;
array_walk($pri ces,'increase_p rice',$increase _rate);
$changes = array_map(null, $oldprices,$pri ces);
print_r($change s);
?>
</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
906
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 => property = 2004-12-01 18:46:20.0
2
1565
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 0" that is the first elements of the arrays, but it prints "undefined 0". Why does temp return
8
2111
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 the array is 2, the output should be 2 for the number and 1 for the location. If anyone could help or point me in the right direction that would be great. Thanks. ------------------- #include <iostream>
7
25172
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 (Bubble, insertion, selection..). I have an array of int's and am passing them to each sort function.
5
2217
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 1,1,2,4. I have narrowed it down to the sort function. I'm almost positive
8
10718
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 receive a byte array as one of its parameters. The project is marked for COM interop, and that all proceeds normally. When I reference the type library in the VB6 project, and write the code to call the function that returns the byte array, it works
3
8899
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 Me.ListBoxPerson.Items.AddRange(ary)
3
3738
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 lot different from what the pros around here use. I really need help with debugging some program errors more than anything, even though my coding style might not be perfect. Anyway here is my code. About the only things that work right are...
23
7416
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 in an array. The application compiles but aborts without giving me any useful information. What I suspect is happening is infinite recursion. Each Directory object creates an array of Subdirectories each of which has an array of...
5
1557
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" , "activity1"); $arr2 = array("site", "description", "area1" , "activity2"); $arr3 = array("site", "description", "area2" , "activity2"); $arr4 = array("site1", "description1", "area3" , "activity2");
0
8832
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,...
1
9331
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
9253
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
8250
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
6798
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...
0
6077
by: conductexam | last post by:
I have .net C# application in which I am extracting data from word file and save it in database particularly. To store word all data as it is I am converting the whole word file firstly in HTML and then checking html paragraph one by one. At the time of converting from word file to html my equations which are in the word document file was convert into image. Globals.ThisAddIn.Application.ActiveDocument.Select();...
0
4879
by: adsilva | last post by:
A Windows Forms form does not have the event Unload, like VB6. What one acts like?
2
2791
muto222
by: muto222 | last post by:
How can i add a mobile payment intergratation into php mysql website.
3
2216
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.