3

I have a string like:

{"Url":"http://localhost","DBName":"John_db","DBUser":"admin","Pass":"a"}

Now using this string I want URL, DBname, DBuser and pass them to separate variables like:

$DBName =  'John_db';
$DBUser =  'admin';
$Url    =  'http://localhost';
$Pass   = 'a';

I am new to PHP, and can't find any solution to achieve this, can anybody help me in this?

5 Answers 5

3

You don't really need to split each one into individual variables. You could just decode that JSON into an array or object:

$str = '{"Url":"http://localhost","DBName":"John_db","DBUser":"admin","Pass":"a"}';
$arr = json_decode( $str, true );

Now you have an associative array containing all of the variables:

Array(
  [ Url ] => "http://localhost",
  [ DBName ] => "John_db",
  ...
)

If you don't specify the second parameter to json_decode(), you'll get a regular object:

$obj = json_decode( $str );
echo $obj->Url; // http://localhost
echo $obj->DBName; // John_db

References -

Sign up to request clarification or add additional context in comments.

2 Comments

I really appreciate your effort!, Now i have one issue, in the string the key has a white space, like {"DB Name":"John_db"}. that time I can't take a value from the $obj, do you have any solution for that?
In that case you should make an associative array (my first example) by passing true to json_decode(). Then you'll be able to access that value by using: $arr[ "DB Name" ].
2

"This string" is JSON object. Use json_decode() to get array with all values and then get it from there.

$str = '{"Url":"http://localhost","DBName":"John_db","DBUser":"admin","Pass":"a"}';
$out = json_decode( $str, true );

and $out ends like:

Array
(
    [Url] => http://localhost
    [DBName] => John_db
    [DBUser] => admin
    [Pass] => a
)

Comments

2
$ar = json_decode( '{"Url":"http://localhost","DBName":"John_db","DBUser":"admin","Pass":"a"}', 1 );
foreach ($ar as $k => $a) {
   $$k = $a;
}

Now you should have your vars filled.

Working code here: http://codepad.org/Do9ixqfN

5 Comments

With list you have to be sure that JSON has every elements in the right position. You can't be sure about this.
True true true.. missed that!
I suppose you could sort the array before using list(), but that's over complicating things...
You're right, but JSON can miss a param and we return to the start of the problem, again :P
That's true... I suppose if you can't guarantee that all the values will always be there then it really isn't an option.
1

Use json_decode function like this:

<?
$string = '{"Url":"http://localhost","DBName":"John_db","DBUser":"admin","Pass":"a"}';
$array = json_decode( $string, true );

print_r($array);
?>

WORKING CODE

Comments

0

That string is a JSON object and you can use json_decode to convert that string into an associative array.

2 Comments

A little more explanation and perhaps some examples would really help to improve this answer.
@Lix, by the time i posted and started to expand, many others answered as well.

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.