I have an array like this,
Array
(
[0] => Array
(
[0] => Array
(
[productId] => 145
[variantId] => 179
)
[1] => Array
(
[productId] => 145
[variantId] => 180
)
[2] => Array
(
[productId] => 147
[variantId] => 181
)
[3] => Array
(
[productId] => 147
[variantId] => 182
)
)
[1] => Array
(
[0] => Array
(
[productId] => 147
[variantId] => 181
)
[1] => Array
(
[productId] => 147
[variantId] => 184
)
)
[2] => Array
(
[0] => Array
(
[productId] => 145
[variantId] => 178
)
[1] => Array
(
[productId] => 145
[variantId] => 180
)
[2] => Array
(
[productId] => 147
[variantId] => 181
)
[3] => Array
(
[productId] => 147
[variantId] => 185
)
[4] => Array
(
[productId] => 147
[variantId] => 186
)
)
)
For this input we can see that [productId] => 147,[variantId] => 181 is common an all index of array.
Im using foreach to check this. But my code is working fine for 2 index, but not for more than 2 index.
In foreach im taking 0th index and comparing with other index, but not working.
Here is the code i have used so far.
$first = $name1[0];
$a = array();
unset($name1[0]);
foreach ($name1 as $row) {// foreach main array
foreach ($first as $row1) {// foreach first index of array array
foreach ($row as $kkk => $r1) {//foreach index of main array
// if first array's index's product is is equal to main array index's product id, push values to one array
if ($row1['productId'] == $r1['productId']) {
if (isset($row1['variantId']) && isset($r1['variantId'])) {
if ($row1['variantId'] == $r1['variantId']) {
$a[] = $r1;
}
} else {
$a[] = $r1;
}
}
}
}
}
First method
Use this, PHP functions are faster than trying to build your own algorithm.
$result = array_intersect($array1, $array2);
Second Method
$array1 = array("5","26","38","42");
$array2 = array("15","36","38","42");
$final_array = array();
foreach($array1 as $key=>$val){
if(in_array($val,$array2)){
$final_array[] = $val;
}
}
print_r($final_array);
Result: Array ( [0] => 38 [1] => 42 )
for 2d array
function multi_intersect($arr) {
$return = array();
foreach ($arr as $a) {
foreach ($arr as $b) {
if ($a === $b) continue;
$return = array_merge($return, array_intersect($a, $b));
}
}
return array_unique($return);
}
you should get
Array
(
[0] => 58
)
so finally you use this to get result
$intersect = call_user_func_array('array_intersect', $arr);
You can use foreach inside a foreach loop
$my_array = array
(
'0' => array
(
'0' => array
(
'productId' => 145,
'variantId' => 179
),
'1' => array
(
'productId' => 145,
'variantId' => 180
),
'2' => array
(
'productId' => 147,
'variantId' => 181
),
'3' => array
(
'productId' => 147,
'variantId' => 182
)
),
'1' => array
(
'0' => array
(
'productId' => 147,
'variantId' => 181
),
'1' => array
(
'productId' => 147,
'variantId' => 184
)
),
'2' => array
(
'0' => array
(
'productId' => 145,
'variantId' => 178
),
'1' => array
(
'productId' => 145,
'variantId' => 180
),
'2' => array
(
'productId' => 147,
'variantId' => 181
),
'3' => array
(
'productId' => 147,
'variantId' => 185
),
'4' => array
(
'productId' => 147,
'variantId' => 186
)
)
);
$final_array = array();
foreach($my_array as $nested_array){
foreach($nested_array as $val ){
$final_array[] = $val;
}
}
echo "<pre>"; print_r($final_array);
Here is the answer which i wanted,
function serialize_array_values($arr){
foreach($arr as $key=>$val){
sort($val);
$arr[$key]=serialize($val);
}
return $arr;
}
$result = array_map("unserialize", array_intersect(
serialize_array_values($name1[0]),
serialize_array_values($name1[1]),
serialize_array_values($name1[2])
));
https://eval.in/738165, check the working link
Reference : array_intersect throws errors when arrays have sub-arrays
Finally here is my working code
foreach ($name1 as $kkk => $row) {
$list[] = $this->serializeArrayValues($row);
}
$intersect = call_user_func_array('array_intersect', $list);
$a = array_map("unserialize", $intersect);
function serializeArrayValues($arr) {
foreach ($arr as $key => $val) {
$arr[$key] = serialize($val);
}
return $arr;
}
Related
I have two arrays like this:
Array1
$array1 = Array
(
0 => Array
(
'ID' => 101,
'Code' => 1075,
'Date' => '2012-03-03 17:13:12.433'
),
1 => Array
(
'ID' => 103,
'Code' => 175,
'Date' => '2012-09-05 20:30:02.217'
),
2 => Array
(
'ID' => 109,
'Code' => 178,
'Date' => '2012-07-05 20:30:02.217'
)
);
Array2
$array2 = Array
(
0 => Array
(
'Amount' => 1234,
'ID' => 101
),
1 => Array
(
'Amount' => 5656,
'ID' => 101
),
2 => Array
(
'Amount' => 1342,
'ID' => 103
),
3 => Array
(
'Amount' => 0,
'ID' => 0
)
);
I'm using the code below to perform a join on the two arrays :
$arr2 = array_column($array2, "ID");
$finalArray = array();
foreach($array1 as $arr){
$key = array_search($arr['ID'], $arr2);
if($key ===false){
$key = array_search(0, $arr2);
}
unset($array2[$key]['ID']);
$finalArray[] = array_merge($arr,$array2[$key]);
}
print_r($finalArray);
The current output using the code above is :
finalArray
Array
(
[0] => Array
(
[ID] => 101
[Code] => 1075
[Date] => 2012-03-03 17:13:12.433
[Amount] => 1234 //considers only the first entry of ID 101
)
[1] => Array
(
[ID] => 103
[Code] => 175
[Date] => 2012-09-05 20:30:02.217
[Amount] => 1342
)
[2] => Array
(
[ID] => 109
[Code] => 178
[Date] => 2012-07-05 20:30:02.217
[Amount] => 0
)
)
But since in array2 there are two entries for ID 101 but the code above only takes the first match for a matching ID.
The expected output is :
Desired Output
Array
(
[0] => Array
(
[ID] => 101
[Code] => 1075
[Date] => 2012-03-03 17:13:12.433
[Amount] => 1234 //amount for first entry of ID 101
)
[1] => Array
(
[ID] => 101
[Code] => 1075
[Date] => 2012-03-03 17:13:12.433
[Amount] => 5656//amount for second entry of ID 101
)
[2] => Array
(
[ID] => 103
[Code] => 175
[Date] => 2012-09-05 20:30:02.217
[Amount] => 1342
)
[3] => Array
(
[ID] => 109
[Code] => 178
[Date] => 2012-07-05 20:30:02.217
[Amount] => 0
)
)
I'm not able to figure out what loop to use.
The code should add rows for each matching ID of array2.
How do I modify my current code such that it gives me the desired output?
$arr2 = array_column($array2, "ID");
$finalArray = array();
foreach($array1 as $arr){
//Get the index of ID which exist in array2. And store in search variable.
$search = array_keys($arr2, $arr['ID']);
if(!$search){
//If ID not exit in array-2, get the index of that ID and store in search variable.
$key = array_search(0, $arr2);
$search[0] = $key;
unset($array2[$key]['ID']);
}
//Fetch search item and merge.
foreach($search as $value){
$finalArray[] = array_merge($arr,$array2[$value]);
}
}
echo "<pre>";
print_r($finalArray);
Make the result array an associative array keyed by ID. You can copy the element of the first array to the result, then add the amounts from the second array.
$finalResult = [];
foreach ($array1 as $arr) {
$finalResult[$arr['ID']] = $arr;
}
foreach ($array2 as $row) {
$id = $row['ID'];
unset($row['ID']);
foreach ($row as $col => $value) {
if (empty($finalResult[$id][$col])) {
$finalResult[$id][$col] = $value;
} else {
$finalResult[$id][$col] += $value;
}
}
}
I am trying to compare the keys of an array with values of another array. In case of greater values of the current key, I would like to push the value into a new array. Then I want to insert all the collected values for that specific key into a DB table.
These are my input arrays:
$productlist = [result] => Array
(
[0] => Array
(
[configoption1] => 2M
[id] => 96
)
[1] => Array
(
[configoption1] => 5M
[id] => 97
)
[2] => Array
(
[configoption1] => 15M
[id] => 98
)
[3] => Array
(
[configoption1] => 30M
[id] => 99
)
)
$myplans = Array
(
[2M] => Array
(
[price] => 10
)
[5M] => Array
(
[price] => 10
)
[15M] => Array
(
[price] => 10
)
[30M] => Array
(
[price] => 10
)
)
The following is my sample code:
$upgradelist = array()
foreach ($myplans as $plan => $data) {
foreach($productlist['result'] as $key=>$value){
if($plan == 'ENTERPRISE'){
//no higher plans than Enterprise
}else{
$plan1 = (int)substr_replace($plan, "", -1);
$value['configoption1'] = (int)substr_replace($value['configoption1'], "", -1);
#echo " configconfig=> ".$value['configoption1'];
if($plan > $value['configoption1']){
$upgrade_product_ids[$plan][] = $value['id'];
}
}
}
//insert upgrade products list
if(!empty($upgradelist)){
foreach($upgradelist as $key => $upgrade_product_id){
#$insert_stmt_upgradeproduct = <insert statement> for each $plan
}
}
}
expected output each time I come out from the foreach loop: > foreach($productlist['result']
$upgradelist = Array
(
[2] => Array
(
[0] => 97 //5M
[1] => 98 //15M
[2] => 99 //30M
)
)
$upgradelist = Array
(
[5] => Array
(
[0] => 98
[1] => 99
)
)
$upgradelist = Array
(
[15] => Array
(
[0] => 99
)
)
$upgradelist = Array
(
[30] => Array
(
)
)
You can solve like below:
foreach($myplans as $key => $plan) {
$data_to_be_inserted = [];
foreach($productlist['result'] as $product) {
if ($key > $product['configoption1']) {
$data_to_be_inserted [] = $product['id'];
}
}
if (count($data_to_be_inserted)) {
//insert into database(expected value will be available here)
}
}
YOu can do like this
<?php
$myArray = [];
$myArray['result'] = array(
array("configoption1" => "2m", "id" => 96),
array("configoption1" => "5m", "id" => 97),
array("configoption1" => "15m", "id" => 98),
array("configoption1" => "30m", "id" => 99)
);
//print_r($myArray);
$myArray2 =[
"2m" => ["price" => 10],
"5m" => ["price" => 10],
"15m" => ["price" => 10],
"30m" => ["price" => 10]
];
$resultArray = [];
foreach($myArray2 as $key => $values) {
$keyNum = (int)$key;
$newTempArray[$keyNum] = [];
foreach($myArray['result'] as $newresult) {
$configoption1Num = (int)$newresult['configoption1'];
if($configoption1Num > $keyNum) {
array_push($newTempArray[$keyNum], $newresult['id']);
}
}
print_r($newTempArray[$keyNum]);
echo "<br>";
}
http://sandbox.onlinephpfunctions.com/code/008c839fb4fad4ed2aceae7c4ed1220579e8e318
How to remove multi dimensional array if duplicate.
In this example Barcode is duplicate value of 111 i want to remove this if found duplicate. PLease help Im new to php. Thanks
Output:
Array
(
[0] => Array
(
[Barcode] => 111
[Transaction_No] => 256
)
[1] => Array
(
[Barcode] => 111
[Transaction_No] => 0
)
[2] => Array
(
[Barcode] => 222
[Transaction_No] => 0
)
)
Expected Output:
Array
(
[0] => Array
(
[Barcode] => 222
[Transaction_No] => 0
)
)
This keeps track of the keys of each barcode array item to find duplicates, then uses array_values at the end to fix the array indexing.
<?php
$myArray = array(
array
(
"Barcode" => 111,
"Transaction_No" => 256
),
array
(
"Barcode" => 111,
"Transaction_No" => 0
),
array
(
"Barcode" => 222,
"Transaction_No" => 0
)
);
$barcodeKeys = array();
foreach ($myArray as $key => $arr) {
$code = $arr["Barcode"];
if (!isset($barcodeKeys[$code])) {
$barcodeKeys[$code] = array();
}
$barcodeKeys[$code][] = $key;
if (count($barcodeKeys[$code]) > 1) {
foreach ($barcodeKeys[$code] as $dupKey) {
if (isset($myArray[$dupKey])) {
unset($myArray[$dupKey]);
}
}
}
}
$myArray = array_values($myArray);
print_r($myArray);
Output
Array
(
[0] => Array
(
[Barcode] => 222
[Transaction_No] => 0
)
)
Fast approach to your question:
<?php
$barcodes = array(array( 'Barcode' => 111,'Transaction_No' => 256),array('Barcode' => 111,'Transaction_No' => 0),array('Barcode' => 222,'Transaction_No' => 0),array('Barcode' => 333,'Transaction_No' => 0));
$result = array();
$exist = array();
foreach($barcodes as $key => $item){
if( in_array( $item['Barcode'], array_values( $exixt ) ){
unset( $result[ array_search ( $item['Barcode'], $exist ) ] );
} else {
$result[ $key ] = array('Barcode' => $item['Barcode'],'Transaction_No' => $item['Transaction_No'] );
$exist[ $item['Barcode'] ] = $key;
}
}
var_dump($result);
I have one multidimensional array and from that array I want to create a new array from same id1 from first array and want to get all matching sub arrays value into its inner array with its element
Below is the array I have:
Array
(
[0] => Array
(
[id1] => 109891
[id2] => 67
)
[1] => Array
(
[id1] => 828393
[id2] => 67
)
[2] => Array
(
[id1] => 828393
[id2] => 68
)
[3] => Array
(
[id1] => 816714
[id2] => 70
)
[4] => Array
(
[id1] => 816714
[id2] => 67
)
[5] => Array
(
[id1] => 816714
[id2] => 68
)
)
And I want output like in this way:
Array
(
[109891] => Array
(
[0] => Array
(
[id2] => 67
)
)
[828393] => Array
(
[0] => Array
(
[id2] => 67
)
[1] => Array
(
[id2] => 68
)
)
[816714] => Array
(
[0] => Array
(
[id2] => 70
)
[1] => Array
(
[id2] => 67
)
[2] => Array
(
[id2] => 68
)
)
)
Any Help Would be Appreciated
This should do it:
$output = array();
foreach($input as $r) {
$id1 = $r['id1'];
if(!isset($output[$id1])) {
$output[$id1] = array();
}
$output[$id1][] = array('id2' => $r['id2']);
}
print_r($output);
origin array:
$array = array(
array('id1' => '109891', 'id2' => '67'),
array('id1' => '828393', 'id2' => '67'),
array('id1' => '828393', 'id2' => '68'),
array('id1' => '816714', 'id2' => '70'),
);
use this foreach:
$new = array();
foreach($array as $element) {
$new[$element['id1']][] = array('id2' => $element['id2']);
}
or this in a function:
function editMyArray($array) {
$newArray = array();
foreach($array as $element) {
$newArray[$element['id1']][] = array('id2' => $element['id2']);
}
return $newArray;
}
Output:
echo "<pre>";
print_r($new);
echo "</pre>";
Array
(
[109891] => Array
(
[0] => Array
(
[id2] => 67
)
)
[828393] => Array
(
[0] => Array
(
[id2] => 67
)
[1] => Array
(
[id2] => 68
)
)
[816714] => Array
(
[0] => Array
(
[id2] => 70
)
)
)
Use below tested code:
$i = 0;
$sepr = 0;
$arr = array();
foreach($your_array as $your_reslut_array) {
$k = $i; $k = ($k > 0 ? $k-1 : 0);
foreach($your_reslut_array as $key => $value) {
if($key != "ID" && $value != $your_reslut_array[$k][$key]) {
$sepr++;
}
}
$arr[$sepr][] = $car;
$i++;
}
print_r($arr);
This is the array
Array
(
[0] => Array
(
[artist] => John Keys
[postID] => 254
)
[1] => Array
(
[artist] => Jay Bloom
[postID] => 249
)
[2] => Array
(
[artist] => John Keys
[postID] => 216
)
[3] => Array
(
[artist] => Angie Belle
[postID] => 225
)
)
and I want to remove all duplicates from the artist elements only. Notice that 0 and 2 have the same artist but a different postID. I just want to keep the first occurence of the artist and remove all others. So the result I want to have is
Array
(
[0] => Array
(
[artist] => John Keys
[postID] => 254
)
[1] => Array
(
[artist] => Jay Bloom
[postID] => 249
)
[2] => Array
(
[artist] => Angie Belle
[postID] => 225
)
)
I've tried array_unique and also serializing and doing an array_map, nothing seems to work right.
<?php
$array = Array
(
'0' => Array
(
'artist' => 'John Keys',
'postID' => 254
),
'1' => Array
(
'artist' => 'Jay Bloom',
'postID' => 249
),
'2' => Array
(
'artist' => 'John Keys',
'postID' => 216
),
'3' => Array
(
'artist' => 'Angie Belle',
'postID' => 225
)
);
$newarray = array();
foreach($array as $key => $val){
if(!array_key_exists('artist', $newarray)){
$newarray[$val['artist']] = $val;
}
}
echo '<pre>';
print_r($newarray);
Edit: using numeric keys:
$newarray = array();
$temp = array();
foreach($array as $key => $val){
if(!in_array($val['artist'], $temp)){
$temp[] = $val['artist'];
}
if( !array_key_exists(array_search($val['artist'], $temp), $newarray) ){
$newarray[array_search($val['artist'], $temp)] = $val;
}
}
Simple approach:
$result = array();
foreach ($input as $item) {
if (!array_key_exists($item['artist'], $result)) {
$result[$item['artist']] = $item;
}
}
<?php
$a=array(
"0" => array
(
"artist" => "John Keys",
"postID" => "254"
),
"1" => array
(
"artist" => "Jay Bloom",
"postID" => "249"
),
"2" => array
(
"artist" => "John Keys",
"postID" => "216"
),
"3" => array
(
"artist" => "Angie Belle",
"postID" => "225"
)
);
var_dump($a);
for($i=0;$i<count($a);$i++)
{
for($j=$i+1;$j<count($a); $j++)
{
$tmp1=$a[$i]["artist"];
$tmp2=$a[$j]["artist"];
if($tmp1==$tmp2)
{
unset($a[$j]);
}
$tmp3=array_values($a);
$a=$tmp3;
}
}
var_dump($a);
?>