Skip to main content

I think the function is not named properly. You are NOT shuffling an associative array. You are shuffling a numerically indexed array (whose values happen to be associative arrays).

As such, I don't see a great need for this function. You could use array_rand() to pick random keys from the array.

Now, assuming you did want to keep such a function, for example to actually get array of values not just keys. I would consider a few things:

  • Consider throwing an exception or logging error if function is passed a non-array value. This can help make sure you are invoking this function properly in your code vs. just returning the passed value unchanged.
  • Consider passing "limit" value to the function vs. hard-coding for 4 return elements.

This might yield something like:

function array_rand_values($arr, $limit = 1) {
    if(!is_array($arr)) {
        throw new InvalidArgumentException('Array expected for first argument.');
    }
    if(!is_int($limit) || $limit < 1) {
        throw new InvalidArgumentException(
            'Positive integer value expected for second argument.'
        );
    }
    if(count($arr) <= $limit) {
       return shuffle($arr);
       return $arr;
    }
    $keys = array_rand($arr, $limit);
    $values = array();
    for ($i = 0; $i < $limit; $i++) {
        $values[] = $arr[$keys[$i]];
    }
    return $values;
}   

Note that this function does not preserve keys if passed an associative array. This should probably be the logical behavior in such a case, as "shuffling" an associative array typically makes zero sense. This would however still pick X random values from an associative array.

If one needed to pick X random key/value pairs from an associative array, I would suggest a separate function, so that intent of caller is clear. That function may be implemented in very similar manner to the one above, with single line of code change.

function array_rand_values_assoc($arr, $limit = 1) {
    // same code as above until assignment in loop

    for ($i = 0; $i < $limit; $i++) {
        $values[$keys[$i]] = $arr[$keys[$i]];
    }
    return $values;
}

I think the function is not named properly. You are NOT shuffling an associative array. You are shuffling a numerically indexed array (whose values happen to be associative arrays).

As such, I don't see a great need for this function. You could use array_rand() to pick random keys from the array.

Now, assuming you did want to keep such a function, for example to actually get array of values not just keys. I would consider a few things:

  • Consider throwing an exception or logging error if function is passed a non-array value. This can help make sure you are invoking this function properly in your code vs. just returning the passed value unchanged.
  • Consider passing "limit" value to the function vs. hard-coding for 4 return elements.

This might yield something like:

function array_rand_values($arr, $limit = 1) {
    if(!is_array($arr)) {
        throw new InvalidArgumentException('Array expected for first argument.');
    }
    if(!is_int($limit) || $limit < 1) {
        throw new InvalidArgumentException(
            'Positive integer value expected for second argument.'
        );
    }
    if(count($arr) <= $limit) {
       return shuffle($arr);
    }
    $keys = array_rand($arr, $limit);
    $values = array();
    for ($i = 0; $i < $limit; $i++) {
        $values[] = $arr[$keys[$i]];
    }
    return $values;
}   

Note that this function does not preserve keys if passed an associative array. This should probably be the logical behavior in such a case, as "shuffling" an associative array typically makes zero sense. This would however still pick X random values from an associative array.

If one needed to pick X random key/value pairs from an associative array, I would suggest a separate function, so that intent of caller is clear. That function may be implemented in very similar manner to the one above, with single line of code change.

function array_rand_values_assoc($arr, $limit = 1) {
    // same code as above until assignment in loop

    for ($i = 0; $i < $limit; $i++) {
        $values[$keys[$i]] = $arr[$keys[$i]];
    }
    return $values;
}

I think the function is not named properly. You are NOT shuffling an associative array. You are shuffling a numerically indexed array (whose values happen to be associative arrays).

As such, I don't see a great need for this function. You could use array_rand() to pick random keys from the array.

Now, assuming you did want to keep such a function, for example to actually get array of values not just keys. I would consider a few things:

  • Consider throwing an exception or logging error if function is passed a non-array value. This can help make sure you are invoking this function properly in your code vs. just returning the passed value unchanged.
  • Consider passing "limit" value to the function vs. hard-coding for 4 return elements.

This might yield something like:

function array_rand_values($arr, $limit = 1) {
    if(!is_array($arr)) {
        throw new InvalidArgumentException('Array expected for first argument.');
    }
    if(!is_int($limit) || $limit < 1) {
        throw new InvalidArgumentException(
            'Positive integer value expected for second argument.'
        );
    }
    if(count($arr) <= $limit) {
       shuffle($arr);
       return $arr;
    }
    $keys = array_rand($arr, $limit);
    $values = array();
    for ($i = 0; $i < $limit; $i++) {
        $values[] = $arr[$keys[$i]];
    }
    return $values;
}   

Note that this function does not preserve keys if passed an associative array. This should probably be the logical behavior in such a case, as "shuffling" an associative array typically makes zero sense. This would however still pick X random values from an associative array.

If one needed to pick X random key/value pairs from an associative array, I would suggest a separate function, so that intent of caller is clear. That function may be implemented in very similar manner to the one above, with single line of code change.

function array_rand_values_assoc($arr, $limit = 1) {
    // same code as above until assignment in loop

    for ($i = 0; $i < $limit; $i++) {
        $values[$keys[$i]] = $arr[$keys[$i]];
    }
    return $values;
}
added 129 characters in body
Source Link
Mike Brant
  • 9.6k
  • 13
  • 23

I think the function is not named properly. You are NOT shuffling an associative array. You are shuffling a numerically indexed array (whose values happen to be associative arrays).

As such, I don't see a great need for this function. You could use array_rand() to pick random keys from the array.

Now, assuming you did want to keep such a function, for example to actually get array of values not just keys. I would consider a few things:

  • Consider throwing an exception or logging error if function is passed a non-array value. This can help make sure you are invoking this function properly in your code vs. just returning the passed value unchanged.
  • Consider passing "limit" value to the function vs. hard-coding for 4 return elements.

This might yield something like:

function array_rand_values($arr, $limit = 1) {
    if(!is_array($arr)) {
        throw new InvalidArgumentException('Array expected for first argument.');
    }
    if(!is_int($limit) || $limit < 1) {
        throw new InvalidArgumentException(
            'Positive integer value expected for second argument.'
        );
    }
    if(count($arr) <= $limit) {
       return shuffle($arr);
    }
    $keys = array_rand($arr, $limit);
    $values = array();
    for ($i = 0; $i < $limit; $i++) {
        $values[] = $arr[$keys[$i]];
    }
    return $values;
}   

Note that this function does not preserve keys if passed an associative array. This should probably be the logical behavior in such a case, as "shuffling" an associative array typically makes zero sense. This would however still pick X random values from an associative array.

If one needed to pick X random key/value pairs from an associative array, I would suggest a separate function, so that intent of caller is clear. That function may be implemented in very similar manner to the one above, with single line of code change.

function array_rand_values_assoc($arr, $limit = 1) {
    // same code as above until assignment in loop

    for ($i = 0; $i < $limit; $i++) {
        $values[$keys[$i]] = $arr[$keys[$i]];
    }
    return $values;
}

I think the function is not named properly. You are NOT shuffling an associative array. You are shuffling a numerically indexed array (whose values happen to be associative arrays).

As such, I don't see a great need for this function. You could use array_rand() to pick random keys from the array.

Now, assuming you did want to keep such a function, for example to actually get array of values not just keys. I would consider a few things:

  • Consider throwing an exception or logging error if function is passed a non-array value. This can help make sure you are invoking this function properly in your code vs. just returning the passed value unchanged.
  • Consider passing "limit" value to the function vs. hard-coding for 4 return elements.

This might yield something like:

function array_rand_values($arr, $limit = 1) {
    if(!is_array($arr)) {
        throw new InvalidArgumentException('Array expected for first argument.');
    }
    if(!is_int($limit) || $limit < 1) {
        throw new InvalidArgumentException(
            'Positive integer value expected for second argument.'
        );
    }
    if(count($arr) <= $limit) {
       return shuffle($arr);
    }
    $keys = array_rand($arr, $limit);
    $values = array();
    for ($i = 0; $i < $limit; $i++) {
        $values[] = $arr[$keys[$i]];
    }
    return $values;
}   

I think the function is not named properly. You are NOT shuffling an associative array. You are shuffling a numerically indexed array (whose values happen to be associative arrays).

As such, I don't see a great need for this function. You could use array_rand() to pick random keys from the array.

Now, assuming you did want to keep such a function, for example to actually get array of values not just keys. I would consider a few things:

  • Consider throwing an exception or logging error if function is passed a non-array value. This can help make sure you are invoking this function properly in your code vs. just returning the passed value unchanged.
  • Consider passing "limit" value to the function vs. hard-coding for 4 return elements.

This might yield something like:

function array_rand_values($arr, $limit = 1) {
    if(!is_array($arr)) {
        throw new InvalidArgumentException('Array expected for first argument.');
    }
    if(!is_int($limit) || $limit < 1) {
        throw new InvalidArgumentException(
            'Positive integer value expected for second argument.'
        );
    }
    if(count($arr) <= $limit) {
       return shuffle($arr);
    }
    $keys = array_rand($arr, $limit);
    $values = array();
    for ($i = 0; $i < $limit; $i++) {
        $values[] = $arr[$keys[$i]];
    }
    return $values;
}   

Note that this function does not preserve keys if passed an associative array. This should probably be the logical behavior in such a case, as "shuffling" an associative array typically makes zero sense. This would however still pick X random values from an associative array.

If one needed to pick X random key/value pairs from an associative array, I would suggest a separate function, so that intent of caller is clear. That function may be implemented in very similar manner to the one above, with single line of code change.

function array_rand_values_assoc($arr, $limit = 1) {
    // same code as above until assignment in loop

    for ($i = 0; $i < $limit; $i++) {
        $values[$keys[$i]] = $arr[$keys[$i]];
    }
    return $values;
}
added 129 characters in body
Source Link
Mike Brant
  • 9.6k
  • 13
  • 23

I think the function is not named properly. You are NOT shuffling an associative array. You are shuffling a numerically indexed array (whose values happen to be associative arrays).

As such, I don't see a great need for this function. You could use array_rand() to pick random keys from the array.

Now, assuming you did want to keep such a function, for example to actually get array of values not just keys. I would consider a few things:

  • Consider throwing an exception or logging error if function is passed a non-array value. This can help make sure you are invoking this function properly in your code vs. just returning the passed value unchanged.
  • Consider passing "limit" value to the function vs. hard-coding for 4 return elements.

This might yield something like:

function array_rand_values($arr, $limit = 1) {
    if(!is_array($arr)) {
         throw new InvalidArgumentException('Array expected for first argument.');
    }
    if(!is_int($limit) || $limit < 1) {
         throw new InvalidArgumentException('Integer
            'Positive integer value expected for second argument.'
        );
    }
    if(count($arr) <= $limit) {
       return shuffle($arr);
    }
    $keys = array_rand($arr, $limit);
    $values = array();
    for ($i = 0; $i < $limit; $i++) {
        $values[] = $arr[$keys[$i]];
    }
    return $values;
}   

I think the function is not named properly. You are NOT shuffling an associative array. You are shuffling a numerically indexed array (whose values happen to be associative arrays).

As such, I don't see a great need for this function. You could use array_rand() to pick random keys from the array.

Now, assuming you did want to keep such a function, for example to actually get array of values not just keys. I would consider a few things:

  • Consider throwing an exception or logging error if function is passed a non-array value. This can help make sure you are invoking this function properly in your code vs. just returning the passed value unchanged.
  • Consider passing "limit" value to the function vs. hard-coding for 4 return elements.

This might yield something like:

function array_rand_values($arr, $limit = 1) {
    if(!is_array($arr)) {
         throw new InvalidArgumentException('Array expected for first argument.');
    }
    if(!is_int($limit)) {
         throw new InvalidArgumentException('Integer expected for second argument.');
    }
    if(count($arr) <= $limit) {
       return shuffle($arr);
    }
    $keys = array_rand($arr, $limit);
    $values = array();
    for ($i = 0; $i < $limit; $i++) {
        $values[] = $arr[$keys[$i]];
    }
    return $values;
}   

I think the function is not named properly. You are NOT shuffling an associative array. You are shuffling a numerically indexed array (whose values happen to be associative arrays).

As such, I don't see a great need for this function. You could use array_rand() to pick random keys from the array.

Now, assuming you did want to keep such a function, for example to actually get array of values not just keys. I would consider a few things:

  • Consider throwing an exception or logging error if function is passed a non-array value. This can help make sure you are invoking this function properly in your code vs. just returning the passed value unchanged.
  • Consider passing "limit" value to the function vs. hard-coding for 4 return elements.

This might yield something like:

function array_rand_values($arr, $limit = 1) {
    if(!is_array($arr)) {
        throw new InvalidArgumentException('Array expected for first argument.');
    }
    if(!is_int($limit) || $limit < 1) {
        throw new InvalidArgumentException(
            'Positive integer value expected for second argument.'
        );
    }
    if(count($arr) <= $limit) {
       return shuffle($arr);
    }
    $keys = array_rand($arr, $limit);
    $values = array();
    for ($i = 0; $i < $limit; $i++) {
        $values[] = $arr[$keys[$i]];
    }
    return $values;
}   
Source Link
Mike Brant
  • 9.6k
  • 13
  • 23
Loading