2

I think I have missed the obvious somewhere, what is the best way to parse parameters which are in the format...

'{"phonenumber":"123456", "mobile":"589521215", "website":"www.xfty.co.uk" }'

so that I have the individual variables?

1
  • 1
    take a look at json_decode() Commented Dec 8, 2015 at 10:00

2 Answers 2

5

If you have a string with JSON content:

$string = '{"phonenumber":"123456","mobile":"589521215","website":"www.xfty.co.uk"}';

You can parse it and get an associative array with json_decode:

$array = json_decode( $string, true );

And access the array with:

$array["phonenumber"] //will give back '123456'

Or, if you prefer to get an stdClass object, just use:

$object = json_decode( $string ); 

And get it with:

$object->phonenumber; //will give back '123456'
Sign up to request clarification or add additional context in comments.

Comments

1

Your input looks like json format. You should use json_decode() to access it's values:

<?php
$values = json_decode('{"phonenumber":"123456","mobile":"589521215","website":"www.xfty.co.uk"}');

echo "Phonenumber:" . $values->phonenumber . "<br/>";
echo "Mobile:" . $values->mobile . "<br/>";
echo "Website:" . $values->website . "<br/>";

1 Comment

Thanks guys, I had not worked out what the format was called and therefore wasn't able to search for info. I'm sure I can move forward from here - Thanks again.

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.