1

In PHP i have array variable from another function like this $v->params:

(
    [{"username":"myusername","email":"myemail@gmail_com","phone":"0123456789","password":"abc123","fullname":"myfullname","register_ip":"127_0_0_1","country":"Qu\u1ed1c_Gia","birthday":"N\u0103m_sinh","gender":"male","bank_code":"Ng\u00e2n_h\u00e0ng","ip":"127_0_0_1","os":"Windows_10","device":"Computer","browser":"Mozilla_Firefox_77_0"}] => 
)

Now i want to access to it item, how can i code to access item value like this:

$password = $v->params->password; //myemail@gmail_com

I new with PHP thank you all

10
  • Take a moment to read through the editing help in the help center. Formatting on Stack Overflow is different than on other sites. The better your post looks, the easier it is for others to read and understand it. (Here's a good Markdown Tutorial as well). Commented Jun 16, 2020 at 17:25
  • you are doing encode and decode at same time $object = json_decode (json_encode($v->params), TRUE); can you explain it once Commented Jun 16, 2020 at 17:26
  • thank you, i want to access item of it array but dont known how, json_decode (json_encode($v->params), TRUE i follow on stackoverflow but still not luck Commented Jun 16, 2020 at 17:30
  • @ForgeWebDesign This is a pretty common practice to convert an array that contains JSON strings to an actual array. Commented Jun 16, 2020 at 17:30
  • @GrumpyCrouton nice with your comment, i want to access to it item in array as you explan: $v->params->username //myusername but dont known how to write code Commented Jun 16, 2020 at 17:37

1 Answer 1

1

The data seems the wrong way round as it's the key of the array rather than a value.

So using array_keys()[0] to get the first key and then json_decode this...

$data = json_decode(array_keys($v->params)[0]);

you can then use the $data object to get at the values...

echo $data->username;
Sign up to request clarification or add additional context in comments.

2 Comments

it return me with this: [16-Jun-2020 20:16:18 Europe/Berlin] PHP Fatal error: Uncaught Error: Cannot use object of type api as array in C:\userms\restful_api.php:147 Stack trace: #0 C:\userms\restful_api.php(129): restful_api::userms_register(Object(api)) #1 C:\userms\restful_api.php(105): restful_api->userms(Object(api)) #2 C:\userms\restful_api.php(42): restful_api->_process_api() #3 C:\userms\api.php(7): restful_api->__construct() #4 C:\userms\api.php(30): api->__construct() #5 {main} thrown in C:\userms\restful_api.php on line 147
Try it with $v->params instead.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.