Array help counting

Discussion in 'PHP' started by astrazone, Oct 24, 2009.

  1. #1
    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.
     
    astrazone, Oct 24, 2009 IP
  2. mastermunj

    mastermunj Well-Known Member

    Messages:
    687
    Likes Received:
    13
    Best Answers:
    0
    Trophy Points:
    110
    #2
    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.
     
    mastermunj, Oct 24, 2009 IP
  3. astrazone

    astrazone Member

    Messages:
    358
    Likes Received:
    5
    Best Answers:
    0
    Trophy Points:
    33
    #3
    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.
     
    astrazone, Oct 24, 2009 IP
  4. mastermunj

    mastermunj Well-Known Member

    Messages:
    687
    Likes Received:
    13
    Best Answers:
    0
    Trophy Points:
    110
    #4
    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:
     
    mastermunj, Oct 24, 2009 IP
  5. JAY6390

    JAY6390 Peon

    Messages:
    918
    Likes Received:
    31
    Best Answers:
    0
    Trophy Points:
    0
    #5
    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):
     
    JAY6390, Oct 24, 2009 IP
  6. mastermunj

    mastermunj Well-Known Member

    Messages:
    687
    Likes Received:
    13
    Best Answers:
    0
    Trophy Points:
    110
    #6
    that one is cool.. thanks for sharing JAY6390 :)
     
    mastermunj, Oct 24, 2009 IP
  7. JAY6390

    JAY6390 Peon

    Messages:
    918
    Likes Received:
    31
    Best Answers:
    0
    Trophy Points:
    0
    #7
    No probs. It's rare you see that function but yeah its pretty nifty :)
     
    JAY6390, Oct 24, 2009 IP
  8. ThomasTwen

    ThomasTwen Peon

    Messages:
    113
    Likes Received:
    4
    Best Answers:
    0
    Trophy Points:
    0
    #8
    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.
     
    ThomasTwen, Oct 24, 2009 IP
  9. PHPWannabe

    PHPWannabe Peon

    Messages:
    2
    Likes Received:
    0
    Best Answers:
    0
    Trophy Points:
    0
    #9
    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
     
    PHPWannabe, Oct 24, 2009 IP
  10. heavydev

    heavydev Peon

    Messages:
    33
    Likes Received:
    0
    Best Answers:
    0
    Trophy Points:
    0
    #10
    heavydev, Oct 24, 2009 IP
  11. PHPWannabe

    PHPWannabe Peon

    Messages:
    2
    Likes Received:
    0
    Best Answers:
    0
    Trophy Points:
    0
    #11
    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"/>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;
    <label for="two">2</label>
    <input type="checkbox" name="salutation" id="two"/>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;
    <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"/>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;
    <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"/>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;
    <label for="twen">2000</label>
    <input type="checkbox" name="salutation" id="twen"/>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;
    <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"/>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;
    <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
     
    PHPWannabe, Oct 24, 2009 IP
  12. heavydev

    heavydev Peon

    Messages:
    33
    Likes Received:
    0
    Best Answers:
    0
    Trophy Points:
    0
    #12
    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
     
    heavydev, Oct 25, 2009 IP
  13. astrazone

    astrazone Member

    Messages:
    358
    Likes Received:
    5
    Best Answers:
    0
    Trophy Points:
    33
    #13
    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.
     
    astrazone, Oct 25, 2009 IP
  14. heavydev

    heavydev Peon

    Messages:
    33
    Likes Received:
    0
    Best Answers:
    0
    Trophy Points:
    0
    #14
    
    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.
     
    heavydev, Oct 25, 2009 IP
  15. JAY6390

    JAY6390 Peon

    Messages:
    918
    Likes Received:
    31
    Best Answers:
    0
    Trophy Points:
    0
    #15
    $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:
     
    JAY6390, Oct 25, 2009 IP