I have a multidimensional array that looks like the one below:
Array
(
[ok] => 1
[result] => Array
(
[0] => Array
(
[update_id] => 1001
[message] => Array
(
[message_id] => 3
[from] => Array
(
[id] => 123
[is_bot] =>
[first_name] => John
[last_name] => Doe
[username] => JohnDoe
[language_code] => de-DE
)
[chat] => Array
(
[id] => 123
[first_name] => John
[last_name] => Doe
[username] => JohnDoe
[type] => private
)
[date] => 1508065616
[text] => Hello
)
)
[1] => Array
(
[update_id] => 1002
[message] => Array
(
[message_id] => 4
[from] => Array
(
[id] => 456
[is_bot] =>
[first_name] => Jane
[language_code] => de-DE
)
[chat] => Array
(
[id] => 456
[first_name] => Jane
[type] => private
)
[date] => 1508067033
[text] => /start
[entities] => Array
(
[0] => Array
(
[offset] => 0
[length] => 6
[type] => bot_command
)
)
)
)
[2] => Array
(
[update_id] => 1003
[message] => Array
(
[message_id] => 5
[from] => Array
(
[id] => 456
[is_bot] =>
[first_name] => Jane
[language_code] => de-DE
)
[chat] => Array
(
[id] => 456
[first_name] => Jane
[type] => private
)
[date] => 1508067035
[text] => Hi
)
)
)
)
I want to get the update_id, the id, the first_name and the text of each of these arrays at the second level (0, 1, 2). Then I want to do something with this variables.
I was searching already in other threads but couldn't get it working right.
<?php
// First I am getting the messages from my Telegram bot
$botToken = "mySecretBotToken"; // removed the token for security reasons here
$website = "https://api.telegram.org/bot".$botToken;
$update = file_get_contents($website."/getUpdates");
$updateArray = json_decode($update, TRUE);
// For testing I was getting here the chatID of the first message
$chatID = $updateArray["result"][0]["message"]["chat"]["id"];
// Please ignore this. Here I was trying around a little bit with foreach
$newArray = array();
$i = 0;
foreach ($updateArray as $key => $value) {
if (is_array($value)) {
$newArray[$i] = array();
foreach ($value as $k => $v) {
$newArray[$i][] = $v;
}
$i++;
}
}
// Printing the chatID for test purposes and replying to the message
print_r($chatID);
file_get_contents($website."/sendmessage?chat_id=".$chatID."&text=test");
?>
Can you please help me?