-2

I'm trying to make a custom management system for Telegram bots so that I can be able to send and read updates easily.. But my main problem is that I don't know how to retrieve data from an array. let me explain this in depth so you can understand what I'm talking about.

Basically when I try the getme command, I get this:

{"ok":true,"result":{"id":275700102,"first_name":"photoshop_post_production","username":"post_pro_bot"}}

So I converted this into an array using Php and it goes like this:

$_SESSION['website'] = "https://api.telegram.org/bot";
$_SESSION['url'] = "https://api.telegram.org/bot".$_SESSION['token'];

$_SESSION['me'] = file_get_contents($_SESSION['url']."/getme");
$_SESSION['meArray'] = json_decode($_SESSION['me'], TRUE);

So as you can see I have an array called meArray and if I print_r it ,this will be shown:

Array ( [ok] => 1 [result] => Array ( [id] => 275700102 [first_name] => photoshop_post_production [username] => post_pro_bot ) )

So basically I don't know how to take out the username or other information that I want from this array. I have tried several things but no success till now.

halfer
  • 19,824
  • 17
  • 99
  • 186
  • Take a quick look at almost any question in the JSON tag and you should get an answer – RiggsFolly Mar 12 '17 at 14:41
  • PHP is very array oriented, its the second thing you need to become familiar with once you have learned to spell PHP [The manual](http://php.net/manual/en/language.types.array.php) – RiggsFolly Mar 12 '17 at 14:44

1 Answers1

1

So, should be:

$_SESSION['meArray']['result']['username']

Try this.

Laischon
  • 37
  • 7
  • Thats not gonna work if u mean this: $_SESSION['username'] = $_SESSION['meArray']['username']; –  Mar 12 '17 at 14:51
  • Oh is true. I had noticed that he was saving the result in $_SESSION. However, it should be $_SESSION['meArray']['result']['username'] considering the print_r result... – Laischon Mar 12 '17 at 15:04
  • It would be a good idea to edit your answer and add the correct syntax, so other do not have to look at the comments to find the correct answer – RiggsFolly Mar 12 '17 at 16:20
  • Done! I'm a newbie on StackOverflow :) – Laischon Mar 12 '17 at 16:26