首页 > 解决方案 > 如何根据值将多维数组拆分为数组 - 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...};

我将如何循环并根据内部数组 [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.

标签: phparrays

解决方案


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

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

推荐阅读