SitePoint Sponsor

User Tag List

Results 1 to 10 of 10

Thread: add array

  1. #1
    SitePoint Guru ripcurlksm's Avatar
    Join Date
    Aug 2004
    Location
    San Clemente, CA
    Posts
    859
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)

    add array

    how can I add a numerical array?

    Code:
    $a=array(100, 50, 300);

  2. #2
    . shoooo... silver trophy logic_earth's Avatar
    Join Date
    Oct 2005
    Location
    CA
    Posts
    9,013
    Mentioned
    8 Post(s)
    Tagged
    0 Thread(s)
    array_sum?
    Logic without the fatal effects.
    All code snippets are licensed under WTFPL.


  3. #3
    SitePoint Guru ripcurlksm's Avatar
    Join Date
    Aug 2004
    Location
    San Clemente, CA
    Posts
    859
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    ::rockin::

  4. #4
    SitePoint Guru ripcurlksm's Avatar
    Join Date
    Aug 2004
    Location
    San Clemente, CA
    Posts
    859
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    so what about this, im trying to create a numerical array through a loop, then count the sum of all numbers in that array


    PHP Code:
    $markets = array(redgreenblue);


    foreach(
    $markets as $key => $value){
        
    $mstring[] = getMarketCount($value);
      }

    echo 
    array_sum($mstring[]); 

  5. #5
    . shoooo... silver trophy logic_earth's Avatar
    Join Date
    Oct 2005
    Location
    CA
    Posts
    9,013
    Mentioned
    8 Post(s)
    Tagged
    0 Thread(s)
    drop [] from inside array_sum
    Logic without the fatal effects.
    All code snippets are licensed under WTFPL.


  6. #6
    SitePoint Guru ripcurlksm's Avatar
    Join Date
    Aug 2004
    Location
    San Clemente, CA
    Posts
    859
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    it appears its printing the values instead of adding them.
    PHP Code:
    $a=array(102030//this is outputting a value of "102030" instead of adding them and getting "60"
    echo array_sum($a); 
    what am i doing wrong?

  7. #7
    . shoooo... silver trophy logic_earth's Avatar
    Join Date
    Oct 2005
    Location
    CA
    Posts
    9,013
    Mentioned
    8 Post(s)
    Tagged
    0 Thread(s)
    Work just fine.

    PHP Code:
    <?php

    $a
    =array(102030);
    echo 
    array_sum($a);
    Logic without the fatal effects.
    All code snippets are licensed under WTFPL.


  8. #8
    SitePoint Enthusiast saisuman2001's Avatar
    Join Date
    Aug 2006
    Posts
    46
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    works fine..

    post u'r total code so that can figure out the problem..

    <?php

    $a=array(10, 20, 30);
    echo array_sum($a);
    Sai Suman Gubba
    Effigent India Pvt Ltd

  9. #9
    SitePoint Guru ripcurlksm's Avatar
    Join Date
    Aug 2004
    Location
    San Clemente, CA
    Posts
    859
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    thanks guys, the problem was with the function i was trying to pass. I had it set to echo instead of return the result of the function, which was causing a funk. Thanks for the help

  10. #10
    SitePoint Member
    Join Date
    Jun 2008
    Posts
    2
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    print_r($a);


Bookmarks

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •