jeudi 27 juillet 2017

Shuffle an array before inserting to database [php]

I have an array like this:

$posts = array(
            array(
                'head' => 'Let\'s pop some Champagne',
                'image' => '/beach/1.jpg'
            ),

            array(
                'head' => 'Sunrise in the beach sand',
                'image' => '/beach/2.jpg'
            ),
        Many more arrays...
 );
DB::table('posts')->insert($posts);

They are inserted to the database table named posts

The insertion works fine but the array are inserted in a sequence i.e Top-to-Bottom

Is there anyway to randomize the arrays insertion? Suppose the array is (1,2,3,4,5) I want it to be (4,2,3,5,1) something like this.




Aucun commentaire:

Enregistrer un commentaire