1
$data = file_get_contents("API LINK HERE");
$json = json_decode($data);

echo($json->zhohar->name);

What I want to do is replace zhohar in the echo with a variable which is defined by user input. So in the end I have something like this

$username = $_POST['username'];

$data = file_get_contents("API LINK HERE");
$json = json_decode($data);

echo($json->$username->name);

But this obviously doesn't work. Anyone has an idea how to solve this?

1

1 Answer 1

1

You can do something like this:

echo $json->{$username}->name

Or:

$json = json_decode( $data, true );
echo $json[$username]['name'];
Sign up to request clarification or add additional context in comments.

2 Comments

@dm03514 You're right, my bad.
@vplusm Welcome, but actually I shouldn't answer this one as it is duplicated and has been already answered before.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.