...

View Full Version : Array Removing Element Question



Bobafart
01-24-2008, 12:43 PM
Hiya,

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



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:



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?

kbluhm
01-24-2008, 03:41 PM
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:


$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( $array, 15 ); // Jeep, Dodge, Ford, Chevy, Pontiac and Hummer
echo array_values_summary( $array, 4 ); // Jeep, Dodge, Ford and Chevy
echo array_values_summary( $array, 2 ); // Jeep and Dodge
echo array_values_summary( $array, 1 ); // Jeep


The function:


/**
* 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 = 0 )
{
// validate arguments
if ( !is_array( $array ) )
{
trigger_error( spfintf( '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_error( spfintf( 'array_values_summary() Argument #2 must be numeric, %s given', gettype( $array ) ), E_USER_ERROR );
return;
}
$num = ( int) $num;
if ( $num === 0 )
{
$num = count( $array );
}
// get values count
$array = array_count_values( $array );
// sort the values
array_multisort( array_values( $array ), SORT_DESC, SORT_NUMERIC, $array );
// get $num values from array
$array = array_slice( $array, 0, $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 - 2 ? ' and ' : ', ' ) ) . $word;
}
return $export;
}

Bobafart
01-24-2008, 09:01 PM
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.

kbluhm
01-24-2008, 09:44 PM
$array = array_slice( $array, 0, 5 );



EZ Archive Ads Plugin for vBulletin Copyright 2006 Computer Help Forum