繁体   English   中英

如何根据值将多维数组拆分为数组-PHP

[英]How to split multidimensional array into arrays based on the values - PHP

我有这个数组,它可能看起来像这样:

array(756) {
[0]=>
  array(2) {
  [0]=>
    string(12) "joint_temps5"
  [1]=>
    string(4) "23.5"
  }
[1]=>
  array(2) {
  [0]=>
    string(12) "joint_temps4"
  [1]=>
    string(4) "23.5"
  }
[2]=>
  array(2) {
  [0]=>
    string(12) "joint_temps3"
  [1]=>
    string(2) "24"
  }
[3]=>
  array(2) {
  [0]=>
    string(12) "joint_temps2"
  [1]=>
    string(4) "24.5"
  }
[4]=>
  array(2) {
  [0]=>
    string(12) "joint_temps1"
  [1]=>
    string(2) "25"
  }
[5]=>
  array(2) {
  [0]=>
    string(12) "joint_temps0"
  [1]=>
    string(4) "25.5"
  }
[6]=>
  array(2) {
  [0]=>
    string(12) "joint_temps5"
  [1]=>
    string(4) "23.5"
  }
[7]=>
  array(2) {
  [0]=>
    string(12) "joint_temps4"
  [1]=>
    string(4) "23.5"
  }
[8]=>
  array(2) {
  [0]=>
    string(12) "joint_temps3"
  [1]=>
    string(2) "24"
  }
[9]=>
  array(2) {
  [0]=>
    string(12) "joint_temps2"
  [1]=>
    string(4) "24.5"
  }
[10]=>
  array(2) {
  [0]=>
    string(12) "joint_temps1"
  [1]=>
    string(2) "25"
  }
[11]=>
  array(2) {
  [0]=>
    string(12) "joint_temps0"
  [1]=>
    string(4) "25.5"
}
etc...};

我将如何循环通过,并根据内部arrays [0]中的值将其拆分为多个数组,例如:“ joint_temps5”。 我已经测试了很多东西,但是没有成功。 我的问题主要是我不知道数组中的字符串可能是什么。

我想结束像这样的数组:

$array1[] = array(x_amount){
[0]=>
  array(2) {
  [0]=>
    string(12) "joint_temps5"
  [1]=>
    string(4) "23.5"
  }
[1]=>
  array(2) {
  [0]=>
    string(12) "joint_temps5"
  [1]=>
    string(4) "23.5"
  }
}


$array2[] = array(x_amount){
[0]=>
  array(2) {
  [0]=>
    string(12) "joint_temps4"
  [1]=>
    string(4) "23.5"
  }
[1]=>
  array(2) {
  [0]=>
    string(12) "joint_temps4"
  [1]=>
    string(4) "23.5"
  }
}
}
etc.

您可以遍历数组,并使用字符串作为键填充新数组,如下所示:

foreach ($array as $working_array) {
$new_array[$working_array[0]][] = $working_array[1]; }

这会给你一个像这样的数组:

$new_array["joint_temps5"]=> array(2) {
[0]=> "23.5"
[1]=> "23.5"}

如果需要,可以轻松地将其解析为一个数组。

我建议从输入数组创建一个新数组,使用该值作为要创建的数组的索引,如下所示:

// test-set: input array is $a
$a[0] = array("joint_temps5","23.5");
$a[1] = array("joint_temps3","24");
$a[2] = array("joint_temps2","24.5");
$a[3] = array("joint_temps1","25");
$a[4] = array("joint_temps0","25.5");
$a[5] = array("joint_temps5","23.5");
$a[6] = array("joint_temps4","23.5");
$a[7] = array("joint_temps3","24");
$a[8] = array("joint_temps2","24.5");
$a[9] = array("joint_temps1","25");

foreach($a as $key => $value){
 $b[$value[0]][] = $value;  // *Explained below
}

*“下面解释”:$ a是源数组,$ b是新创建的数组。 $ b [$ value [0]] []表示它将为数组$ b [$ value [0]]创建一个新元素。 $ value [0]将被foreach循环命中的$ a元素中的第一个值替换。 示例:$ a的第一个元素是以下数组:array(“ joint_temps5”,“ 23.5”)。 因此,在foreach循环中,文本“ joint_temps5”(foreach中的$ value [0])将用作键/索引来为数组$ b创建新元素。 []表示此行的每次新执行都会添加一个新元素,其键值为$ value [0]。

它将导致:

array(6) {
  ["joint_temps5"]=>
  array(2) {
    [0]=>
    array(2) {
      [0]=>
      string(12) "joint_temps5"
      [1]=>
      string(4) "23.5"
    }
    [1]=>
    array(2) {
      [0]=>
      string(12) "joint_temps5"
      [1]=>
      string(4) "23.5"
    }
  }
  ["joint_temps3"]=>
  array(2) {
    [0]=>
    array(2) {
      [0]=>
      string(12) "joint_temps3"
      [1]=>
      string(2) "24"
    }
    [1]=>
    array(2) {
      [0]=>
      string(12) "joint_temps3"
      [1]=>
      string(2) "24"
    }
  }
  ["joint_temps2"]=>
  array(2) {
    [0]=>
    array(2) {
      [0]=>
      string(12) "joint_temps2"
      [1]=>
      string(4) "24.5"
    }
    [1]=>
    array(2) {
      [0]=>
      string(12) "joint_temps2"
      [1]=>
      string(4) "24.5"
    }
  }
  ["joint_temps1"]=>
  array(2) {
    [0]=>
    array(2) {
      [0]=>
      string(12) "joint_temps1"
      [1]=>
      string(2) "25"
    }
    [1]=>
    array(2) {
      [0]=>
      string(12) "joint_temps1"
      [1]=>
      string(2) "25"
    }
  }
  ["joint_temps0"]=>
  array(1) {
    [0]=>
    array(2) {
      [0]=>
      string(12) "joint_temps0"
      [1]=>
      string(4) "25.5"
    }
  }
  ["joint_temps4"]=>
  array(1) {
    [0]=>
    array(2) {
      [0]=>
      string(12) "joint_temps4"
      [1]=>
      string(4) "23.5"
    }
  }
}

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM