Hello and welcome to our community! Is this your first visit?
Register
Enjoy an ad free experience by logging in. Not a member yet? Register.
Results 1 to 4 of 4
  1. #1
    Regular Coder
    Join Date
    Dec 2006
    Posts
    417
    Thanks
    168
    Thanked 1 Time in 1 Post

    Array Removing Element Question

    Hiya,

    I did a array_count_values() on my array and have the following sample print_r() result:

    Code:
    Array ( [fraud] => 2 [—] => 2 [generale] => 2 [societe] => 2 [$7] => 1 [uncovered] => 1 [bank] => 1 [thursday] => 1 [uncovers] => 1 [massive] => 1 [french] => 1 [0] => 1 [billion] => 1 [orchestrated] => 1 [series] => 1 [bogus] => 1 [transactions] => 1 [trader] => 1 [futures] => 1 [one] => 1 [history] => 1 [biggest] => 1 [single] => 1 )
    I want to only keep the elements in my array that appear more than 1 time (ie. delete all of the elements that appear only once). There needs to be a conditional however, that if there are only elements that appear once that it save the first 5 elements.

    So if my array was:

    Code:
    Array ( [billion] => 1 [orchestrated] => 1 [series] => 1 [bogus] => 1 [transactions] => 1 [trader] => 1 [futures] => 1 [one] => 1 [history] => 1 [biggest] => 1 [single] => 1 )
    it would save "billion, orchestrated, series, bogus and transactions"

    how can I do this with PHP?

  • #2
    Senior Coder kbluhm's Avatar
    Join Date
    Apr 2007
    Location
    Philadelphia, PA, USA
    Posts
    1,509
    Thanks
    3
    Thanked 258 Times in 254 Posts
    First off, array_count_values() will not return the value count in descending order from most to least, so you will have to sort it with array_multisort() to preserver the key/value pairs.

    No need to check the number of each value, just use array_slice() since you'll always want at least that number of values.

    The usage:
    PHP Code:
    $array = array
    (
        
    'Jeep',
        
    'Chevy',
        
    'Ford',
        
    'Jeep',
        
    'Hummer',
        
    'Ford',
        
    'Pontiac',
        
    'Jeep',
        
    'Chevy',
        
    'Dodge',
        
    'Dodge',
    );

    echo 
    array_values_summary$array );     // Jeep, Dodge, Ford, Chevy, Pontiac and Hummer
    echo array_values_summary$array15 ); // Jeep, Dodge, Ford, Chevy, Pontiac and Hummer
    echo array_values_summary$array);  // Jeep, Dodge, Ford and Chevy
    echo array_values_summary$array);  // Jeep and Dodge
    echo array_values_summary$array);  // Jeep 
    The function:
    PHP Code:
    /**
     * array_values_summary()
     * Return a formatted string summary of array values
     **  *  
     * @var $array  array          The array you're sorting
     * @var $num    mixed numeric  The number of values to return (optional)
     */
    function array_values_summary$array$num )
    {
        
    // validate arguments
        
    if ( !is_array$array ) )
        {
            
    trigger_errorspfintf'array_values_summary() Argument #1 must be an array, %s given'gettype$array ) ), E_USER_ERROR );
            return;
        }
        if ( !
    is_int$num ) AND !ctype_digit$num ) )
        {
            
    trigger_errorspfintf'array_values_summary() Argument #2 must be numeric, %s given'gettype$array ) ), E_USER_ERROR );
            return;
        }
        
    $num = ( int) $num;
        if ( 
    $num === )
        {
            
    $num count$array );
        }
        
    // get values count
        
    $array array_count_values$array );
        
    // sort the values
        
    array_multisortarray_values$array ), SORT_DESCSORT_NUMERIC$array );
        
    // get $num values from array
        
    $array array_slice$array0$num );
        
    // fix $num if necessary
        
    $len   count$array );
        if ( 
    $num $len )
        {
            
    $num $len;
        }
        
    // get key values
        
    $array  array_keys$array );
        
    $i      0;
        
    $export '';
        
    // build and return the summary
        
    foreach ( $array as $word )
        {
            
    $export .= ( empty( $export ) ? '' : ( $i++ == $num ' and ' ', ' ) ) . $word;
        }
        return 
    $export;

    Last edited by kbluhm; 01-24-2008 at 06:53 PM.

  • Users who have thanked kbluhm for this post:

    Bobafart (01-24-2008)

  • #3
    Regular Coder
    Join Date
    Dec 2006
    Posts
    417
    Thanks
    168
    Thanked 1 Time in 1 Post
    I didn't fully explain what I was doing.. I dont just use array_count_values(), I also use asort() and array_reverse() to sort and position the elements in order form most commonly occurring to least commonly occurring.

    Rather than using a custom function (I appreciate your assitance, of course) I was wondering if there was a simpler way to only show the first 5 elements.

  • #4
    Senior Coder kbluhm's Avatar
    Join Date
    Apr 2007
    Location
    Philadelphia, PA, USA
    Posts
    1,509
    Thanks
    3
    Thanked 258 Times in 254 Posts
    PHP Code:
    $array array_slice$array0); 


  •  

    Posting Permissions

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