Warning: file_get_contents(/data/phpspider/zhask/data//catemap/1/php/265.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181

Warning: file_get_contents(/data/phpspider/zhask/data//catemap/8/mysql/62.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Php 从多维数组中的特定索引构建数组_Php_Mysql_Arrays_Multidimensional Array - Fatal编程技术网

Php 从多维数组中的特定索引构建数组

Php 从多维数组中的特定索引构建数组,php,mysql,arrays,multidimensional-array,Php,Mysql,Arrays,Multidimensional Array,如何过滤掉包含MySQL查询返回的行名称的索引零,然后将结果放回如下所示的数组中 所需示例阵列: array:1 [ 0 => array:10 [ 0 => array:2 [ 0 => "2016-01-06" 1 => 10 ] 1 => array:2 [ 0 => "2016-01-12" 1 => 15 ] ] ] 从MySQL查询返回的数组: arra

如何过滤掉包含MySQL查询返回的行名称的索引零,然后将结果放回如下所示的数组中

所需示例阵列:

array:1 [
  0 => array:10 [
    0 => array:2 [
      0 => "2016-01-06"
      1 => 10
    ]
    1 => array:2 [
      0 => "2016-01-12"
      1 => 15
    ]
  ]
]
从MySQL查询返回的数组:

array:1 [
  0 => array:10 [
    0 => array:2 [
      0 => "price_1"
      1 => 10
    ]
    1 => array:2 [
      0 => "day_1"
      1 => "2016-01-06"
    ]
    2 => array:2 [
      0 => "price_2"
      1 => 15
    ]
    3 => array:2 [
      0 => "day_2"
      1 => "2016-01-12"
    ]
  ]
]

您可以这样做(假设
$array
是您的带有MySQL结果的输入数组,
$output
是我们转换的结果数组):

<?php

$output = array_map(function($value) {
    return [$value[1][1], $value[0][1]];
}, $array);
php > var_dump($array);
array(1) {
  [0]=>
  array(4) {
    [0]=>
    array(2) {
      [0]=>
      string(7) "price_1"
      [1]=>
      int(10)
    }
    [1]=>
    array(2) {
      [0]=>
      string(5) "day_1"
      [1]=>
      string(10) "2016-01-06"
    }
    [2]=>
    array(2) {
      [0]=>
      string(7) "price_2"
      [1]=>
      int(15)
    }
    [3]=>
    array(2) {
      [0]=>
      string(5) "day_2"
      [1]=>
      string(10) "2016-01-12"
    }
  }
}
php > $output = array_map(function($value) { return [$value[1][1], $value[0][1]]; }, $array);
php > var_dump($output);                                                        array(1) {
  [0]=>
  array(2) {
    [0]=>
    string(10) "2016-01-06"
    [1]=>
    int(10)
  }
}
php >