Wednesday, 15 July 2015

php - How to randomly select a value in an Array -



php - How to randomly select a value in an Array -

this question has reply here:

how convert object array? 6 answers

please note have read similar questions on site none worked me , seem unique @ to the lowest degree now/to me.

i have next array;

[{"advert_id":"advert1"},{"advert_id":"advert2"},{"advert_id":"advert3"},{"advert_id":"advert4"},{"advert_id":"advert5"},{"advert_id":"advert6"},{"advert_id":"advert7"}]

i got info when fetch info database laravel $adverts = advert::select('advert_id')->get();

how can randomly select element it?

i tried using array_rand($adverts) had errors (array_rand() expects parameter 1 array, object given )

here's proper reply can mark correct, allowing others find well.

you're referencing array parsing object. using json_decode() below , parsing sec parameter, explicitly inquire array in homecoming instead of object.

json_decode($array, true);

php arrays

No comments:

Post a Comment