Array Comparison in Php, and find the diff in values - php

I want to compare two arrays in php. My arrays looks like this
Array (
[0] => Array ( [Id] => 1 [row1] => 1458)
[1] => Array ( [Id] => 2 [row1] => 16)
[2] => Array ( [Id] => 3 [row1] => 115)
[3] => Array ( [Id] => 4 [row1] => 18)
[4] => Array ( [Id] => 5 [row1] => 13)
[5] => Array ( [Id] => 6 [row1] => 13)
[6] => Array ( [Id] => 7 [row1] => 131)
)
Array (
[0] => Array ( [Id] => 1 [row1] => 158)
[1] => Array ( [Id] => 2 [row1] => 165)
[2] => Array ( [Id] => 3 [row1] => 111)
[3] => Array ( [Id] => 4 [row1] => 186)
[4] => Array ( [Id] => 5 [row1] => 3)
)
Firstly, array1 size and array2 sizes were not equal always. Id value in array1 may or may not present in array2, If the value is not present, function have to print the total index in array3, like
[someindex] => Array ( [Id] => 6 [row1] => 13 )
if it is present, function should subtract the row1 of array1 to row1 of array2 and print in array3, like this
[someindex] => Array ( [Id] => 1 [row1] => 1300)
and my final output should be,
Array (
[0] => Array ( [Id] => 1 [row1] => 1300)
[1] => Array ( [Id] => 2 [row1] => -149)
[2] => Array ( [Id] => 3 [row1] => 4)
[3] => Array ( [Id] => 4 [row1] => -168)
[4] => Array ( [Id] => 5 [row1] => 10)
[5] => Array ( [Id] => 6 [row1] => 13)
[6] => Array ( [Id] => 7 [row1] => 131)
)
Can any one help me in solving this problem.

$arr1 = Array (
0 => Array ('Id' => 1, 'row1' => 1458)
,1 => Array ('Id' => 2, 'row1' => 16)
,2 => Array ('Id' => 3, 'row1' => 115)
,3 => Array ('Id' => 4, 'row1' => 18)
,4 => Array ('Id' => 5, 'row1' => 13)
,5 => Array ('Id' => 6, 'row1' => 13)
,6 => Array ('Id' => 7, 'row1' => 131)
);
$arr2 = Array (
0 => Array('Id' => 1, 'row1' => 158)
,1 => Array('Id' => 2, 'row1' => 165)
,2 => Array('Id' => 3, 'row1'=> 111)
,3 => Array('Id' => 4, 'row1' => 186)
,4 => Array('Id' => 5, 'row1' => 3)
);
$final = array();
foreach($arr1 as $k => $sec)
{
$sub = 0;
foreach($arr2 as $sec2)
{
if($sec2['Id']==$sec['Id'])
{
$sub = $sec2['row1'];
break;
}
}
$sec['row1'] -= $sub;
//Or to save to another element:
//$sec['row2'] = $sec['row1']-$sub;
$final[] = $sec;
}
echo '<pre>'.print_r($final,true).'</pre>';
Output:
Array (
[0] => Array ( [Id] => 1 [row1] => 1300)
[1] => Array ( [Id] => 2 [row1] => -149)
[2] => Array ( [Id] => 3 [row1] => 4)
[3] => Array ( [Id] => 4 [row1] => -168)
[4] => Array ( [Id] => 5 [row1] => 10)
[5] => Array ( [Id] => 6 [row1] => 13)
[6] => Array ( [Id] => 7 [row1] => 131)
)

First, you either have to make the second array searchable by using the Id value as the keys or write a search function. I'm choosing the former:
$searchable = array_reduce($array2, function(&$result, $item) {
return $result + array($item['Id'] => $item['row1']);
}, array());
The array_reduce() function starts with an empty array and builds it using the array addition operator; this creates an array that can be dereferenced using the id.
Then you perform a map operation on the first array:
$array3 = array_map(function($item) use ($searchable) {
$value = isset($searchable[$item['Id']]) ? $searchable[$item['Id']] : 0;
$item['row1'] -= $value;
// $item['row2'] = $item['row1'] - $value;
return $item;
}, $array1);
Doing a map operation preserves the original array and creates a new one with the values you choose inside a callback function.

Don't know if this exactly what you want, but you can check if a key exists with array_key_exists:
$array3 = array();
foreach ($array1 as $k => $v) {
if (array_key_exists($k, $array2)) {
$array3[] = $v - $array2[$k];
} else {
$array3[] = $v;
}
}

This creates an indexing array ($new) in case the main keys don't match of in the original arrays.
$arr = array (
0 => array ( 'Id' => '1','row1' => 1458),
1 => array ( 'Id' => '2','row1' => 16),
2 => array ( 'Id' => '3','row1' => 115),
3 => array ( 'Id' => '4','row1' => 18),
4 => array ( 'Id' => '5','row1' => 13),
5 => array ( 'Id' => '6','row1' => 13),
6 => array ( 'Id' => '7','row1' => 131));
$arr2 = array (
0 => array ( 'Id' => '1','row1' => 158),
1 => array ( 'Id' => '2','row1' => 165),
2 => array ( 'Id' => '3','row1' => 111),
3 => array ( 'Id' => '4','row1' => 186),
4 => array ( 'Id' => '5','row1' => 3));
$new = array();
foreach ($arr2 as $key => $value){
$new[$value['Id']] = $value['row1'];
}
foreach ($arr as $key => $value){
if (isset($new[$value['Id']])){
$arr[$key]['row1'] -= $new[$value['Id']];
}
}
print_r($arr);

Related

Create array key from value. i have created array define to bellow.i have differentiate actual result and expected result

Actual Array
Array
(
[0] => Array
(
[sub_id] => 3
[sub_name] => tttt
[master_id] => 3
)
[1] => Array
(
[sub_id] => 4
[sub_name] => yyyy
[master_id] => 3
)
[2] => Array
(
[sub_id] => 5
[sub_name] => kkkk
[master_id] => 4
)
)
Expected Result
Array
(
[3] => Array(
[0] => Array
(
[sub_id] => 3
[sub_name] => tttt
[master_id] => 3
)
[1] => Array
(
[sub_id] => 4
[sub_name] => yyyy
[master_id] => 3
)
)
[4] => Array(
[0] => Array
(
[sub_id] => 5
[sub_name] => kkkk
[master_id] => 4
)
)
)
You can create a new array and set value of master id as the index and put the value in it.
$data = array();
foreach($array as $key=>$value){
$data[$value['master_id']][] = $value;
}
$actualArray = array(array('sub_id' => 3, 'sub_name' => 'tttt', 'master_id' => 3),array('sub_id' => 4, 'sub_name' => 'yyyy', 'master_id' => 3),array('sub_id' => 5, 'sub_name' => 'kkkk', 'master_id' => 4));
$tempArray = array_unique(array_column($actualArray, 'master_id'));
$uniqueArray = array_intersect_key($actualArray, $tempArray);
foreach ($uniqueArray as $key => $masters) {
$count = 0;
foreach ($actualArray as $key1 => $actuals) {
if($masters['master_id'] == $actuals['master_id']){
$expectedArray[$key][$count] = $actuals;
$count++;
}
}
}

compare values from multidimensional array and add key to array

Array
(
[681074CRPAK4] => Array
(
[0] => 681074
[1] => 681074CRPAK4
[2] => 5602385431605
)
[681520XXXP6L] => Array
(
[0] => 681520
[1] => 681520XXXP6L
[2] => 5602385667394
)
[681530XXXP6V] => Array
(
[0] => 681530
[1] => 681530XXXP6V
[2] => 5602385667417
)
[681530XXXP6W] => Array
(
[0] => 681530
[1] => 681530XXXP6W
[2] => 5602385667424
)
[681530XXXP6X] => Array
(
[0] => 681530
[1] => 681530XXXP6X
[2] => 5602385667400
)
)
I want to compare the value of key[0] of each array.
If they are the same then I would like to add a new key[3] to each array with an id.
This is an array of variable products if the product has the same key[0] then its the same product with different variations.
If the key[0] is different from the previous then add id+1, in this example, I would like to end up with:
Array
(
[681074CRPAK4] => Array
(
[0] => 681074
[1] => 681074CRPAK4
[2] => 5602385431605
[3] => 1
)
[681520XXXP6L] => Array
(
[0] => 681520
[1] => 681520XXXP6L
[2] => 5602385667394
[3] => 2
)
[681530XXXP6V] => Array
(
[0] => 681530
[1] => 681530XXXP6V
[2] => 5602385667417
[3] => 3
)
[681530XXXP6W] => Array
(
[0] => 681530
[1] => 681530XXXP6W
[2] => 5602385667424
[3] => 3
)
[681530XXXP6X] => Array
(
[0] => 681530
[1] => 681530XXXP6X
[2] => 5602385667400
[3] => 3
)
)
can you guys help me with this?
I tried this:
but does not work
foreach ($new as $current_key => $current_array) {
foreach ($new as $search_key => $search_array) {
$ref1 = $current_array[0];
$ref2 = $search_array[0];
if (($search_key != $current_key) and ($ref1 == $ref2)) {
$current_array[3] = $p_id_product;
}
else{
$current_array[3] = $p_id_product++;
}
}
}
Assuming you have already sorted the array by the initial index, so at least they are grouped:
<?php
$data =
[
[
'foo',
'spam',
'bar',
],
[
'foo',
'eggs',
],
[
'bar',
'ham'
],
];
$output = [];
$counter = 0;
$last = null;
foreach($data as $k => $v) {
if($last !== $v[0])
$counter++;
$v[3] = $counter;
$output[$k] = $v;
$last = $v[0];
}
var_export($output);
Output:
array (
0 =>
array (
0 => 'foo',
1 => 'spam',
2 => 'bar',
3 => 1,
),
1 =>
array (
0 => 'foo',
1 => 'eggs',
3 => 1,
),
2 =>
array (
0 => 'bar',
1 => 'ham',
3 => 2,
),
)

Creating a Multi-Dimentional from another Multi Dimensional Array

Wrecking my brains for a while now and need your help
I have an array as below:
$originalArray = array(
array('id' => 1, 'sub-id' => 0),
array('id' => 2, 'sub-id' => 0),
array('id' => 3, 'sub-id' => 1),
array('id' => 4, 'sub-id' => 3),
array('id' => 5, 'sub-id' => 4),
array('id' => 6, 'sub-id' => 0),
array('id' => 7, 'sub-id' => 0),
array('id' => 8, 'sub-id' => 6),
array('id' => 9, 'sub-id' => 8),
array('id' => 10, 'sub-id' => 8)
);
and the logic here is
if sub-id of any element is equal to the id of another element,
then the array goes into the sub key of the parent element. i.e. sub-id 1 should go in 'sub' element of id 1 and sub-id 3 should go in 'sub' element of id 3
The required output of the above array is:
$requiredArray = array(
array('id' => 1,'sub-id' => 0,
'sub' => array(
array('id' => 3,'sub-id' => 1,
'sub' => array(
array('id' => 4,'sub-id' => 3,
'sub' => array(
array('id' => 5,'sub-id' => 4)
)
)
)
)
)
),
array('id' => 2,'sub-id' => 0),
array('id' => 6,'sub-id' => 0,
'sub' => array(
array('id' => 8,'sub-id' => 6,
'sub' => array(
array('id' => 9,'sub-id' => 8),
array('id' => 10,'sub-id' => 8)
)
)
)
),
array('id' => 7,'sub-id' => 0)
);
What have I tried so far
// $original array is the array shown above
function compare_subid($a, $b)
{
if ($a['sub-id'] == $b['sub-id']) return 0;
return ($a['sub-id'] < $b['sub-id']) ? -1 : 1;
}
usort($originalArray, 'compare_subid');
$newArray = array();
$newArray = create_multidimensional($originalArray, $newArray);
function create_multidimensional($originalArray, $newArray = null)
{
if ($newArray == null) $newArray = array();
array_walk($originalArray, function ($value, $key) use (&$newArray) {
//e($value);
if ($value['sub-id'] == 0) {
$newArray[] = $value;
} else {
foreach ($newArray as &$v) {
if ($v['id'] == $value['sub-id']) {
$v['sub'] = $value;
} else {
// not sure what to put here
}
}
}
});
return $newArray;
}
With this i am able to achieve a part of the $requiredArray which is as follows:
Array
(
[0] => Array
(
[id] => 6
[sub-id] => 0
[sub] => Array
(
[id] => 8
[sub-id] => 6
)
)
[1] => Array
(
[id] => 7
[sub-id] => 0
)
[2] => Array
(
[id] => 2
[sub-id] => 0
)
[3] => Array
(
[id] => 1
[sub-id] => 0
[sub] => Array
(
[id] => 3
[sub-id] => 1
)
)
)
Not sure if this is the correct method to use or there is any better way to do so.
If what I am doing is correct, I am not able to figure out what to input in the else statement of create_multidimensional function that I have created.
There is an easy way using references and only remove the ones with a sub-id at the very end:
// We need keys to be able to quickly map our assignments
foreach ($originalArray as $val) {
$array[$val["id"]] = $val;
}
// we first assign the arrays in a non-destructive way, so that we can easily find the
// appropriate key in the array
foreach ($array as $key => $val) {
if ($val["sub-id"] !== 0) {
$array[$val["sub-id"]]["sub"][] = &$array[$key];
}
}
// remove the ones from the first dimension which are somewhere deeper
foreach ($array as $key => $val) {
if ($val["sub-id"] !== 0) {
unset($array[$key]);
}
}
This is why objects in PHP are cool.
// Format all data into objects keyed by id
$input = array();
foreach ($originalArray as $el) {
$input[ $el['id'] ] = (object)($el + array('sub' => array()));
}
$result = array();
foreach ($input as $el) {
$sid = $el->{'sub-id'};
// Parent object: into result root
if ( !$sid ) {
$result[] = $el;
}
// Child object: into other object
else {
$input[$sid]->sub[] = $el;
}
}
print_r($result);
The obvious downside is that objects use ->prop syntax, which doesn't work well with -, so you have to ugly it: $el->{'sub-id'}.
And of course the result is a bunch of objects. May not be what you want.
Result (http://3v4l.org/8VMJr):
Array
(
[0] => stdClass Object
(
[id] => 1
[sub-id] => 0
[sub] => Array
(
[0] => stdClass Object
(
[id] => 3
[sub-id] => 1
[sub] => Array
(
[0] => stdClass Object
(
[id] => 4
[sub-id] => 3
[sub] => Array
(
[0] => stdClass Object
(
[id] => 5
[sub-id] => 4
[sub] => Array
(
)
)
)
)
)
)
)
)
[1] => stdClass Object
(
[id] => 2
[sub-id] => 0
[sub] => Array
(
)
)
[2] => stdClass Object
(
[id] => 6
[sub-id] => 0
[sub] => Array
(
[0] => stdClass Object
(
[id] => 8
[sub-id] => 6
[sub] => Array
(
[0] => stdClass Object
(
[id] => 9
[sub-id] => 8
[sub] => Array
(
)
)
[1] => stdClass Object
(
[id] => 10
[sub-id] => 8
[sub] => Array
(
)
)
)
)
)
)
[3] => stdClass Object
(
[id] => 7
[sub-id] => 0
[sub] => Array
(
)
)
)

How to Add a new Key and Merge the array values in multidimemsional array PHP

here's the result of my first function:
Array
(
[0] => Array
(
[MaidID] => 13
[Stores] => Array
(
[0] => 14
[1] => 5
)
)
[1] => Array
(
[MaidID] => 3
[Stores] => Array
(
[0] => 4
)
)
[2] => Array
(
[MaidID] => 41
[Stores] => Array
(
[0] => 14
)
)
)
Then, here's the result of my second function:
Array
(
[1] => Array
(
[MaidID] => 14
[Cash] => 10000
[Debit] => 0
[Credit] => 0
)
)
and here's should be the result:
Array ([0] => Array (
[MaidID] => 14
[Cash] => 10000.00
[Debit] => 0
[Credit] => 0
[MaidsID] => Array(
[0] => 13
[1] => 41
)
)
)
Is it possible to make it?I need to a new key name MaidsID pointing to the list of MaidID owned by more than one Stores.Please help me, Please be patience in my question, im just a beginner.Thank you so much.
this code work ok. $a is your first array $b is the second, $c is result
$a = array (array('Maid' => 1, 'Stores' => array (1,5) ), array('Maid' => 3, 'Stores' => array (4) ), array('Maid' => 4, 'Stores' => array (1) ));
$b = array (array('Maid' => 1, 'Cash' => 10000, 'Debit' => 0, 'Credit' => 0));
$MaidsID=array();
foreach ($a as $aa ){
if (count($aa['Stores']>1)){
array_push($MaidsID, $aa['Maid']);
}
}
$MaidsID=array('MaidsID' => $MaidsID);
$c = array_merge($b, $MaidsID);`
I tested it here and it was ok. (Just replace $a with your first array and $b with seccond ).
Are you sure that the structure of your arrays is exactly as you have written above?? Maybe there is any problem with that.
You have puted array inside another array? (its not needed i think)
Howerver: For this code:
`$a = array (array('Maid' => 1, 'Stores' => array (1,5) ), array('Maid' => 3, 'Stores' => array (4) ), array('Maid' => 4, 'Stores' => array (1) ));
$b = array (array('Maid' => 1, 'Cash' => 10000, 'Debit' => 0, 'Credit' => 0));
print_r($a);
echo "<br><br>================================================<br><br>";
print_r($b);
echo "<br><br>================================================<br><br>";
$MaidsID=array();
foreach ($a as $aa ){
if (count($aa['Stores']>1)){
array_push($MaidsID, $aa['Maid']);
}
}
$MaidsID=array('MaidsID' => $MaidsID);
$c = array_merge($b, $MaidsID);
print_r($c);
echo "<br><br>================================================<br><br>";`
The output is:
Array ( [0] => Array ( [Maid] => 1 [Stores] => Array ( [0] => 1 [1] => 5 ) ) [1] => Array ( [Maid] => 3 [Stores] => Array ( [0] => 4 ) ) [2] => Array ( [Maid] => 4 [Stores] => Array ( [0] => 1 ) ) )
================================================
Array ( [0] => Array ( [Maid] => 1 [Cash] => 10000 [Debit] => 0 [Credit] => 0 ) )
================================================
Array ( [0] => Array ( [Maid] => 1 [Cash] => 10000 [Debit] => 0 [Credit] => 0 ) [MaidsID] => Array ( [0] => 1 [1] => 3 [2] => 4 ) )
================================================
Isn't this how you want the result?
Have a look at this. Maybe this can help you.
$result = array_merge($array1, $array2);

I've spent hours trying to get these array keys into variables but everything I try is not working

I have an array of all possible combinations of values, a bit like working out what monetary values I could make with only certain coins. Now I have an array built but much of the useful data are keys and not values.
A small snippet is below:
Each root key is an array with keys total, denomination and quantity. Each of the quantities multiplied by the denominations total the total. While I've been able to access the total easily enough I just can't get a handle on the denominations and the quantities.
It's my plan to output to separate radio buttons like so:
foreach($array as $arr)
{
echo '<input type="radio" name="name" value="'.$arr[$total].'">';
foreach($arr[denom] as $index => $d)
{
echo $d[qty][$index].' x '.$d[denom][$index].' = '.($qty[$index]*$denom[$index]).'<br>';
}
}
Here's the array I have, any help would be much appreciate, I'm usually great at this bot it's driving me crazy
Array
(
[2] => Array
(
[total] => 105
[denom] => Array
(
[0] => 105
)
[qty] => Array
(
[0] => 1
)
)
[3] => Array
(
[total] => 210
[denom] => Array
(
[0] => 105
)
[qty] => Array
(
[0] => 2
)
)
[4] => Array
(
[total] => 300
[denom] => Array
(
[0] => 300
)
[qty] => Array
(
[0] => 1
)
)
[5] => Array
(
[total] => 405
[denom] => Array
(
[0] => 300
[1] => 105
)
[qty] => Array
(
[0] => 1
[1] => 1
)
)
[6] => Array
(
[total] => 500
[denom] => Array
(
[0] => 500
)
[qty] => Array
(
[0] => 1
)
)
[7] => Array
(
[total] => 605
[denom] => Array
(
[0] => 500
[1] => 105
)
[qty] => Array
(
[0] => 1
[1] => 1
)
)
Constant non-numeric array indices should be written like any other string, i.e. encapsulated in quotes.
foreach($array as $arr) {
echo '<input type="radio" name="name" value="'.$arr['total'].'">';
foreach($arr['denom'] as $index => $d){
for ($j = 0;$j < count($denom);$j++) {
echo $d['qty'][$j].' x '.$d['denom'][$j].' = ';
echo ($d['qty'][$j]*$d['denom'][$j]) . '<br>';
}
}
}
First format your array like this in my code, add 6, 7 array elements. I modify for loop too.
<?php
$array = array(2 => array('total' => 105, 'denom' => array(0 => 105), 'qty' => array(0 => 1)),
3 => array('total' => 210, 'denom' => array(0 => 105), 'qty' => array(0 => 2)),
4 => array('total' => 300, 'denom' => array(0 => 300), 'qty' => array(0 => 1)),
5 => array('total' => 405, 'denom' => array(0 => 300, 1 => 105), 'qty' => array(0 => 1, 1 => 1)),
);
foreach($array as $arr)
{
//var_dump($arr);
echo '<input type="radio" name="name" value="'.$arr['total'].'">';
foreach($arr['denom'] as $index => $d)
{
echo $arr['qty'][$index].' x '.$d.' = '.($arr['qty'][$index]*$d).'<br>';
}
}
?>

Categories