php-从多维数组(foreach)获取数据

php-从多维数组(foreach)获取数据,php,arrays,multidimensional-array,foreach,Php,Arrays,Multidimensional Array,Foreach,我有一个多维数组,如下所示: Array ( [ok] => 1 [result] => Array ( [0] => Array ( [update_id] => 1001 [message] => Array (

我有一个多维数组,如下所示:

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
                        )

                )

        )

)
我想在第二级(0、1、2)获取这些数组的update\u id、id、first\u name和文本。然后我想用这个变量做点什么

我已经在其他线程中搜索了,但无法使其正常工作

<?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");

?>


您能帮助我吗?

类似于您尝试的
foreach
循环:

$newArray = array();
foreach ($updateArray as $key => $value) {
  if (is_array($value)) {
    foreach ($value as $k => $v) {
      $newArray[$k]               = array();
      $newArray[$k]['update_id']  = $v['update_id'];
      $newArray[$k]['id']         = $v['message']['chat']['id'];
      $newArray[$k]['first_name'] = $v['message']['chat']['first_name'];
      $newArray[$k]['text']       = $v['message']['text'];
    }
  }
}
然后,您可以使用以下方式访问聊天室值:

foreach ( $newArray as $chat_info ) {
  print_r($chat_info['id']);
  file_get_contents(
    $website."/sendmessage?chat_id=".$chat_info['id']."&text=test"
  );
  // $chat_info['update_id']
  // $chat_info['first_name']
  // $chat_info['text']
}

下面解释一下您的
foreach
循环是如何工作的:

$newArray = array();
$i = 0;
foreach ($updateArray as $key => $value) {
  // $updateArray keys are ['ok' (not array), 'result' (array)]

  if (is_array($value)) { // only 'result' passes this test
    $newArray[$i] = array(); // this only happens once

    foreach ($value as $k => $v) { // loop over 'result'
      // 'result' keys are [0, 1, 2]

      $newArray[$i][] = $v; 
      // $newArray[0][0] = $v (first loop)
      // $newArray[0][1] = $v (second loop)
      // $newArray[0][2] = $v (third loop)
    }
    $i++; // this only happens once
  }
}
现在,您可以通过以下方式访问您的值:

foreach ( $newArray as $value ) { // only one value in the sample, key: 0
  foreach ( $value as $chat_info ) { // 3 values in the sample, keys: 0, 1, 2

    print_r($chat_info['message']['chat']['id']);
    file_get_contents(
      $website."/sendmessage?chat_id=" . $chat_info['message']['chat']['id'] . "&text=test"
    );

    // $chat_info['update_id']
    // $chat_info['message']['chat']['first_name']
    // $chat_info['message']['text']
  }    
}

与您尝试的
foreach
循环类似:

$newArray = array();
foreach ($updateArray as $key => $value) {
  if (is_array($value)) {
    foreach ($value as $k => $v) {
      $newArray[$k]               = array();
      $newArray[$k]['update_id']  = $v['update_id'];
      $newArray[$k]['id']         = $v['message']['chat']['id'];
      $newArray[$k]['first_name'] = $v['message']['chat']['first_name'];
      $newArray[$k]['text']       = $v['message']['text'];
    }
  }
}
然后,您可以使用以下方式访问聊天室值:

foreach ( $newArray as $chat_info ) {
  print_r($chat_info['id']);
  file_get_contents(
    $website."/sendmessage?chat_id=".$chat_info['id']."&text=test"
  );
  // $chat_info['update_id']
  // $chat_info['first_name']
  // $chat_info['text']
}

下面解释一下您的
foreach
循环是如何工作的:

$newArray = array();
$i = 0;
foreach ($updateArray as $key => $value) {
  // $updateArray keys are ['ok' (not array), 'result' (array)]

  if (is_array($value)) { // only 'result' passes this test
    $newArray[$i] = array(); // this only happens once

    foreach ($value as $k => $v) { // loop over 'result'
      // 'result' keys are [0, 1, 2]

      $newArray[$i][] = $v; 
      // $newArray[0][0] = $v (first loop)
      // $newArray[0][1] = $v (second loop)
      // $newArray[0][2] = $v (third loop)
    }
    $i++; // this only happens once
  }
}
现在,您可以通过以下方式访问您的值:

foreach ( $newArray as $value ) { // only one value in the sample, key: 0
  foreach ( $value as $chat_info ) { // 3 values in the sample, keys: 0, 1, 2

    print_r($chat_info['message']['chat']['id']);
    file_get_contents(
      $website."/sendmessage?chat_id=" . $chat_info['message']['chat']['id'] . "&text=test"
    );

    // $chat_info['update_id']
    // $chat_info['message']['chat']['first_name']
    // $chat_info['message']['text']
  }    
}

这应该不难


我想这里没有太多的解释,但如果有什么不清楚的地方,请随时询问。

这应该不难


我想这里没有太多的解释,但是如果有什么不清楚的地方,请随时询问。

阵列中的必填字段是什么阵列的格式始终与上面显示的格式相同。我从电报机器人API(GetUpdate)自动获取它。我想访问以下字段:update_id、id、first_name、text您需要的数组字段是什么数组的格式始终与上面显示的格式相同。我从电报机器人API(GetUpdate)自动获取它。我想访问以下字段:更新id、id、名字、文本