Muiter
Muiter

Reputation: 1520

Shuffle array losing data

I have an array and when I check this with print_r the output is:

Array ( [0] => metaalboutique.jpg [1] => asc.jpg [2] => thure.jpg [3] => stegge.jpg [4] => aws.jpg [5] => rsw.jpg [6] => pmm.jpg )

I want the export to be shuffled so I use shuffle() but when I check the output with print_r now I only see 1 as output.

$portfolio = array
  (
    'thure.jpg',
    'rsw.jpg',
    'pmm.jpg',
    'asc.jpg',
    'stegge.jpg',
    'metaalboutique.jpg',
    'aws.jpg'
  );

$shuffled_portfolio = shuffle($portfolio);
print_r($portfolio);
print_r($shuffled_portfolio);

Upvotes: 0

Views: 116

Answers (2)

Phoenix404
Phoenix404

Reputation: 1058

PHP shuffle function returns boolean value.

shuffle — Shuffle an array

bool shuffle ( array &$array )

&$array - the & sign means you are passing a reference of an array in that function.

Return Values

Returns TRUE (1) on success or FALSE(0) on failure.

Upvotes: 1

Mureinik
Mureinik

Reputation: 311208

shuffle shuffles an array in place and returns a boolean to indicate if the shuffling succeeded (TRUE) or not (FALSE):

$portfolio = array
  (
    'thure.jpg',
    'rsw.jpg',
    'pmm.jpg',
    'asc.jpg',
    'stegge.jpg',
    'metaalboutique.jpg',
    'aws.jpg'
  );
print_r($portfolio);

$success = shuffle($portfolio);
if ($success) {
    # $portfolio is now shuffled
    print_r($portfolio);
}

Upvotes: 4

Related Questions