Group elements in collections of arrays - php

I have this code, it fetches data from CSV file using splFileObject:
while(!$this->_file->eof()){
$data[$i] = $this->_file->fgetcsv();
}
This is the result :
array(12) {
[0]=>
array(1) {
[0]=>
string(41) "134550;651099595;3004050;1340.03;16/04/15"
}
[1]=>
array(1) {
[0]=>
string(41) "134333;651099594;3004051;1500.03;10/08/15"
}
[2]=>
array(1) {
[0]=>
string(41) "134550;651099595;3004050;1340.03;16/04/15"
}
[3]=>
array(1) {
[0]=>
string(41) "134333;651099594;3004051;1500.03;10/08/15"
}
}
What I want to do , is to group the arrays by collections of 2 (or whatever) like this (e.g of count = 2):
array(12) {
[0] =>
array(2){
[0]=>
array(1) {
[0]=>
string(41) "134550;651099595;3004050;1340.03;16/04/15"
}
[1]=>
array(1) {
[0]=>
string(41) "134550;651099595;3004050;1340.03;16/04/15"
}
}
[1] =>
array(2){
[0]=>
array(1) {
[0]=>
string(41) "134550;651099595;3004050;1340.03;16/04/15"
}
[1]=>
array(1) {
[0]=>
string(41) "134550;651099595;3004050;1340.03;16/04/15"
}
}
}

Sounds like a job for array_chunk()
$data = array_chunk($data, 2);

Related

How to get part of array excluding one key in multidimensional array?

I'm grouping one multidimensional array by age.
This is my code:
$mEmployees = array (
array("name"=>"Pedro", "age"=>20, "ID"=>1111),
array("name"=>"Carlos", "age"=>15, "ID"=>2222),
array("name"=>"Susana", "age"=>20, "ID"=>3333),
array("name"=>"Carmen", "age"=>19, "ID"=>4444)
);
$byAge=array();
foreach ($mEmployees as $k => $oneItem) {
$byAge[$oneItem['age']][$k] = $oneItem;
}
var_dump($byAge);
That works fine as you can see below:
output:
array(3) {
[20]=>
array(2) {
[0]=>
array(3) {
["name"]=>
string(5) "Pedro"
["age"]=>
int(20)
["ID"]=>
int(1111)
}
[2]=>
array(3) {
["name"]=>
string(6) "Susana"
["age"]=>
int(20)
["ID"]=>
int(3333)
}
}
[15]=>
array(1) {
[1]=>
array(3) {
["name"]=>
string(6) "Carlos"
["age"]=>
int(15)
["ID"]=>
int(2222)
}
}
[19]=>
array(1) {
[3]=>
array(3) {
["name"]=>
string(6) "Carmen"
["age"]=>
int(19)
["ID"]=>
int(4444)
}
}
}
But in the results, the age key is redundant. I want to remove this key in the $byAge array.
I tried with array_slice, but it's not possible to indicate one irregular offset (the key age is in middle).
How I can achieve this easily for this result?
array(3) {
[20]=>
array(2) {
[0]=>
array(3) {
["name"]=>
string(5) "Pedro"
["ID"]=>
int(1111)
}
[2]=>
array(3) {
["name"]=>
string(6) "Susana"
["ID"]=>
int(3333)
}
}
[15]=>
array(1) {
[1]=>
array(3) {
["name"]=>
string(6) "Carlos"
["ID"]=>
int(2222)
}
}
[19]=>
array(1) {
[3]=>
array(3) {
["name"]=>
string(6) "Carmen"
["ID"]=>
int(4444)
}
}
}
Cache the age value in a variable and unset from $oneItem.
foreach ($mEmployees as $k => $oneItem) {
$age = $oneItem['age'];
unset($oneItem['age']);
$byAge[$age][$k] = $oneItem;
}
Demo: https://3v4l.org/pDDn5

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

JSON signature to PDF using FPDF

The title of this question is 100% what I'm after.
I have a JSON signature which is stored like this:
{"lines":[[[228.95,21.4],[228.95,24.4],[227.95,32.4],[225.95,40.4],[223.95,47.4],[221.95,54.4],[219.95,61.4],[216.95,69.4],[214.95,74.4],[212.95,80.4],[210.95,88.4],[208.95,93.4],[207.95,99.4],[206.95,106.4],[205.95,113.4],[205.95,119.4],[204.95,125.4],[204.95,129.4],[203.95,132.4],[202.95,134.4],[202.95,136.4],[202.95,137.4],[202.95,138.4],[201.95,139.4],[200.95,140.4],[200.95,141.4],[200.95,142.4],[200.95,143.4]],[[207.95,27.4],[231.95,24.4],[244.95,22.4],[257.95,22.4],[270.95,24.4],[282.95,26.4],[293.95,30.4],[303.95,35.4],[309.95,39.4],[314.95,44.4],[315.95,47.4],[315.95,51.4],[315.95,56.4],[312.95,62.4],[308.95,68.4],[301.95,75.4],[293.95,81.4],[284.95,87.4],[277.95,92.4],[268.95,97.4],[261.95,101.4],[255.95,103.4],[249.95,105.4],[244.95,105.4],[240.95,106.4],[235.95,107.4],[233.95,107.4],[231.95,107.4],[230.95,107.4],[229.95,107.4],[231.95,107.4],[234.95,107.4],[238.95,107.4],[245.95,107.4],[254.95,107.4],[268.95,109.4],[290.95,114.4],[317.95,120.4],[348.95,128.4],[378.95,135.4],[399.95,140.4],[413.95,142.4],[421.95,143.4],[427.95,144.4],[429.95,144.4],[430.95,144.4],[429.95,144.4],[427.95,144.4],[426.95,144.4],[423.95,144.4]]]}
I can show the signature on an page using jQuery like:
$('#sig').signature('draw', <?php echo $signature_1; ?>);
I can't figure it out though how I can decode the JSON string and display the signature using FPDF.
using
var_dump(json_decode();
I get
object(stdClass)#1 (1) { ["lines"]=> array(2) { [0]=> array(28) { [0]=> array(2) { [0]=> float(228.95) [1]=> float(21.4) } [1]=> array(2) { [0]=> float(228.95) [1]=> float(24.4) } [2]=> array(2) { [0]=> float(227.95) [1]=> float(32.4) } [3]=> array(2) { [0]=> float(225.95) [1]=> float(40.4) } [4]=> array(2) { [0]=> float(223.95) [1]=> float(47.4) } [5]=> array(2) { [0]=> float(221.95) [1]=> float(54.4) } [6]=> array(2) { [0]=> float(219.95) [1]=> float(61.4) } [7]=> array(2) { [0]=> float(216.95) [1]=> float(69.4) } [8]=> array(2) { [0]=> float(214.95) [1]=> float(74.4) } [9]=> array(2) { [0]=> float(212.95) [1]=> float(80.4) } [10]=> array(2) { [0]=> float(210.95) [1]=> float(88.4) } [11]=> array(2) { [0]=> float(208.95) [1]=> float(93.4) } [12]=> array(2) { [0]=> float(207.95) [1]=> float(99.4) } [13]=> array(2) { [0]=> float(206.95) [1]=> float(106.4) } [14]=> array(2) { [0]=> float(205.95) [1]=> float(113.4) } [15]=> array(2) { [0]=> float(205.95) [1]=> float(119.4) } [16]=> array(2) { [0]=> float(204.95) [1]=> float(125.4) } [17]=> array(2) { [0]=> float(204.95) [1]=> float(129.4) } [18]=> array(2) { [0]=> float(203.95) [1]=> float(132.4) } [19]=> array(2) { [0]=> float(202.95) [1]=> float(134.4) } [20]=> array(2) { [0]=> float(202.95) [1]=> float(136.4) } [21]=> array(2) { [0]=> float(202.95) [1]=> float(137.4) } [22]=> array(2) { [0]=> float(202.95) [1]=> float(138.4) } [23]=> array(2) { [0]=> float(201.95) [1]=> float(139.4) } [24]=> array(2) { [0]=> float(200.95) [1]=> float(140.4) } [25]=> array(2) { [0]=> float(200.95) [1]=> float(141.4) } [26]=> array(2) { [0]=> float(200.95) [1]=> float(142.4) } [27]=> array(2) { [0]=> float(200.95) [1]=> float(143.4) } } [1]=> array(50) { [0]=> array(2) { [0]=> float(207.95) [1]=> float(27.4) } [1]=> array(2) { [0]=> float(231.95) [1]=> float(24.4) } [2]=> array(2) { [0]=> float(244.95) [1]=> float(22.4) } [3]=> array(2) { [0]=> float(257.95) [1]=> float(22.4) } [4]=> array(2) { [0]=> float(270.95) [1]=> float(24.4) } [5]=> array(2) { [0]=> float(282.95) [1]=> float(26.4) } [6]=> array(2) { [0]=> float(293.95) [1]=> float(30.4) } [7]=> array(2) { [0]=> float(303.95) [1]=> float(35.4) } [8]=> array(2) { [0]=> float(309.95) [1]=> float(39.4) } [9]=> array(2) { [0]=> float(314.95) [1]=> float(44.4) } [10]=> array(2) { [0]=> float(315.95) [1]=> float(47.4) } [11]=> array(2) { [0]=> float(315.95) [1]=> float(51.4) } [12]=> array(2) { [0]=> float(315.95) [1]=> float(56.4) } [13]=> array(2) { [0]=> float(312.95) [1]=> float(62.4) } [14]=> array(2) { [0]=> float(308.95) [1]=> float(68.4) } [15]=> array(2) { [0]=> float(301.95) [1]=> float(75.4) } [16]=> array(2) { [0]=> float(293.95) [1]=> float(81.4) } [17]=> array(2) { [0]=> float(284.95) [1]=> float(87.4) } [18]=> array(2) { [0]=> float(277.95) [1]=> float(92.4) } [19]=> array(2) { [0]=> float(268.95) [1]=> float(97.4) } [20]=> array(2) { [0]=> float(261.95) [1]=> float(101.4) } [21]=> array(2) { [0]=> float(255.95) [1]=> float(103.4) } [22]=> array(2) { [0]=> float(249.95) [1]=> float(105.4) } [23]=> array(2) { [0]=> float(244.95) [1]=> float(105.4) } [24]=> array(2) { [0]=> float(240.95) [1]=> float(106.4) } [25]=> array(2) { [0]=> float(235.95) [1]=> float(107.4) } [26]=> array(2) { [0]=> float(233.95) [1]=> float(107.4) } [27]=> array(2) { [0]=> float(231.95) [1]=> float(107.4) } [28]=> array(2) { [0]=> float(230.95) [1]=> float(107.4) } [29]=> array(2) { [0]=> float(229.95) [1]=> float(107.4) } [30]=> array(2) { [0]=> float(231.95) [1]=> float(107.4) } [31]=> array(2) { [0]=> float(234.95) [1]=> float(107.4) } [32]=> array(2) { [0]=> float(238.95) [1]=> float(107.4) } [33]=> array(2) { [0]=> float(245.95) [1]=> float(107.4) } [34]=> array(2) { [0]=> float(254.95) [1]=> float(107.4) } [35]=> array(2) { [0]=> float(268.95) [1]=> float(109.4) } [36]=> array(2) { [0]=> float(290.95) [1]=> float(114.4) } [37]=> array(2) { [0]=> float(317.95) [1]=> float(120.4) } [38]=> array(2) { [0]=> float(348.95) [1]=> float(128.4) } [39]=> array(2) { [0]=> float(378.95) [1]=> float(135.4) } [40]=> array(2) { [0]=> float(399.95) [1]=> float(140.4) } [41]=> array(2) { [0]=> float(413.95) [1]=> float(142.4) } [42]=> array(2) { [0]=> float(421.95) [1]=> float(143.4) } [43]=> array(2) { [0]=> float(427.95) [1]=> float(144.4) } [44]=> array(2) { [0]=> float(429.95) [1]=> float(144.4) } [45]=> array(2) { [0]=> float(430.95) [1]=> float(144.4) } [46]=> array(2) { [0]=> float(429.95) [1]=> float(144.4) } [47]=> array(2) { [0]=> float(427.95) [1]=> float(144.4) } [48]=> array(2) { [0]=> float(426.95) [1]=> float(144.4) } [49]=> array(2) { [0]=> float(423.95) [1]=> float(144.4) } } } }
Looking for help.
Thanks.
BB
In my opinion you should obtain an image of your signature an include it into the pdf.
You have two paths for doing this, client side and server side.
For server side there is this script for other js project, now discontinued but maybe usefull for you https://github.com/thomasjbradley/signature-to-image/ I don't now if your plugin generates de signature JSON in the same fashion that this one, but reading this code can help you doing the server work
Other server side solution is to send the signature in svg format from your plugin and convert it to pdf, this php project supports svg natively https://tcpdf.org/
In the client side, you can get the canvas image through
HTMLCanvasElement.toDataURL()
This generates an uri containing the base64 encoded image string that you can send to the server, but depends on the browser support of the feature
Info here https://developer.mozilla.org/en-US/docs/Web/API/HTMLCanvasElement/toDataURL
Good luck

Flatten multi dimension array but maintain keys?

I have the following:
[6199]=>
array(12) {
["Origin"]=>
array(3) {
["name"]=>
array(1) {
[0]=>
string(4) "Cuba"
}
["slug"]=>
array(1) {
[0]=>
string(27) "cuabn-havana-habanos-cigars"
}
["id"]=>
array(1) {
[0]=>
int(0)
}
}
["Filler"]=>
array(3) {
["name"]=>
array(2) {
[0]=>
string(9) "Dominican"
[1]=>
string(10) "Nicaraguan"
}
["slug"]=>
array(2) {
[0]=>
string(9) "dominican"
[1]=>
string(10) "nicaraguan"
}
["id"]=>
array(2) {
[0]=>
int(0)
[1]=>
int(1)
}
}
}
[6192]=>
array(11) {
["Origin"]=>
array(3) {
["name"]=>
array(1) {
[0]=>
string(9) "Nicaragua"
}
["slug"]=>
array(1) {
[0]=>
string(27) "nicaraguan-new-world-cigars"
}
["id"]=>
array(1) {
[0]=>
int(0)
}
}
["Filler"]=>
array(3) {
["name"]=>
array(2) {
[0]=>
string(9) "Java"
[1]=>
string(10) "Nicaraguan"
}
["slug"]=>
array(2) {
[0]=>
string(9) "java"
[1]=>
string(10) "nicaraguan"
}
["id"]=>
array(2) {
[0]=>
int(0)
[1]=>
int(1)
}
}
}
and my expected output is:
array(12) {
["Origin"]=>
array(3) {
["name"]=>
array(1) {
[0]=>
string(4) "Cuba".
[1]=>
string(9) "Nicaragua"
}
["slug"]=>
array(1) {
[0]=>
string(27) "cuabn-havana-habanos-cigars",
[0]=>
string(27) "nicaraguan-new-world-cigars"
}
["id"]=>
array(1) {
[0]=>
int(0)
}
}
["Filler"]=>
array(3) {
["name"]=>
array(2) {
[0]=>
string(9) "Dominican"
[1]=>
string(10) "Nicaraguan"
[2]=>
string(9) "Java"
}
["slug"]=>
array(2) {
[0]=>
string(9) "dominican"
[1]=>
string(10) "nicaraguan"
[3]=>
string(9) "java"
}
["id"]=>
array(2) {
[0]=>
int(0)
[1]=>
int(1)
}
}
See how it eliminates dupes and merges each array maintaining the "origin" key.
I've tried :
foreach ($resultterms as $keyname => $valuename){
foreach ($valuename as $keysub => $valuesub) {
foreach($valuesub['name'] as $keysubsub => $valuesubsub){
# code...
$prods_atts[$keysub]['name'][$keysubsub] = $valuesubsub;
$prods_atts[$keysub]['slug'][$keysubsub] = $valuesub['slug'][$keysubsub];
$prods_atts[$keysub]['id'][$keysubsub] = $valuesub['id'][$keysubsub];
}
}
}
where $resultterms is the original arrays but it's not working. I was wondering if there was a wonderful php function I could use to merge these instead of so many nested for each loops?
I believe you're just looking for array_merge_recursive.
call_user_func_array('array_merge_recursive', array_values($prod_atts));
call_user_func_array allows to transform an array into a list of arguments
array_values because in the end, you seem to want to get rid of the first layer of your array
In order to try it, could you post the var_export of your variable instead of the var_dump?
echo(var_export($prod_atts, true));
merge your array by any suggested method. After that you will get duplicated values. And you need save only the unique items
$new = array_merge_recursive($resultterms['6199'], $resultterms['6192']);
foreach($new['Origin'] as &$item) { $item = array_unique($item); }
foreach($new['Filler'] as &$item) { $item = array_unique($item); }

Group array row data by two columns creating a new 3-level structure

I have 1 arrays:
array(5) { // This is the keys on the CSV.
[0]=>
array(4) {
[0]=>
string(4) "user"
[1]=>
string(4) "date"
[2]=>
string(3) "md5"
[3]=>
string(4) "sha1"
}
[1]=>
array(4) {
[0]=>
string(4) "user1"
[1]=>
string(8) "02/02/15"
[2]=>
string(6) "123456"
[3]=>
string(5) "54321"
}
[2]=>
array(4) {
[0]=>
string(4) "user1"
[1]=>
string(8) "02/03/15"
[2]=>
string(6) "123456"
[3]=>
string(5) "54321"
}
[3]=>
array(4) {
[0]=>
string(5) "user2"
[1]=>
string(8) "02/02/15"
[2]=>
string(6) "112233"
[3]=>
string(6) "332211"
}
[4]=>
array(4) {
[0]=>
string(5) "user2"
[1]=>
string(8) "02/03/15"
[2]=>
string(6) "112244"
[3]=>
string(6) "332244"
}
}
So User 1 and User 2 each have a file that is MD5/SHA1 sum'd and checked.
I would like the data to turn from the above, to this:
array(3) {
["user"]=>
array(1) {
["date"]=>
array(2) {
[0]=>
string(3) "md5"
[1]=>
string(4) "sha1"
}
}
["user1"]=>
array(1) {
["02/02/15"]=>
array(2) {
[0]=>
string(6) "123456"
[1]=>
string(5) "54321"
}
["02/03/15"]=>
array(2) {
[0]=>
string(6) "123456"
[1]=>
string(5) "54321"
}
}
["user2"]=>
array(1) {
["02/02/15"]=>
array(2) {
[0]=>
string(6) "112233"
[1]=>
string(6) "332211"
}
["02/03/15"]=>
array(2) {
[0]=>
string(6) "112244"
[1]=>
string(6) "332244"
}
}
}
So far, I'm close. Here is my code:
<?php
error_reporting(E_ALL & ~E_NOTICE);
//Reading csv
$csv = array_map('str_getcsv',file('/var/www/sums.csv'));
debug($csv);
//User list
$userData = array();
foreach ($csv as $user) {
$ts = $user[1];
$un = $user[0];
$sums = array($user[2],$user[3]);
$userPreProc[$un] = array($ts => $sums);
$userData = array_merge($userPreProc, $userData);
}
debug($userData);
function debug($db) {
echo "<pre>";
var_dump($db);
echo "</pre>";
}
?>
The issue here is that only the first 'user1' and 'user2' entries are merged to the new array, so it looks like this:
array(3) {
["user"]=>
array(1) {
["date"]=>
array(2) {
[0]=>
string(3) "md5"
[1]=>
string(4) "sha1"
}
}
["user1"]=>
array(1) {
["02/02/15"]=>
array(2) {
[0]=>
string(6) "123456"
[1]=>
string(5) "54321"
}
}
["user2"]=>
array(1) {
["02/02/15"]=>
array(2) {
[0]=>
string(6) "112233"
[1]=>
string(6) "332211"
}
}
}
Any suggestions how I can ensure that all the keys are merged as expected?
Just shift off the first two elements and use those respective values as the first and second level keys.
Code: (Demo)
$result = [];
foreach ($array as $row) {
$result[array_shift($row)][array_shift($row)] = $row;
}
var_export($result);
Or use array destructuring in the foreach declaration: (Demo)
$result = [];
foreach ($array as [$user, $date, $md5, $sha1]) {
$result[$user][$date] = [$md5, $sha1];
}
var_export($result);
Just change your foreach loop to be like this. You don't need to merge your array via function.
foreach ($csv as $user) {
$ts = $user[1];
$un = $user[0];
$sums = array($user[2],$user[3]);
$userData[$un][$ts] = $sums;
}

Categories