php - Function returns "Array" instead of actual output -


i have code:

function returnarray() {      $intdata = array("1", "3");      return $intdata[array_rand($intdata)];  } 

i'm trying make function similar 1 above.

instead of adding integers commas i'm trying implode commas integers.

sort of dynamic version of above using json_decode, file_get_contents, foreach loop, array, array_rand & return

this code:

function something() {   foreach (json_decode(file_get_contents('removed'), true) $jsonarr) {     $arrdata = array(implode(",", $jsonarr));     $rounddata = $arrdata[array_rand($arrdata)];     return $rounddata;   } } 

i wondering if i'm doing right , if array correct or not.

also doesn't return array.

when try implode, throws error

implode invalid argument

it seems you're trying print random room_id json response.

the problem lies here:

$intdata = array(implode(",", $arrroomsreverse['room_id'])); 

you can't initialize array that.

just push room_id element array, so:

$intdata[] = $arrroomsreverse['room_id']; 

now, can random room_id outside loop:

$introom = $intdata[array_rand($intdata)]; 

the function like:

function returnarray()  {     $str = file_get_contents('...');     $jsonarr = json_decode($str, true);     foreach ($jsonarr $arrroomsreverse)      {         $intdata[] = $arrroomsreverse['room_id'];     }     $introom = $intdata[array_rand($intdata)];     return $introom; } 

and call function:

echo returnarray(); 

Comments

Popular posts from this blog

java.util.scanner - How to read and add only numbers to array from a text file -

rewrite - Trouble with Wordpress multiple custom querystrings -