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

时间:2019-03-12 13:59:07

标签: php arrays

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

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.

2 个答案:

答案 0 :(得分:0)

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

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"}

如果需要,您可以轻松地将其解析为所需格式的数组。

答案 1 :(得分:0)

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

// 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"
    }
  }
}