2

Hi am having a little from with json array. when i try to get the values out of json array.

Controller

 $data['payment'] = $this->admin->get_payment_settings();
 $value = $data['payment'][0]->json;
 echo $value['username'];

Hi am having a json array in database. using my controller i am getting the filed json. When i do a var_dump($value) array look like this

 {"username":"foodi1.lch.co","password":"FBUEWQH6X4D","signature":"AFcWxV21C7fd0v3bZnWKgr9on9AmTuhyd4MVq","currency":"CAD"}

i want to get each value out of this array.

 echo $value['username'];
 echo $value['password'];

When i try to do this i get the error

A PHP Error was encountered
Severity: Warning
Message: Illegal string offset 'username'
Filename: controllers/administrator.php
Line Number: 620

Can some one help me to get the values out of json array. tnx..

asked Jun 9, 2015 at 16:10
2
  • $value=json_decode($value,true);echo $value['username']; Commented Jun 9, 2015 at 16:14
  • @ShaifulIslam tnx. that fixed my problem. plz add that as answer i will accept it. Commented Jun 9, 2015 at 16:16

2 Answers 2

2

You need to decode the string to a valid array, use json_decode as follows:

$json = json_decode($value, true);
echo $json['username'];
answered Jun 9, 2015 at 16:17

Comments

0

It seems your $data['payment'][0] is json string.So you need to decode it.

Try this way

$value=json_decode($data['payment'][0]->json,true);//second parameter true will return as array. 
echo $value['username'];
answered Jun 9, 2015 at 16:19

Comments

Your Answer

Draft saved
Draft discarded

Sign up or log in

Sign up using Google
Sign up using Email and Password

Post as a guest

Required, but never shown

Post as a guest

Required, but never shown

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.