merge two array's key value of same index - php

i have 2 arrays, i have to merge array within same index.
Array
(
[0] => Array
(
[vendor_name] => MIRAGE PET PRODUCTS
[count_es] => 86
[outofstalk] => 19
[listing] => 64
[pricing] => 2
[discontinued] => 1
[others] => 0
)
[1] => Array
(
[vendor_name] => THE HOME DEPOT
[count_es] => 1
[outofstalk] => 0
[listing] => 1
[pricing] => 0
[discontinued] => 0
[others] => 0
)
)
this is first array and
Array
( [0] => Array
(
[incorrect_esc_count] => 1
)
[1] => Array
(
[incorrect_esc_count] => 0
)
)
this is second array .
I want that result
Array
(
[0] => Array
(
[vendor_name] => MIRAGE PET PRODUCTS
[count_es] => 86
[outofstalk] => 19
[listing] => 64
[pricing] => 2
[discontinued] => 1
[others] => 0
[incorrect_esc_count] => 1
)
[1] => Array
(
[vendor_name] => THE HOME DEPOT
[count_es] => 1
[outofstalk] => 0
[listing] => 1
[pricing] => 0
[discontinued] => 0
[others] => 0
[incorrect_esc_count] => 0
)
)
How can I achieve that? `The first array is getting from one variable and second array is getting from another variable. I want this sholud be in one array because for view i use angular.js and using for table

You are trying to merge two multidimensional associative arrays into one. This code should work for you, with some comments:
<?php
// declare the first array, I did not add all of the key-value pairs
$marr1 = array
(
array("vendor_name"=>"MIRAGE PET PRODUCTS","count_es"=>86,"outofstalk"=>19),
array("vendor_name"=>"THE HOME DEPOT","count_es"=>1,"outofstalk"=>0)
);
// declare second array
$marr2 = array
(
array("incorrect_esc_count"=>1),
array("incorrect_esc_count"=>0)
);
// declare third empty array to hold the result
$marr3 = array();
// loop through both arrays
// this will not work if your arrays have different lengths
for ($i = 0; $i < count($marr1); $i++) {
// merge the ith elements of both array and then push that array into the third array
array_push($marr3, array_merge($marr1[$i],$marr2[$i]));
}
// print out result
print_r($marr3);
?>

Related

Find if two different keys have the same value in 2 arrays PHP

My arrays are:
Array1
(
[0] => Array
(
[id] => 2
[name] => Melamine
[deleted] => 0
)
[1] => Array
(
[id] => 4
[name] => Vinyl
[deleted] => 0
)
[2] => Array
(
[id] => 5
[name] => Polyu
[deleted] => 0
)
)
Array2
(
[0] => Array
(
[productFinish] => 29
[type] => 2
)
[1] => Array
(
[productFinish] => 29
[type] => 4
)
)
So, i would like to return first array if id of 1st array matches with type of another array. In this case, first 2 indexes of first array must come out in return.
Thanks
You can use array_uintersect to get the results you want, supplying a callback function that compares the id value in array1 with the type value in array2:
$result = array_uintersect($array1, $array2, function ($a1, $a2) {
return ($a1['id'] ?? $a1['type']) - ($a2['type'] ?? $a2['id']);
});
print_r($result);
Note that because the callback is also called with values exclusively from $array1 or $array2 (for sorting), we have to allow for that in the comparison expression.
Output:
Array
(
[0] => Array
(
[id] => 2
[name] => Melamine
[deleted] => 0
)
[1] => Array
(
[id] => 4
[name] => Vinyl
[deleted] => 0
)
)
Demo on 3v4l.org
Ok, i got it with for loop.
$newTypeFilter = [];
for($i=0; $i < count($arra1); $i++){
for($j=0;$j<count($arra2); $j++){
if($arra1[$i]['id'] == $arra2[$j]['type']){
$newTypeFilter[] = $arra1[$i];
}
}
}
Any other answers will be appreciated. Thanks

Dynamically combine subsets of data with their header row, then group/merge/flatten datasets by a shared column

I have an array of arrays like this which I get from a server's response:
$data = array (
'data1' => array (
0 =>
array (
0 => 'ID',
1 => 'PinCode',
2 => 'Date',
),
1 =>
array (
0 => '101',
1 => '454075',
2 => '2012-03-03',
),
2 =>
array (
0 => '103',
1 => '786075',
2 => '2012-09-05',
),
),
'data2' => array (
0 =>
array (
0 => 'Balance',
1 => 'ID',
),
1 =>
array (
0 => '4533',
1 => '101',
)
),
'data3' => array (
0 =>
array (
0 => 'Active',
1 => 'ID',
),
1 =>
array (
0 => 'Yes',
1 => '101',
),
2 =>
array (
0 => 'No',
1 => '103',
),
),
);
On the above array I perform kind of a left join operation to get the final output below :
Array
(
[0] => Array
(
[ID] => 101
[PinCode] => 454075
[Date] => 2012-03-03
[Balance] => 4533
[Active] => Yes
)
[1] => Array
(
[ID] => 103
[PinCode] => 786075
[Date] => 2012-09-05
[Balance] => 0
[Active] => No
)
)
I will explain how I get the output.
In all the arrays data1 , data2 and data3 the first row is the name of the columns and the remaining rows contain the data for those columns.
Firstly I take the array whose length is maximum in the $data array. Hence from $data I first select data1 array as its length is 3.
Using the following code:
$data1=$data['data1'];
$columns_data1 = $data1[0];
for($i=1;$i<=(count($data1)-1);$i++)
{
$output_data1[] = array_combine($columns_data1,$data1[$i]);
}
echo print_r($output_data1);
I get the output $output_data1 like this :
Array
(
[0] => Array
(
[ID] => 101
[PinCode] => 454075
[Date] => 2012-03-03
)
[1] => Array
(
[ID] => 103
[PinCode] => 786075
[Date] => 2012-09-05
)
)
After this I perform the following operation on data2 and data3 arrays to make their length equal to that of data1 length - 1 i.e excluding the first row which contains column names.
$data2=$data['data2'];
$columns_data2 = $data2[0];
for($i=1;$i<=(count($data1)-1);$i++)
{
if($i<count($data2))
$output_data2[] = array_combine($columns_data2,$data2[$i]);
else
$output_data2[]=array_combine($columns_data2,array('0'=>'0','1'=>'0')); //add the value as 0 to the rows and make the length equal to that of data1. Here I've take index 0 and 1 because first row in data contains two index i.e Balance and ID.
}
echo print_r($output_data2);
This code gives the following output for data2 :
Array
(
[0] => Array
(
[Balance] => 4533
[ID] => 101
)
[1] => Array
(
[Balance] => 0
[ID] => 0
)
)
Similary for data3 :
$data3=$data['data3'];
$columns_data3 = $data3[0];
for($i=1;$i<=(count($data1)-1);$i++)
{
if($i<count($data3))
$output_data3[] = array_combine($columns_data3,$data3[$i]);
else
$output_data3[]=array_combine($columns_data3,array('0'=>'0','1'=>'0'));
}
echo "<pre>"; print_r($output_data3);
gives the output :
Array
(
[0] => Array
(
[Active] => Yes
[ID] => 101
)
[1] => Array
(
[Active] => No
[ID] => 103
)
)
Next I'll combine $output_data1 and $output_data2 :
$left_join_on = array_column($output_data2, "ID");
$first_leftjoin_output = array();
foreach($output_data1 as $values){
$key = array_search($values['ID'], $left_join_on);
if($key ===false){
$key = array_search(0, $left_join_on);
}
unset($output_data2[$key]['ID']);
$first_leftjoin_output[] = array_merge($values,$output_data2[$key]);
}
echo "<pre>"; print_r($first_leftjoin_output);
gives the output :
Array
(
[0] => Array
(
[ID] => 101
[PinCode] => 454075
[Date] => 2012-03-03
[Balance] => 4533
)
[1] => Array
(
[ID] => 103
[PinCode] => 786075
[Date] => 2012-09-05
[Balance] => 0 //added 0 to balance as ID 103 didn't existed in output_data2. just like a left join of sql
)
)
Next I'll do a join on $first_leftjoin_output and $output_data3
$left_join_on_next = array_column($output_data3, "ID");
$second_leftjoin_output = array();
foreach($first_leftjoin_output as $values){
$key = array_search($values['ID'], $left_join_on_next);
if($key ===false){
$key = array_search(0, $left_join_on_next);
}
unset($output_data3[$key]['ID']);
$second_leftjoin_output[] = array_merge($values,$output_data3[$key]);
}
echo "<pre>"; print_r($second_leftjoin_output);
This step gives the final output $second_leftjoin_output which I already mentioned at the beginning of the question.
Array
(
[0] => Array
(
[ID] => 101
[PinCode] => 454075
[Date] => 2012-03-03
[Balance] => 4533
[Active] => Yes
)
[1] => Array
(
[ID] => 103
[PinCode] => 786075
[Date] => 2012-09-05
[Balance] => 0
[Active] => No
)
)
Hence this way I left joined all the three arrays based on matching ID field.
As you can see I'm doing all this in a hard coded way.
If my $data array contains data4 then again I'll have add the code to get $third_leftjoin_output.
I want this process to be dynamic.
I want a dynamic solution such that I just have to pass the $data array and the name of the feild the left join is going to be based on.
Something like this :
$output = left_join_function($data,"ID");
and this function should return the mentioned output. Or any other better solution would be also very helpful.
I'm not able to figure out how this can be done in a neat way.
As you mentioned, you can use a function (https://www.php.net/manual/de/functions.user-defined.php)
function foo($arg_1, $arg_2, /* ..., */ $arg_n)
{
echo "Example function.\n";
return $retval;
}
For you:
function dataJoinStep1($array)
{
// Do something with your array, left join etc.
return $array;
}
You can call the function in a loop (while/for/etc) dataJoinStep1($data)
My recommendation is robust enough to work on a dynamic number of data subsets, but it relies on the fact that all subsets are related by their ID value.
Iterate each group, separate the header data and combine it with subsequent rows within the group.
In the same loop, use first level keys to associate data from previously encountered groups (with the same first level key) and merge related data before pushing it into the result array. When finished iterating, call array_values() to re-index the result array.
Code: (Demo)
var_export(
array_values(
array_reduce(
$data,
function ($result, $group) {
$header = array_shift($group);
$idIndex = array_search('ID', $header);
foreach ($group as $row) {
$result[$row[$idIndex]] = array_merge(
$result[$row[$idIndex]] ?? [],
array_combine($header, $row)
);
}
return $result;
},
[]
)
)
);

Only 1 array (irregular array)

http://prntscr.com/fl69px
Hi, how can I get the part shown in the picture? There are many arrays.
Array
(
[max] => 46.784
[total] => 74.562
)
Array
(
[0] => 6
)
Array
(
[0] => 3
)
Array
(
[0] => 18 Oct 2017 14:12
)
Array
(
[0] => 2017-06-18T14:12:33+03:00
)
Array
(
[0] => New Cup
)
In short, I am not asking: New Cup
There are 16 in total.
Assuming that you want this array:
Array(
[bet] => 3
[featureEvent] => 0
[bank] => 0
[column] => 2
[requiredBet] => 2
)
You can create a basic php foreach-loop, which loops through your array and returns each key and value.
Currently we do not know how you created your array nor how the array name is, but lets assume that the array's name above is $bets
The foreach loop would look like this then:
foreach($bets as $betKey => $betValue)
{
// do something ...
// echo the values
echo $betValues;
}

How to remove duplicate values compare two array

I have two array.
one
Array
(
[0] => Array
(
[driverId] => 3
[latitude] => 23.752182
[longitude] => 90.377730
[distance] => 0
[EstTime] => 0
)
[1] => Array
(
[driverId] => 6
[latitude] => 23.752782
[longitude] => 90.375730
[distance] => 0.2341134331552646
[EstTime] => 133
)
)
two
Array
(
[0] => Array
(
[driverId] => 3
)
[1] => Array
(
[driverId] => 61
)
)
first array store in $info and second array store in $infor
here first array item driverId is 3 and second array item driverId is 3.
so in my output i want to skip first array first item.
When looping through each array store the driverId in another array and also check that the current driverId is not in this array, if it is then we can skip it. For example:
$ids = array();
foreach($infor AS $arr2){
$ids[] = $arr2['driverId'];
}
foreach($info AS $i){
if(!in_array($i['driverId'],$ids)){
print_r($i);
}
}

Merge a 3D Array with 2D Array Based on Shared Values

I need to merge a three-dimensional array with a two-dimensional array based on a shared value for 'id.'
In the example below, "George Washington" has an 'id' of 1. I need to append his 'value', which is found in the second array where 'id' is also 1.
The 'id' of "John Adams" is 2, which is not found in the second array. As a result, his 'value' needs to be set to 0 (or NULL).
The final result is a three-dimension array where 'value' has been added to each item in the original array.
Array #1
Array
(
[0] => Array
(
[0] => Array
(
[id] => 1
[name] => "George Washington"
)
[total] => 8
[average] => 2.5
)
[1] => Array
(
[0] => Array
(
[id] => 2
[name] => "John Adams"
)
[total] => 6
[average] => 3.0
)
[2] => Array
(
[0] => Array
(
[id] => 5
[name] => "James Monroe"
)
[total] => 9
[average] => 2.0
)
)
Array #2
Array
(
[0] => Array
(
[id] => 1
[value] => 12
)
[1] => Array
(
[id] => 5
[value] => 18
)
)
Desired Result:
Array
(
[0] => Array
(
[0] => Array
(
[id] => 1
[name] => "George Washington"
)
[total] => 8
[average] => 2.5
[value] => 12
)
[1] => Array
(
[0] => Array
(
[id] => 2
[name] => "John Adams"
)
[total] => 6
[average] => 3.0
[value] => 0
)
[2] => Array
(
[0] => Array
(
[id] => 5
[name] => "James Monroe"
)
[total] => 9
[average] => 2.0
[value] => 18
)
)
What I've tried so far:
I separated all of the 'id' values from the first array into a new array named $ids. Then while looping through the items in the second array, I check to see whether the 'id' from the second array is found in the $ids array.
But then I'm stuck because I don't know how to specify which item in the first array needs to receive the new 'value'. Also, this seems like a messy solution because of the overhead involved in creating the $ids array.
Didn't even realize you had tried to list the IDs and such before I was done writing this, but here you go anyway. Good luck!
$array1_size = count($array1);
$array2_size = count($array2);
// Creates a list containing all available IDs of Array1
for ($i = 0; $i < $array1_size; $i ++) {
$id_list[] = $array1[$i][0]['id'];
$array1[$i]['value'] = NULL; // Adds a NULL to all value fields
}
// Loops through Array2
for ($i = 0; $i < $array2_size; $i++) {
if (in_array($array2[$i]['id'], $id_list)) { // Checks if each ID exists in the ID list
$key = array_search($array2[$i]['id'], $id_list); // Gets the key of the matching ID
$array1[$key]['value'] = $array2[$i]['value']; // Adds the value
}
}
Edit: Values are now set to NULL by default and thus only gets changed later on if needed.

Categories