Using php code

$client = new RandomORGClient('10fbcd24-2628-4ae0-ab50-0160e1ce5ae1'); $data = $client->generateSignedIntegers(1, 1, 60); $arr = array(); foreach($data as $randomInt) { array_push($arr, ($randomInt)); } $random_arr = json_encode($arr[1]); var_dump(json_encode($arr[1])); 

I get this array:

 "{"random":{"method":"generateSignedIntegers","hashedApiKey":"FZSXJR+2P+umSj1UIOvBEc3RpYuwh8ZQfqppk8O5froIsgTlcPWRmoYkKzLB07cCs4TXgMcZS+MduDyF0RK9+Q==","n":1,"min":1,"max":60,"replacement":true,"base":10,"data":[9],"completionTime":"2015-10-03 15:32:01Z","serialNumber":408},"signature":"zMvGKYYNgLXRJW8k4F+XlJ1Yw68R9tqcmReY7KXBgsqO9TpO+w1F82b18ihbUyaaQX\/kus+9styOZ\/\/xnjnmH0K\/LWWAbhzVtwed8Y2bToZdAUQRyYj+9zEFIr4iZLyRSxhNdLaAyd7V5w6uLAI5CrkyVJcqrBoFnqHsDFUA0\/VnLhA4sSlfW+EmASslQn\/a2HXVDwPixs+6tJIBlZz24vtvSeqxfDwWNrajAHdiaxSF9n3kGJufv6n8yzEasEM6LQ3gYBVE6XmtV8yyCFVxIl8XJpdBVtRMNhq\/fLU+fZNJs8Y2QGCwEu1kPkRKP2bKKR2gp6U8hX8FAhdjO7gu4RAQwngKekSXDYPUpz\/1XjhSWMPl7RMDk9Spsb9uQqocJ88d1fCOIXDsbjmruUUBpD3M9OGYwP\/1Juip6wNUNmkT1uZyJS0EwNrxVw8a5VxS58EVxjdhWUNGXi7sFX73yxpmAs751sqGFR0bFdauG8mPORvzH1jeskPJaULKjgRMcrP0Xx7a+vkr+nShb1bpNMFUA0+UOh3NQLlM\/VkyIeh+SilvuOE0P9sLUHzeUETxvXhImTjpAyb6ObvAxf7xzjVQQVBp4uRCgP6DonqymSAS\/Hzs8AhWQntFXk38PhXpoFmI4tSibeePR3KXLBQhlZaWjILE0sQJv6WBDCmhfFg=","bitsUsed":6,"bitsLeft":247666,"requestsLeft":610,"advisoryDelay":0}" 

Now I want to add this whole array to another array, using php to do this:

 $array = array('id' => $id_lot, 'time' => date('Ymd H:i:s'), 'winner' => array('login' => $_SESSION['personaname'], 'url' => $_SESSION['steamid'], 'image'=> $_SESSION['avatarmedium'], 'place' => '31', 'random' => $random_arr)); 

Added, though as a string and in the value ... :( That's what happened: enter image description here

What should come out: http://i.stack.imgur.com/hIPcW.png

How can I make this json string be inserted as a continuation in the $ array array? To transfer it to JS later. In the browser console on the 2nd screenshot I tried to show how it would look. And then he writes it in value and that's it ...

  • Use json_decode() to get an array from a JSON string - Roman Kozin
  • sandbox.onlinephpfunctions.com/code/… Here’s a sandbox for your solution. How would it normally translate a string into an array - Roman Kozin
  • And if you use new versions of PHP, then to declare an array, use [] . And json_decode/json_encode works with arrays of any nesting - Roman Kozin
  • @RomanKozin thank you very much, but if I use decode, then I just have an error in the file crashes. Now I will try to decode and then loop the values ​​I need to pull out. but I can’t even imagine how much I’m going to steam now. but I understood the essence of what needs to be done. I'll write about the result soon if it works. - private person

2 answers 2

It is necessary to bring your $random_arr to the form of a regular array:

 $random_arr = json_decode($random_arr,true); 

The second parameter is needed in order to get an associative array, by default json_decode leads JSON to stdObject and then the call to the elements occurs through ->название_параметра parameter_name. If the second parameter is set to true , then through ['название_параметра'] parameter_name ['название_параметра'] .

After casting, just add our $random_arr to the final array

 $array = array('id' => $id_lot, 'time' => date('Ymd H:i:s'), 'winner' => array('login' => $_SESSION['personaname'], 'url' => $_SESSION['steamid'], 'image'=> $_SESSION['avatarmedium'], 'place' => '31', 'random' => $random_arr) //вот наш массив ); 

    Tired of steaming with json, I decided to manually withdraw everything.

      $client = new RandomORGClient('-----------------------------'); $data = $client->generateSignedIntegers(1, 1, $maxUsers); $arr = array(); foreach($data as $randomInt) { array_push($arr, ($randomInt)); } $x_method = $arr[1]->random->method; $x_replacement = $arr[1]->random->replacement; $x_serialNumber = $arr[1]->random->serialNumber; $x_hashedApiKey = $arr[1]->random->hashedApiKey; $x_n = $arr[1]->random->n; $x_min = $arr[1]->random->min; $x_max = $arr[1]->random->max; $x_base = $arr[1]->random->base; $x_completionTime = $arr[1]->random->completionTime; $x_signature = $arr[1]->signature; $x_bitsUsed = $arr[1]->bitsUsed; $x_bitsLeft = $arr[1]->bitsLeft; $x_data = $arr[1]->random->data[0]; $x_requestsLeft = $arr[1]->requestsLeft; $x_advisoryDelay = $arr[1]->advisoryDelay;