How to shuffle this array inside itself? [duplicate]

2
$img = array
  (
  array("1.jpg","Logo","Tigre"),
  array("2.jpg","Logo","Cão"),
  array("3.jpg","Logo","Montanha"),
  array("4.jpg","banner","Design Grafico"),
  array("5.jpg","Logo","Dentista"),
  array("6.jpg","Logo","Basketball")
  );

I wanted the array to look like this

$img = array
  (
  array("5.jpg","Logo","Dentista"),
  array("1.jpg","Logo","Tigre"),
  array("4.jpg","banner","Design Grafico"),
  array("3.jpg","Logo","Montanha"),
  array("2.jpg","Logo","Cão"),
  array("6.jpg","Logo","Basketball")
  );

I tried this:

   function shuffle_assoc($list) { 
      if (!is_array($list)) return $list; 

      $keys = array_keys($list); 
      shuffle($keys); 
      $random = array(); 
      foreach ($keys as $key) { 
        $random[$key] = $list[$key]; 
      }
      return $random; 
    } 

    $img = array
      (
      array("1.jpg","Logo","Tigre"),
      array("2.jpg","Logo","Cão"),
      array("3.jpg","Logo","Montanha"),
      array("4.jpg","banner","Design Grafico"),
      array("5.jpg","Logo","Dentista"),
      array("6.jpg","Logo","Basketball")
      );
   $img = shuffle_assoc($img);
    print_r($img);
    
asked by anonymous 18.11.2016 / 01:06

1 answer

6

So I noticed there is no default, so using only shuffle(...) should resolve:

<?php

$img = array(
    array("1.jpg","Logo","Tigre"),
    array("2.jpg","Logo","Cão"),
    array("3.jpg","Logo","Montanha"),
    array("4.jpg","banner","Design Grafico"),
    array("5.jpg","Logo","Dentista"),
    array("6.jpg","Logo","Basketball")
);

echo 'Antes:', PHP_EOL;

var_dump($img);

shuffle($img);

echo 'Depois:', PHP_EOL;

var_dump($img);

See the result on ideone

    
18.11.2016 / 01:32