Results 1 to 10 of 10

Thread: Extract Values From PHP Array

  1. #1
    Join Date
    Nov 2010
    Posts
    115
    Thanks
    27
    Thanked 0 Times in 0 Posts

    Default Extract Values From PHP Array

    Hi All,

    Anyone can help me to get the values from the following array

    PHP Code:

    Array ( [0] => 2644 [1] => 10 [2] => 2644 [3] => 50 [4] => 2644 [5] => 10 [6] => 5172 [7] => 70 [8] => 6546 [9] => 100 

    In the above array first one customer-id(2644) and his amount is (10). Now can i extract the amount of all three customer-id's (2644) and sum their amount. I am trying to do it dynamically. Is this possible? Can anyone suggest me how to do this?

  2. #2
    Join Date
    Apr 2008
    Location
    So.Cal
    Posts
    3,643
    Thanks
    63
    Thanked 516 Times in 502 Posts
    Blog Entries
    5

    Default

    so, odd values are amounts? put the array through a loop:
    PHP Code:
    <?php
    $array 
    = Array( 2644,10,2644,50,2644,10,5172,70,6546,100 );
    foreach( 
    $array as $key=>$value ){
        
    // use the % (modulus (remainder after division)) to determine if the key is odd or even
        
    if( $key%=== ){
            
    // it's even (or 0) --a customer number
            
    $cust_no[] = $value;
        }else{
            
    // it's odd --an amount
            
    $amount[] = $value;
        }
    }
    // now you have two arrays: one with customer numbers, one with amounts.
    //  want to combine them? (first arg will be keys; second arg will be values)
    $assoc array_combine$cust_no,$amount );

    // view results:
    foreach( $assoc as $k=>$v ){
       print 
    "customer #$k has amount $v<br>";
    }
    print 
    "Sum of all amounts: ".array_sum$assoc );

  3. The Following User Says Thank You to traq For This Useful Post:

    hemi519 (08-22-2012)

  4. #3
    Join Date
    Nov 2010
    Posts
    115
    Thanks
    27
    Thanked 0 Times in 0 Posts

    Default

    Quote Originally Posted by traq View Post
    so, odd values are amounts? put the array through a loop:
    PHP Code:
    <?php
    $array 
    = Array( 2644,10,2644,50,2644,10,5172,70,6546,100 );
    foreach( 
    $array as $key=>$value ){
        
    // use the % (modulus (remainder after division)) to determine if the key is odd or even
        
    if( $key%=== ){
            
    // it's even (or 0) --a customer number
            
    $cust_no[] = $value;
        }else{
            
    // it's odd --an amount
            
    $amount[] = $value;
        }
    }
    // now you have two arrays: one with customer numbers, one with amounts.
    //  want to combine them? (first arg will be keys; second arg will be values)
    $assoc array_combine$cust_no,$amount );

    // view results:
    foreach( $assoc as $k=>$v ){
       print 
    "customer #$k has amount $v<br>";
    }
    print 
    "Sum of all amounts: ".array_sum$assoc );

    Thanks for your time and suggestion. In the above code i am having one doubt,

    Now For Customer-Id (2644) there are three amounts which should give a total amount of 70, but according to your code the value for 2644 is 10. How can i make it to 70

  5. #4
    Join Date
    Mar 2005
    Location
    SE PA USA
    Posts
    30,495
    Thanks
    82
    Thanked 3,449 Times in 3,410 Posts
    Blog Entries
    12

    Default

    PHP Code:
    <?php
    $array 
    = Array( 2644,10,2644,50,2644,10,5172,70,6546,100 );

    foreach( 
    $array as $key => $value ){
        
    // use the % (modulus (remainder after division)) to determine if the key is odd or even
        
    if( $key%=== ){
            
    // it's even (or 0) --a customer number
            
    $cust_no[$value] = isset($cust_no[$value])? $cust_no[$value] + $array[$key 1] : $array[$key 1];
        }
    }

    // view results:
    foreach( $cust_no as $k => $v ){
       print 
    "customer #$k has amount $v<br>";
       
    $assoc[] = $v;
    }
    print 
    "Sum of all amounts: " array_sum$assoc );
    ?>
    - John
    ________________________

    Show Additional Thanks: International Rescue Committee - Donate or: The Ocean Conservancy - Donate or: PayPal - Donate

  6. The Following User Says Thank You to jscheuer1 For This Useful Post:

    hemi519 (08-22-2012)

  7. #5
    Join Date
    Nov 2010
    Posts
    115
    Thanks
    27
    Thanked 0 Times in 0 Posts

    Default

    Thanks for both of you, it is working perfectly. But one last help

    I am getting array in this format

    PHP Code:
    Array ( [0] => 2644 [1] => 10 [2] => 2644 [3] => 50 [4] => 2644 [5] => 10 [6] => 5172 [7] => 70 [8] => 6546 [9] => 100 
    how to convert that according to array taken by you in the above example

    PHP Code:
    Array ( 2644 ,10,2644,50,2644,10,5172,70,6546,100 

  8. #6
    Join Date
    Mar 2005
    Location
    SE PA USA
    Posts
    30,495
    Thanks
    82
    Thanked 3,449 Times in 3,410 Posts
    Blog Entries
    12

    Default

    Aside from a typo you made in copying our version, they are equivalent. Doing:

    PHP Code:
    $array = Array( 2644,10,2644,50,2644,10,5172,70,6546,100 );
    print_r($array); 
    will get you:

    Array ( [0] => 2644 [1] => 10 [2] => 2644 [3] => 50 [4] => 2644 [5] => 10 [6] => 5172 [7] => 70 [8] => 6546 [9] => 100 )
    You cannot do this though:

    PHP Code:
    $array = Array ( [0] => 2644 [1] => 10 [2] => 2644 [3] => 50 [4] => 2644 [5] => 10 [6] => 5172 [7] => 70 [8] => 6546 [9] => 100 ); 
    It's invalid PHP.

    You can do this:

    PHP Code:
    $array = Array ( => 2644=> 10=> 2644=> 50=> 2644=> 10=> 5172=> 70=> 6546=> 100 ); 
    and that's equivalent.

    If you're still having problems, where are you getting your array from? Show us the code that produces it.
    Last edited by jscheuer1; 08-22-2012 at 04:56 PM.
    - John
    ________________________

    Show Additional Thanks: International Rescue Committee - Donate or: The Ocean Conservancy - Donate or: PayPal - Donate

  9. #7
    Join Date
    Nov 2010
    Posts
    115
    Thanks
    27
    Thanked 0 Times in 0 Posts

    Default

    In my coding there are two while loops which will produce customer-id and their amount
    PHP Code:

    $array 
    []= $customerid;
    $array []= $amount;

    // following is outside of two while loops
    showresult($array); // calling a function

    function showresult($array// code given by you both
    {

    print_r($array// this will give  $array = Array ( 0 => 2644, 1 => 10, 2 => 2644, 3 => 50, 4 => 2644, 5 => 10, 6 => 5172, 7 => 70, 8 => 6546, 9 => 100 );  
    // the above array will produce incorrect results

    $array = Array( 2644,10,2644,50,2644,10,5172,70,6546,100 ); // array taken by you
    // the above array will produce correct results

    // So i want my array to  be taken as Array( 2644,10,2644,50,2644,10,5172,70,6546,100 );

    foreach( $array as $key => $value ){ 
        
    // use the % (modulus (remainder after division)) to determine if the key is odd or even 
        
    if( $key%=== ){ 
            
    // it's even (or 0) --a customer number 
            
    $cust_no[$value] = isset($cust_no[$value])? $cust_no[$value] + $array[$key 1] : $array[$key 1]; 
        } 


    // view results: 
    foreach( $cust_no as $k => $v ){ 
       print 
    "customer #$k has amount $v<br>"
       
    $assoc[] = $v

    print 
    "Sum of all amounts: " array_sum$assoc ); 


  10. #8
    Join Date
    Mar 2005
    Location
    SE PA USA
    Posts
    30,495
    Thanks
    82
    Thanked 3,449 Times in 3,410 Posts
    Blog Entries
    12

    Default

    Code:
    print_r($array) // this will give  $array = Array ( 0 => 2644, 1 => 10, 2 => 2644, 3 => 50, 4 => 2644, 5 => 10, 6 => 5172, 7 => 70, 8 => 6546, 9 => 100 );  
    // the above array will produce incorrect results
    Code:
    print_r($array)
    will never give that result. For one thing it has no ; terminus, for another print_r doesn't output in the format you indicate in the comment.

    I don't see any while loops.

    What you have here suggests one:

    Code:
    $array []= $customerid;
    $array []= $amount;
    And could if it were a while loop output an array like, when print_r is used on it:

    Array ( [0] => 2644 [1] => 10 [2] => 2644 [3] => 50 [4] => 2644 [5] => 10 [6] => 5172 [7] => 70 [8] => 6546 [9] => 100 )
    And that would work with the function showresult you have there once you clean it up (add the missing terminus and remove the line declaring a new array for $array).
    - John
    ________________________

    Show Additional Thanks: International Rescue Committee - Donate or: The Ocean Conservancy - Donate or: PayPal - Donate

  11. #9
    Join Date
    Nov 2010
    Posts
    115
    Thanks
    27
    Thanked 0 Times in 0 Posts

    Default

    I am sry, i was having issue in my coding, everything is working fine, onceagain thanks

  12. #10
    Join Date
    Mar 2006
    Location
    Illinois, USA
    Posts
    12,164
    Thanks
    265
    Thanked 690 Times in 678 Posts

    Default

    Just in case anyone is wondering, it is possible to convert between the print_r() output format and the array() format. It is relatively simple and works using str_replace(). (It's easier if you're using just numerical data or something else that doesn't involve the comma or quotes symbols in the array data, but possible even if you're doing that, probably using regex.)

    Or, what I have done sometimes is to create my own equivalent function to print_r() that just immediately prints it in the array() format and then you can save that into a .php file and read it directly. (Of course be very careful about what could get into that .php file and don't run it on your server if you're not certain the code is safe!)
    Daniel - Freelance Web Design | <?php?> | <html>| español | Deutsch | italiano | português | català | un peu de français | some knowledge of several other languages: I can sometimes help translate here on DD | Linguistics Forum

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
  •