[英]How to split multidimensional array into arrays based on the values - PHP
I have this array, it could look something like this: 我有这个数组,它可能看起来像这样:
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...};
How would i go about looping thru and splitting it up into arrays based on the value in the inner arrays[0] ex: "joint_temps5". 我将如何循环通过,并根据内部arrays [0]中的值将其拆分为多个数组,例如:“ joint_temps5”。 I have tested quite a few things but without success. 我已经测试了很多东西,但是没有成功。 My problem mainly is i dont know what might be in the string in the arrays. 我的问题主要是我不知道数组中的字符串可能是什么。
I would like to end up with arrays like: 我想结束像这样的数组:
$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.
You could loop through your array, and populate a new array using the string as a key, so something like: 您可以遍历数组,并使用字符串作为键填充新数组,如下所示:
foreach ($array as $working_array) {
$new_array[$working_array[0]][] = $working_array[1]; }
Which would give you an array something like : 这会给你一个像这样的数组:
$new_array["joint_temps5"]=> array(2) {
[0]=> "23.5"
[1]=> "23.5"}
If you needed to you could then parse that into an array in the format you desire quite easily. 如果需要,可以轻松地将其解析为一个数组。
I would recommend to create a new array from your input array, using the value as an index of the array to be created, like so: 我建议从输入数组创建一个新数组,使用该值作为要创建的数组的索引,如下所示:
// 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
}
*"Explained below": $a is the source array, $b is the newly created array. *“下面解释”:$ a是源数组,$ b是新创建的数组。 $b[$value[0]][] means it wil create a new element for array $b[$value[0]]. $ b [$ value [0]] []表示它将为数组$ b [$ value [0]]创建一个新元素。 And $value[0] will be substituted by the first value in the element of $a that the foreach loop hits. $ value [0]将被foreach循环命中的$ a元素中的第一个值替换。 Example: the first element of $a is this array: array("joint_temps5","23.5"). 示例:$ a的第一个元素是以下数组:array(“ joint_temps5”,“ 23.5”)。 So in the foreach loop, the text "joint_temps5" ($value[0] in the foreach) will be used as a key/index to create a new element for array $b. 因此,在foreach循环中,文本“ joint_temps5”(foreach中的$ value [0])将用作键/索引来为数组$ b创建新元素。 The [] means that with every new execution of this line, a new element, with that key value $value[0], will be added. []表示此行的每次新执行都会添加一个新元素,其键值为$ value [0]。
It will result in: 它将导致:
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.