This is what I have: <?php $arr = array(1,1,1,1,1,2,2,2,2,3,3,3,3,4,4,4,4,4,5,5,5,5,5,6); //and it goes through a loop and each time gives 1. //example: for(i=0;i<count($arr);++i){ echo $arr[i]; //Here I need to count the array } ?> //the output needs to be 1=>5,2=>4.... PHP: btw this problem isnt from PHP its from AS3 but if I can get the way probably I can make it work in AS3 too.
well, your purpose seems to be unclear.. example depicts altogether different functionality from what you desire. If you can help us with example data, we can help you write php code to do the needful.
ok, I will try. I get this array from xml. $arr = array(1,1,1,1,1,2,2,2,2,3,3,3,3,4,4,4,4,4,5,5,5,5,5,6); I need to know how many ones in it twos etc.
following will do the trick: <?php $arr = array(1,1,1,1,1,2,2,2,2,3,3,3,3,4,4,4,4,4,5,5,5,5,5,6); $counts = get_element_counts($arr); print_r($counts); function get_element_counts($data_array) { $unique_array = array_unique($data_array); $count_array = array(); foreach($unique_array as $key) { $counter[$key] = 0; } foreach($data_array as $key) { $counter[$key] = $counter[$key] + 1; } return $counter; } ?> PHP:
This is far quicker $arr = array(1,1,1,1,1,2,2,2,2,3,3,3,3,4,4,4,4,4,5,5,5,5,5,6); $new = array_count_values($arr); foreach($new as $number=>$total) { echo $number.' = '.$total.'<br />'; } PHP: Output: 1 = 5 2 = 4 3 = 4 4 = 5 5 = 5 6 = 1 Code (markup):
Just for the record, your mistake was quite small: In your loop definition, it says "++i" - replace it with "$i++", and it will work. Use the foreach loop anyway, its the better solution.
Hello, I also have a PHP problem. I am a PHP newbie and have spent the past four or more hours researching how to do some calculations with integers people input into a form using checkboxes. In each set of data I have one integer and four null values. Nothing I try works. Here is my PHP <?php $years = array("one","two","three",four","five"); $rent = array("fif","twen","tfive","thir","thfive"); $house = $years * ($rent - 900)*12/2; echo "Using those two time frames and rental amounts, you can most likely afford a house that costs as much as <strong>$house dollars</strong>."; ?> <br /><br /> <?php echo "Use your browser's back button to return to the calculator or anywhere else on the Paradise Valley Investors website."; ?> Any ideas on how I can manipulate those variables? PHPWannabe
http://www.w3schools.com/php/php_arrays.asp this tutorial should give you all the information you need to get that working.
Here is the result of my PHP: twothfivethfive00 Fatal error: Unsupported operand types in /home/paradise/public_html/sendAfford.php on line 12. There are several things there but I cannot figure it out: 1. It says "two" followed by "thfive". Those are the names of two check boxes that had the integers "2" and "3500" in them resepectively. I wanted the integers to show up. 2. If you look at the PHP code below, you will see how I used the operands. <html> <?php $years=array("one","two","three","four","five"); echo $years[1]; $rent=array("fif","twen","tfive","thir","thfive"); echo $rent[4 ]; echo $rent[4 ]; $year = $years[ 0] + $years[ 1] + $years[ 2] + $years[ 3] + $years[ 4]; $renting = $rent[0 ] + $rent[1 ] + $rent[2 ] + $rent[3 ] + $rent[4 ]; echo $year; echo $renting; $afford = $years * ($rent - 900)*12/2; echo "Using those two time frames and rental amounts, you can most likely afford a house that costs as much as <strong>$house dollars</strong>."; ?> <br /><br /> <?php echo "Use your browser's back button to return to the calculator or anywhere else on the Paradise Valley Investors website."; ?> </html> Here is the html: <form class = "four" method="post" action="sendAfford.php"> <fieldset> <legend>In how many years would you like to own your own home? <span class = "brown">(Make sure only one box is checked.)</span></legend> <div class="inner6"> <label for="one">1</label> <input type="checkbox" name="salutation" id="one"/> <label for="two">2</label> <input type="checkbox" name="salutation" id="two"/> <label for="three">3</label> <input type="checkbox" name="salutation" id="three"/><br /> <label for="four">4</label> <input type="checkbox" name="salutation" id="four"/> <label for="five">5</label> <input type="checkbox" name="salutation" id="five"/> </div> </fieldset> <fieldset class = "two"> <legend>How much monthly rent can you afford? <span class = "brown">(Make sure only one box is checked.)</span></legend> <div class="inner6"> <label for="fif">1500</label> <input type="checkbox" name="salutation" id="fif"/> <label for="twen">2000</label> <input type="checkbox" name="salutation" id="twen"/> <label for="tfive">2500</label> <input type="checkbox" name="salutation" id="tfive"/><br /> <label for="thir">3000</label> <input type="checkbox" name="salutation" id="thir"/> <label for="thfive">3500</label> <input type="checkbox" name="salutation" id="thfive"/> </div> </fieldset> <label for="submit"><span class = "brown">Click below to calculate maximum value of house.</span></label> <p><input type="submit" id = "submit" value = "Calculate Now"/></p> </fieldset> </form> Thank you very much for whatever help you can give me. PHPWannabe
there's a lot here that you need to work out if you want to learn how to do it, send me a pm and I can help you figure it out
I know that in PHP its ++$i. I am in AS3 and that ok there, sorry for the mistake. Any way there is no $new = array_count_values($arr); in AS3 so I am looking for other options. I wish DP had an ActionScript forum.
arr = array(1,1,1,1,1,2,2,2,2,3,3,3,3,4,4,4,4,4,5,5,5,5,5,6); for(i=0;i<count($arr);++i){ echo arr[i]; counts[arr[i]]++; } Code (markup): something like that should work just fine for you, pm me if need more help with the logic, the syntax i am not 100% sure of since it is actionscript.
$arr = array(1,1,1,1,1,2,2,2,2,3,3,3,3,4,4,4,4,4,5,5,5,5,5,6); $new = array(); foreach($arr as $v) { $new[$v]++; } foreach($new as $number=>$total) { echo $number.' = '.$total.'<br />'; } PHP: