PHP - Two dimensional array - php

I'm quite new to PHP and I'm havint some trouble understanding how I'm supposed to turn something like this:
Array
(
[name] => Array
(
[0] => Bob
[1] => Tom
[2] => Ryan
[3] => Lisa
[4] => Peter
)
[age] => Array
(
[0] => 23
[1] => 33
[2] => 43
[3] => 33
[4] => 29
)
)
Into this kind of an array:
Array
(
[person] => Array
(
[name] => Bob
[age] => 23
)
[person] => Array
(
[name] => Tom
[age] => 33
)
)
So I wan't to be able to have a person key, rather than name and age keys. And to put identical indexes from name and age into this person key. How would I go about achieving this?
Thanks.

It is impossible to have many person keys in array because keys have to be unique.
Just try with:
$input = [
'name' => ['Bob', 'Tom', 'Ryan', 'Lisa', 'Peter'],
'age' => [23, 33, 43, 33, 29],
];
$output = array_map(function($name, $age) {
return ['name' => $name, 'age' => $age];
}, $input['name'], $input['age']);
var_dump($output);
Output:
array (size=5)
0 =>
array (size=2)
'name' => string 'Bob' (length=3)
'age' => int 23
1 =>
array (size=2)
'name' => string 'Tom' (length=3)
'age' => int 33
2 =>
array (size=2)
'name' => string 'Ryan' (length=4)
'age' => int 43
3 =>
array (size=2)
'name' => string 'Lisa' (length=4)
'age' => int 33
4 =>
array (size=2)
'name' => string 'Peter' (length=5)
'age' => int 29

Easiest way is this:
// Create a main array for the 'people' to be stored in
$people = array();
// Create a person
$bob = array('name' => 'Bob', 'age' => 23);
// Add that person into the 'people' array
$people[] = $bob;
// Repeat as many times a necessary
$tom = array('name' => 'Tom', 'age' => 33);
$people[] = $tom;
$ryan = array('name' => 'Ryan', 'age' => 43);
$people[] = $ryan;
To see the output do:
var_dump($people);
Should produce something like:
array(3) {
[0]=>
array(2) {
["name"]=>
string(3) "Bob"
["age"]=>
int(23)
}
[1]=>
array(2) {
["name"]=>
string(3) "Tom"
["age"]=>
int(33)
}
[2]=>
array(2) {
["name"]=>
string(4) "Ryan"
["age"]=>
int(43)
}
}
This is because you can automatically append items to the end of arrays without needing to specify the key you wish to insert them at. This is what we're doing with the lines:
$people[] = $bob;
Essentially that says, add the variable '$bob' to the next available slot in the array. As the array is initially empty, that happens to be the '0' key, and so on.

The key, by definition, must be unique. What you can do is, make an array called $persons, with the following data:
Array (
[1] => Array (
[name] => Bob
[age] => 23
)
[2] => Array (
[name] => Tom
[age] => 33
)
...
)
You can do this as follows:
$persons = Array();
for($i = 0; $i < sizeof($arr["name"]); $i++) {
$persons[$i]["name"] = $arr["name"][$i];
$persons[$i]["age"] = $arr["age"][$i];
}
Sample output:
Array
(
[0] => Array
(
[name] => Bob
[age] => 23
)
[1] => Array
(
[name] => Tom
[age] => 33
)
[2] => Array
(
[name] => Ryan
[age] => 43
)
...
)

like
$array=new Array();
$array[]=array('name'=>'Jhon Doe','age'=>23]
and so on for each person you want to store.

Related

Merge first array particular key and value into second array

I have 2 array
$arr1 = Array (
[0] => Array ( [customer_id] => 1 [Expire] => 2019-05-14 [paid] => 1 )
[1] => Array ( [customer_id] => 2 [Expire] => 2019-06-20 [paid] => 0 ))
and
$arr2 = Array (
[0] => Array ( [id] => 3943 [customer_id] => 1 [Expire] => 2019-05-14 )
[1] => Array ( [id] => 3944 [customer_id] => 1[Expire] => 2019-05-14 )
[2] => Array ( [id] => 4713 [customer_id] => 2 [Expire] => 2019-06-20 )
)
and try to put first array key and value [paid]=>1 or 0 in second array if customer id and expire match like
Array (
[0] => Array ( [id] => 3943 [customer_id] => 1 [Expire] => 2019-05-14 [paid] => 1)
[1] => Array ( [id] => 3944 [customer_id] => 1 [Expire] => 2019-05-14 [paid] => 1)
[2] => Array ( [id] => 4713 [customer_id] => 2 [Expire] => 2019-06-20 [paid] => 0)
)
I try to merge array in php but not get exact what i want. Is there any php function to do it?.
Loop your second array $arr2, and find the index of the first array $arr1 where the column customer_id is the same as in the current iteration of $arr2. array_search() returns that index, which we can then use to fetch the paid index of that array. Then we append it to our array $arr2, by doing $a['paid'] = $paid;.
Since we loop the array by reference (the & before $a in the loop), the changes we make to it, will be applied back to the original array.
foreach ($arr2 as &$a) {
$customerID = $a['customer_id']; // This customer ID
$arr1_key = array_search($customerID, array_column($arr1, 'customer_id')); // Find the index of this customerID in the first array
$paid = $arr1[$arr1_key]['paid']; // Find the paid value matching that customer ID
$a['paid'] = $paid;
}
Live demo at https://3v4l.org/Meqtu
Update
If you need it to match the ID as well as the expiration date, then use array_filter() to fetch the array-element within $arr1 that matches the date and the ID. Using reset(), we use the first element in that array.
foreach ($arr2 as &$a) {
// Find the sub-array of $arr1 that matches this array's date and ID
$arr1_match = array_filter($arr1, function($v) use ($a) {
return $v['customer_id'] == $a['customer_id'] && $v['Expire'] == $a['Expire'];
});
// Get the first element from the result
$paid = reset($arr1_match)['paid'];
// Append the paid-value to this array (done by reference)
$a['paid'] = $paid;
}
Live demo at https://3v4l.org/mov6d
sometimes things can be done in hard way:)
<?php
$arr1 = [
['customer_id' => 1, 'Expire' => '2019-05-14', 'paid' => 1],
['customer_id' => 2, 'Expire' => '2019-06-20', 'paid' => 0],
];
$arr2 = [
['id' => 3943, 'customer_id' => 1, 'Expire' => '2019-05-14'],
['id' => 3944, 'customer_id' => 1, 'Expire' => '2019-05-14'],
['id' => 3945, 'customer_id' => 2, 'Expire' => '2019-05-14'],
['id' => 4713, 'customer_id' => 2, 'Expire' => '2019-06-20'],
];
foreach ($arr2 as &$item2) {
foreach ($arr1 as $item1) {
if (
$item2['customer_id'] === $item1['customer_id']
&& $item2['Expire'] === $item1['Expire']
) {
$item2['paid'] = $item1['paid'];
break;
}
}
}
unset($item2);
var_dump($arr2);
If you cannot change the layout of the first array I think it will be best to first create an intermediate array that keeps a record of all expire dates for every customer.
The following implementation does not require you to use a nested loop.
<?php
$arr1 = [
['customer_id' => 1, 'Expire' => '2019-05-14', 'paid' => 1],
['customer_id' => 2, 'Expire' => '2019-06-20', 'paid' => 0],
];
$arr2 = [
['id' => 3943, 'customer_id' => 1, 'Expire' => '2019-05-14'],
['id' => 3944, 'customer_id' => 1, 'Expire' => '2019-05-14'],
['id' => 3945, 'customer_id' => 2, 'Expire' => '2019-05-14'],
['id' => 4713, 'customer_id' => 2, 'Expire' => '2019-06-20'],
];
// Create a list of all paid, expiry dates, keyed by customer_id
$payed = [];
foreach ($arr1 as $item) {
if (!isset($payed[$item['customer_id']])) {
$payed[$item['customer_id']] = [];
}
$payed[$item['customer_id']][] = $item['Expire'];
}
// Lookup the customer and expire date for every entry
$arr2 = array_map(function($item) use ($payed) {
$item['paid'] = in_array($item['Expire'], $payed[$item['customer_id']] ?? []);
return $item;
}, $arr2);
Result:
Array
(
[0] => Array
(
[id] => 3943
[customer_id] => 1
[Expire] => 2019-05-14
[paid] => 1
)
[1] => Array
(
[id] => 3944
[customer_id] => 1
[Expire] => 2019-05-14
[paid] => 1
)
[2] => Array
(
[id] => 3945
[customer_id] => 2
[Expire] => 2019-05-14
[paid] =>
)
[3] => Array
(
[id] => 4713
[customer_id] => 2
[Expire] => 2019-06-20
[paid] => 1
)
)
See demo
This can fix the issue :
$arr1 = array(
["customer_id"=>1,"Expire"=> "2019-05-14", "paid"=>1],
["customer_id"=>2,"Expire"=> "2019-06-20", "paid"=>0]
);
$arr2 = array(
["id"=>3943, "customer_id"=>1,"Expire"=> "2019-05-14"],
["id"=>3944,"customer_id"=>2,"Expire"=> "2019-06-20"],
["id"=>4713,"customer_id"=>1,"Expire"=> "2019-05-14"]
);
$result= array();
function getRowByCustomerID($id, $array){
foreach($array as $value){
if($value['customer_id'] ==$id){
return $value;
}
}
return null;
}
foreach($arr2 as $subarr){
$object = getRowByCustomerID($subarr['customer_id'],$arr1 );
if(!is_null($object)){
$object['id']=$subarr['id'];
$result[]= $object;
}
}
var_dump($result);
the output is similar to what you are looking for :
array(3) {
[0]=>
array(4) {
["customer_id"]=>
int(1)
["Expire"]=>
string(10) "2019-05-14"
["paid"]=>
int(1)
["id"]=>
int(3943)
}
[1]=>
array(4) {
["customer_id"]=>
int(2)
["Expire"]=>
string(10) "2019-06-20"
["paid"]=>
int(0)
["id"]=>
int(3944)
}
[2]=>
array(4) {
["customer_id"]=>
int(1)
["Expire"]=>
string(10) "2019-05-14"
["paid"]=>
int(1)
["id"]=>
int(4713)
}
}

How to extract values from multidimensional array grouped by some value inside it using PHP?

I have an array that looks like this:-
Array (
[0] => Array ( [id] => 10 [group] => 11 )
[1] => Array ( [id] => 11 [group] => 13 )
[2] => Array ( [id] => 12 [group] => 13 )
[3] => Array ( [id] => 13 [group] => 13 )
[4] => Array ( [id] => 14 [group] => 16 )
[5] => Array ( [id] => 15 [group] => 16 )
[6] => Array ( [id] => 16 [group] => 16 )
)
For each different group in this array i want to create array that stores id's.
In my example i have 3 groups, so i want to get 3 arrays like this:
Array ( [0] => 10)
Array ( [0] => 11
[1] => 12
[2] => 13)
Array ( [0] => 14
[1] => 15
[2] => 16)
Is it possible if it is how can i do it?
This should help you get started.
https://iconoun.com/demo/temp_icold.php
<?php // demo/temp_icold.php
/**
* Manipulate multi-dimensional arrays
*
* https://stackoverflow.com/questions/45422046/how-to-extract-values-from-multidimensional-array-grouped-by-some-value-inside-i
*/
error_reporting(E_ALL);
echo '<pre>';
$original = Array (
'0' => Array ( 'id' => 10, 'group' => 11 ),
'1' => Array ( 'id' => 11, 'group' => 13 ),
'2' => Array ( 'id' => 12, 'group' => 13 ),
'3' => Array ( 'id' => 13, 'group' => 13 ),
'4' => Array ( 'id' => 14, 'group' => 16 ),
'5' => Array ( 'id' => 15, 'group' => 16 ),
'6' => Array ( 'id' => 16, 'group' => 16 ),
);
print_r($original);
foreach ($original as $arr)
{
$ndx = 'group' . $arr['group'];
$out[$ndx][] = $arr['id'];
}
print_r($out);
You can achieve this through many different methods, but the simplest is probably a foreach() loop. In the example below I am looping through $a (your sample array) and building up a new array called $grouped with the index as the group attributes value.
In my example I am not duplicating the ID's inside of $group. If you wanted duplicates you could remove the second if statement.
$a = [
['id' => 10, 'group' => 11],
['id' => 11, 'group' => 13],
['id' => 12, 'group' => 13],
['id' => 13, 'group' => 13],
['id' => 14, 'group' => 16],
['id' => 15, 'group' => 16],
['id' => 16, 'group' => 16],
];
$grouped = [];
foreach ($a as $entry) {
if (! array_key_exists($entry['group'], $grouped)) {
$grouped[$entry['group']] = [];
}
if (! in_array($entry['id'], $grouped[$entry['group']])) {
$grouped[$entry['group']][] = $entry['id'];
}
}
var_dump($grouped);
The example outputs the following:
array(3) {
[11]=>
array(1) {
[0]=>
int(10)
}
[13]=>
array(3) {
[0]=>
int(11)
[1]=>
int(12)
[2]=>
int(13)
}
[16]=>
array(3) {
[0]=>
int(14)
[1]=>
int(15)
[2]=>
int(16)
}
}
$result_array = array();
foreach($array_name as $sub_array){
$result_array[$sub_array['group']][] = $sub_array['id'];
}
This will loop through your input array and create a result two dimensional array indexed by your group values. To extract a group result just index for the group as such: $result_array['GROUP_NUMBER'];
Here is my take on this:
<?php
$arr = array(
array("id"=>10,"group"=>11),
array("id"=>11,"group"=>13),
array("id"=>12,"group"=>13),
array("id"=>13,"group"=>13),
array("id"=>14,"group"=>16),
array("id"=>15,"group"=>16),
array("id"=>16,"group"=>16)
);
echo "<pre>".print_r(groupsToArrays($arr),true)."</pre>";
function groupsToArrays($arr, $groupkey = "group") {
$main = array();
$group = 0;
$arr[] = array("id"=>"end", $groupkey => "0");
foreach($arr as $key => $value) {
if($group != $value[$groupkey]) {
if($key != 0) $main[$group] = $tempArray;
if($value['id'] == "end") continue;
$group = $value[$groupkey];
$tempArray = array();
}
$tempArray[] = $value;
}
return $main;
}
This function will loop through your array and check the $groupkey key and will add every match to it's own array.
This is the return:
Array
(
[11] => Array
(
[0] => Array
(
[id] => 10
[group] => 11
)
)
[13] => Array
(
[0] => Array
(
[id] => 11
[group] => 13
)
[1] => Array
(
[id] => 12
[group] => 13
)
[2] => Array
(
[id] => 13
[group] => 13
)
)
[16] => Array
(
[0] => Array
(
[id] => 14
[group] => 16
)
[1] => Array
(
[id] => 15
[group] => 16
)
[2] => Array
(
[id] => 16
[group] => 16
)
)
)
So now you can access that group array by doing:
$groups = groupsToArrays($arr);
print_r($groups[$groupID]);

PHP : merge multi arrays in sections

I load data from .xlsx sheet
And i convert it to two arrays
The first is the header columns
The second is the data columns
I did this
$c = array_combine($headers, $data);
when i print $c
Array
(
[id] => Array
(
[0] => 1
[1] => 2
)
[f-name] => Array
(
[0] => Mohammed
[1] => Ziad
)
[s-name] => Array
(
[0] => Amer
[1] => Mohammed
)
[t-name] => Array
(
[0] => Hendy
[1] => Shokry
)
[cid] => Array
(
[0] => 89
[1] => 55
)
)
i want to make the result like that
array(
[0] => Array(
[id] => 0
[f-name] => mohammed
[s-name] => amer
[t-name] => hendy
[cid] => 89
)
[1] => Array(
[id] => 1
[f-name] => ziad
[s-name] => mohammed
[t-name] => shokry
[cid] => 55
)
)
try this:
$data = array(
'id' => array
(
0 => 1,
1 => 2
),
'f-name' => array
(
0 => 'Mohammed',
1 => 'Ziad'
),
's-name' => array
(
0 => 'Amer',
1 => 'Mohammed'
),
't-name' => array
(
0 => 'Hendy',
1 => 'Shokry'
),
'cid' => array
(
0 => 89,
1 => 55
)
);
//
$result = array();
foreach($data as $k=>$v){
for($i=0;$i<count($data['id']);$i++){
//$v[$i] = ($k!='id') ?: $i;// uncomment for reset id from 0
$result[$i][$k] = $v[$i];
}
}
var_dump($result);
result:
array (size=2)
0 =>
array (size=5)
'id' => int 1
'f-name' => string 'Mohammed' (length=8)
's-name' => string 'Amer' (length=4)
't-name' => string 'Hendy' (length=5)
'cid' => int 89
1 =>
array (size=5)
'id' => int 2
'f-name' => string 'Ziad' (length=4)
's-name' => string 'Mohammed' (length=8)
't-name' => string 'Shokry' (length=6)
'cid' => int 55
Try something like this:
for ($index = 0; &index < 2; ++$index)
{
thing['id'] = $index;
thing['f-name'] = $c['f-name'];
thing['l-name'] = $c['s-name'];
thing['t-name'] = $c['t-name'];
thing['cid'] = $c['cid'];
$newArra y[$index] = thing;
}
I like to include "fancy" array functions like array_walk and array_map whenever I have the chance:
Initialize your data
$data = [
'id' => [
0 => 1,
1 => 2
],
'f-name' => [
0 => 'Mohammed',
1 => 'Ziad'
],
's-name' => [
0 => 'Amer',
1 => 'Mohammed'
],
't-name' => [
0 => 'Hendy',
1 => 'Shokry'
],
'cid' => [
0 => 89,
1 => 55
]
];
Transform the data
$result = [];
array_walk($data['id'],
function($f, $i) use ($data, &$result){
$result[$i] = array_map(function($e) use ($i){
return $e[$i];
}, $data);
}
);
Output the result
var_dump($result);
array(2) {
[0]=>
array(5) {
["id"]=>
int(1)
["f-name"]=>
string(8) "Mohammed"
["s-name"]=>
string(4) "Amer"
["t-name"]=>
string(5) "Hendy"
["cid"]=>
int(89)
}
[1]=>
array(5) {
["id"]=>
int(2)
["f-name"]=>
string(4) "Ziad"
["s-name"]=>
string(8) "Mohammed"
["t-name"]=>
string(6) "Shokry"
["cid"]=>
int(55)
}
}

Merge two array with specific id, then , order this new array alphabetically

I have to array and I want to merge this two array with a main key ( ID ) , and I would like to order alphabetically this NEW array ( the lastname field )
Array (
[0] =>
Array ( [id] => 172
[user_id] => 1217330
[behaviour_action_id] => 97
[state] => accepted
)
[1] =>
Array (
[id] => 173
[user_id] => 1217330
[behaviour_action_id] => 97
[state] => pending
) )
And this array
Array (
[1217330 ] =>
Array ( [firstname] => Philip
[lastname] => Audet
[birthdate] => 1995-07-17
[id] => 1217330
)
[232323] =>
Array ( [firstname] => Frédéric
[lastname] => Bouchard-Dubé
[birthdate] => 1995-07-17
[id] => 232323
)
And I would like to have this
[0] =>
Array ( [id] => 172
[user_id] => 1217330
[behaviour_action_id] => 97
[state] => accepted
[firstname] => Philip
[lastname] => Audet
)
[1] =>
Array (
[id] => 173
[user_id] => 1217330
[behaviour_action_id] => 97
[state] => pending
[firstname] => Philip
[lastname] => Audet
) )
I wont want to have the birthdate index for THIS array, I only want to get the firstname and the lastname from the second array. So I want to math the index USER_ID ( first_table) with ID (second table). I also want to order this new table alphabetically with the lastname (in my exemple, I don't need to sort it alphabetically, but I will need to do this )
Can some one can help me please ? Thx
Thx
This is doing what you are looking for. As your example is out of context you might need to take some different approaches, but I hope it will help you get the task done.
$behaviour = array(
array(
'id' => 172,
'user_id' => 1217330,
'behaviour_action_id' => 97,
'state' => 'accepted'
),
array(
'id' => 173,
'user_id' => 232323,
'behaviour_action_id' => 97,
'state' => 'pending'
),
);
$users = array(
1217330 => array(
'firstname' => 'Philip',
'lastname' => 'Audet',
'birthdate' => '1995-07-17',
'id' => 1217330
),
232323 => array(
'firstname' => 'Frédéric',
'lastname' => 'Bouchard-Dubé',
'birthdate' => '1995-07-17',
'id' => 232323
),
);
//we will collect the data in a new array
$combined = array();
//now we loop through all behaviours
foreach($behaviour as $key => $behaviourData){
//we look up the data which belongs to the user of this behaviour
$wantedUserData = $users[$behaviourData['user_id']];
//birthdate is unwanted
unset($wantedUserData['birthdate']);
//merge data
$combined[] = array_merge($behaviourData, $wantedUserData);
}
//order array
usort($combined,'cmp');
//voilà!
var_dump($combined);
//Comparison function used in usort above
function cmp($a, $b){
if ($a['lastname'] == $b['lastname']){
return 0;
}
return ($a['lastname'] < $b['lastname']) ? -1 : 1;
}
$arr1 = array (
array("id"=>172, "user_id"=>1217330, "behaviour_action_id"=>97, "state"=>"accepted"),
array("id"=>173, "user_id"=>1217330, "behaviour_action_id"=>97, "state"=>"pending")
);
$arr2 = array(
"1217330" => array(
"firstname" => "Philip",
"lastname" => "Audet",
"birthdate" => "1995-07-17",
"id" => 1217330
),
"232323" => array (
"firstname" => "Frédéric",
"lastname" => "Bouchard-Dubé",
"birthdate" => "1995-07-17",
"id" => 232323
)
);
foreach($arr1 as $arr) {
$extra = $arr2[$arr["user_id"]];
unset($extra["birthdate"]);
$newarray[] = array_merge($extra, $arr);
}
print_r($newarray);
this one? (assuming $arr1 and $arr2 the given arrays)
$arr3 = array();
foreach($arr1 as $key=>$val) {
$arr3[$key] = $val;
$arr3[$key]["firstname"] = $arr2[$val["user_id"]]["firstname"];
$arr3[$key]["lastname"] = $arr2[$val["user_id"]]["lastname"];
}
echo "<pre>";
print_r($arr3);
echo "</pre>";
Well, I don't want to give a cooked-up code but this algorithm will do the job:
1) Loop through first array and create another array with user_id as key. like
$new_array[$array['user_id']] = array($array['id'], $array['state']...);
2) Loop through second array and unset() the key birthdate.
3) Use array_merge_recursive() and merge the two arrays.

Reindex and fill out array depending on another array

I'm trying to create a tables with information from two arrays. Here are the two arrays:
First array, for table headers
Array
(
[0] => Color
[1] => Length
[2] => Waist
)
Second array, the one that needs modification
Array
(
[0] => Array
[0] => green [1] => Color
[1] => Array
[0] => 23 [1] => Length
)
Array
(
[0] =>
)
Array
(
[0] => Array
[0] => 23 [1] => Length
[1] => Array
[0] => 24 [1] => Waist
)
Array needs to look like this:
Array
(
[0] => Array
[0] => green [1] => Color
[1] => Array
[0] => 23 [1] => Length
[2] => Array
[0] => [1] => Waist
Array
(
[0] => Array
[0] => [1] => Color
[1] => Array
[0] => [1] => Length
[2] => Array
[0] => [1] => Waist
Array
(
[0] => Array
[0] => [1] => Color
[1] => Array
[0] => 23 [1] => Length
[2] => Array
[0] => 24 [1] => Waist
So the point is that the keys in the first level needs to match the keys in the array that makes the table headers, where [1] one the second level has the same value as the table header. Any ideas?
After some feedback, an alternative acceptable output array structure would be:
array(
array(
'Color' => 'green',
'Length' => 23,
'Waist' => null
),
array(
'Color' => null,
'Length' => null,
'Waist' => null
),
array(
'Color' => null,
'Length' => 23,
'Waist' => 24
)
)
You have a complex array structure for an easy set of data. Could your final array work better like this?
$data = array(
array(
'Color' => 'green',
'Length' => 23,
'Waist' => NULL
),
array(
'Color' => NULL,
'Length' => NULL,
'Waist' => NULL
),
array(
'Color' => NULL,
'Length' => 23,
'Waist' => 24
)
);
If you're dead set on your structure, though, this should work:
function format_my_array($keys, $malformed) {
foreach ($malformed as $key => $fragments) {
$temp = array(
'Color' => NULL,
'Length' => NULL,
'Waist' => NULL
);
foreach ($fragments as $fragment) {
if (isset($fragment[1])) {
switch($fragment[1]) {
case 'Length':
$temp['Length'] = $fragment[1];
break;
case 'Waist':
$temp['Waist'] = $fragment[1];
break;
default:
$temp['Color'] = $fragment[1];
break;
}
}
}
$malformed[$key] = array(
array($temp['Color'], 'Color'),
array($temp['Length'], 'Length'),
array($temp['Waist'], 'Waist')
);
}
return $malformed;
}
Generate the default values (once) as a re-usable array, then iterate your input data and overwrite the default values with each item's set of "attributes".
Code: (Demo) (PHP7.4+ Demo)
$headers = ['Color', 'Length', 'Waist'];
$data = [
[
['green', 'Color'],
['23', 'Length'],
],
[],
[
['23', 'Length'],
['24', 'Waist'],
],
];
$defaults = array_fill_keys($headers, null);
var_export(
array_map(
function($item) use($defaults) {
return array_replace($defaults, array_column($item, 0, 1));
},
$data
)
);

Categories