【问题标题】:Get data of an array without timestamp获取不带时间戳的数组数据
【发布时间】:2021-07-31 05:20:42
【问题描述】:

如何在不使用时间戳的情况下从该数组中检索数据?

例如元素 1、元素 25、元素 45

{
    "1620066141": [21.5, 1117, 45, 52, 1010.5],
    "1620067941": [20.399999999999999, 738, 44, 53, 1010.4],
    "1620069741": [20.699999999999999, 961, 45, 54, 1010.2],
    "1620071541": [21.199999999999999, 1131, 45, 53, 1010.1],
[...]
}

喜欢:

$data->body->element[1]
$data->body->element[25]
$data->body->element[45]

【问题讨论】:

标签: php arrays timestamp


【解决方案1】:

设旧数组为$old,新数组为$new

foreach($old as key => $value){
    $new[] = $value; 
}

我想你也想将它添加到$data->body 使用:

$data->body->element = $new;

【讨论】:

【解决方案2】:

使用 Foreach 循环访问每个节点的数据。

foreach($datas as $data) {
     echo $data;
}

【讨论】:

    【解决方案3】:

    您可以使用array_values()。但是,array_values() 只接受一个数组。当你有一个对象时,你需要将它转换为一个数组。

    $json = <<< DATA
    {"1620066141": [21.5, 1117, 45, 52, 1010.5],
        "1620067941": [20.399999999999999, 738, 44, 53, 1010.4],
        "1620069741": [20.699999999999999, 961, 45, 54, 1010.2],
        "1620071541": [21.199999999999999, 1131, 45, 53, 1010.1]
    }
    DATA;
    $data = json_decode($json);
    
    $element = array_values((array)$data);
    
    print_r($element[0]);
    

    【讨论】:

      【解决方案4】:

      ES6中的Object.keys()和map()方法

      这是另一种可能的方式,在 ES6 中使用 Object.keys()map() 方法在一行中完成:

      let element = Object.keys(myObject).map(val => myObject[val]);
      

      // let's declare your object {} as variable
      const myObject = {
        "1620066141": [21.5, 1117, 45, 52, 1010.5],
        "1620067941": [20.399999999999999, 738, 44, 53, 1010.4],
        "1620069741": [20.699999999999999, 961, 45, 54, 1010.2],
        "1620071541": [21.199999999999999, 1131, 45, 53, 1010.1],
      };
      
      // Create an array of strings that represent all the enumerable
      // properties of the given object 
      // (here: 'timestamps' , which are your keys)
      //
      // Then:
      // Map that array of properties (keys) into their values 
      // (here: element[] -- the values without keys)
      
      let element = Object.keys(myObject).map(val => myObject[val]);
      
      console.log(element);
      
      // now you can access your data via index
      // element[0], element[1], etc ...
      
      console.log('first element:')
      console.log(element[0]);
      
      console.log('third element:')
      console.log(element[2]);

      您可以在以下位置阅读有关Object.keys() 方法的更多信息:

      MDN:https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/Object/keys

      您可以在以下位置阅读有关map() 方法的更多信息:

      MDN:https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/Array/map

      希望这能增加一些价值。

      【讨论】:

        【解决方案5】:

        谢谢! 我的解决方案:

        $data = json_decode($json, true);
        $element = array_values($data);
        print_r($element[0][2]);
        print_r($element[24][2]);
        print_r($element[44][2]);
        

        【讨论】:

          猜你喜欢
          • 1970-01-01
          • 1970-01-01
          • 2020-03-07
          • 1970-01-01
          • 2021-05-19
          • 1970-01-01
          • 2018-12-25
          • 1970-01-01
          • 2012-05-23
          相关资源
          最近更新 更多