简体   繁体   中英

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". 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. $b[$value[0]][] means it wil create a new element for array $b[$value[0]]. And $value[0] will be substituted by the first value in the element of $a that the foreach loop hits. Example: the first element of $a is this array: 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. The [] means that with every new execution of this line, a new element, with that key value $value[0], will be added.

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

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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