php two Multidimensional Array difference - php

I am working with two multidimensional array difference bellow are my array:
Array1:
Array
(
[0] => Array
(
[F_CONTACT_ID] => 2
[F_CONTACT_FNAME] => name2
[F_CONTACT_NAME] => name22
)
[1] => Array
(
[F_CONTACT_ID] => 3
[F_CONTACT_FNAME] => name3
[F_CONTACT_NAME] => name33
)
)
Array2:
Array
(
[0] => Array
(
[F_CONTACT_ID] => 2
[F_CONTACT_FNAME] => name2
[F_CONTACT_NAME] => name22
)
[1] => Array
(
[F_CONTACT_ID] => 3
[F_CONTACT_FNAME] => name3
[F_CONTACT_NAME] => name33
)
[2] => Array
(
[F_CONTACT_ID] => 5
[F_CONTACT_FNAME] => name5
[F_CONTACT_NAME] => name55
)
)
I just want o compare the difference with 'F_CONTACT_ID' in the array.
My Resulting Array Should be:
Result:
Array
(
[2] => Array
(
[F_CONTACT_ID] => 5
[F_CONTACT_FNAME] => name5
[F_CONTACT_NAME] => name55
)
)
Also If one array is empty: suppose Array2 is empty. My result Array should be:
Array
(
[0] => Array
(
[F_CONTACT_ID] => 2
[F_CONTACT_FNAME] => name2
[F_CONTACT_NAME] => name22
)
[1] => Array
(
[F_CONTACT_ID] => 3
[F_CONTACT_FNAME] => name3
[F_CONTACT_NAME] => name33
)
)
I tried with different solutions but nothing worked for me. I tried to retrieve the F_CONTACT_ID and stored in single-dimensional array and compare but It took lot of time.
Kindly help me in better and fast solution.

Have you tried this?
for($i=0;$i<count($array1);$i++) {
$temp[$array1[$i]['F_CONTACT_ID']] = $array1[$i];
};
for($i=0;$i<count($array2);$i++) {
if($temp[$array2[$i]['F_CONTACT_ID']]) {
unset($temp[$array2[$i]['F_CONTACT_ID']]);
} else {
$temp[$array2[$i]['F_CONTACT_ID']] = $array2[$i];
}
}
echo "<pre>";
print_r($temp);
echo "</pre>";
The result will be some thing like this:
Array
(
[5] => Array
(
[F_CONTACT_ID] => 5
[F_CONTACT_FNAME] => name5
[F_CONTACT_NAME] => name55
)
)

You could try the function array-diff-key() function, which helps you to compare two multidimensional arrays using keys.
You could visit this page for more information: http://php.net/manual/en/function.array-diff-key.php

what about something like this?
$array1;
$array2;
$array3;
foreach ($array1 as $ar1) {
foreach ($array2 as $ar2) {
if ($ar1['F_CONTACT_ID']==$ar2['F_CONTACT_ID']) {
array_push($array3, $ar1);
}
}
}
it's not very optimize, to maximize performance change the second foreach checking if array_push is already append (using a bool condition)

Related

Converting a single array into multi array

I have an array which looks like this below:
Array ( [:status0] => 1 [:status1] => 2 )
I would want to convert it into something like this:
Array ( [:status0] => Array ( [0] => 1 [1] => 1 ) [:status1] => Array ( [0] => 2 [1] => 1 ) )
i want to do this with flexibility because the number of the array and names are random. I was thinking of using a for loop something like this:
foreach ($newParam as $row){
$newArray[$row['Continent']][$row['Country']][] = $row['City'];
}
But i cant use this in my case, please help
This code should do what you want:
$newParam = Array ( ':status0' => 1, ':status1' => 2 ) ;
foreach ($newParam as $key => $value) {
$newArray[$key] = array($value, 1);
}
print_r($newArray);
Output:
Array (
[:status0] => Array (
[0] => 1
[1] => 1
)
[:status1] => Array (
[0] => 2
[1] => 1
)
)
Demo on 3v4l.org

how to count the array values with key

I have array like this i need to count by array values
Array ( [Cop] => Array ( [0] => Dozen [1] => Dozen [2] => Akls [3] => Akls ) [MSN] => Array ( [0] => Dozen ) [NeK] => Array ( [0] => Suhan [1] => Ebao ) [NetSE] => Array ( [0] => SuZhan [1] => Guhang ) )
For example
Array ( [Cop] => Array ( [0] => Dozen [1] => Dozen [2] => Akls [3] => Akls ))
In the Cop key i have two different values for cop so i need cop should be 2
Cop - 2
MSn - 1
NeK - 2
NetSE - 2
I need the count like above how can i do this ?
Try simply using array_map,count,& array_unique like as
array_map(function($v) {
return count(array_unique($v));
}, $arr);
Use array_unique() and then count.
count(array_unique($array['Cop']));// output 2
If you want to print for every key do following:
$array = array('Cop'=>array('Dozen','Dozen','Akls','Akls'), 'MSN'=> array('Dozen'), 'NeK'=> array('Suhan','Ebao'));
foreach($array as $key => &$value) {
$value = count(array_unique($array[$key]));
}
print_r($array);
Output:
Cop = 2
MSN = 1
NeK = 2
You should use array_count_values() for that, here's an example:
$data = array('cop' => array(0 => 'test', 1 => 'test', 2 => 'test2'));
foreach($data as $item){
$result = array_count_values($item);
print_r($result);
}
Outputs:
Array
(
[test] => 2
[test2] => 1
)

How to sum same field values in multidimentional array

I have to admit that understanding the tables are big challenge for me so please dont judge me so hard...
This is my array:
Array
(
[names] => Array
(
[0] => Name1
[1] => Name2
[2] => Name1
)
[ids] => Array
(
[0] => 1
[1] => 2
[2] => 1
)
[quantities] => Array
(
[0] => 255
[1] => 2
[2] => 467
)
)
And i wish to sum "quantities" where names or ids are the same.
Example output should be:
Array
(
[names] => Array
(
[0] => Name1
[1] => Name2
)
[ids] => Array
(
[0] => 1
[1] => 2
)
[quantities] => Array
(
[0] => 722
[1] => 2
)
)
I know there is a function like "array_reduce" but don't know how to use it.
Thanks for help!
try this
$result = [];
foreach($array['ids'] as $key=>$val ){
if(array_key_exists($val, $result)){
$result[$val]['sum_quantity'] += $array['quantities'][$key];
}
else{
$result[$val]['sum_quantity'] = $array['quantities'][$key];
$result[$val]['name'] = $array['names'][$key];
$result[$val]['id'] = $array['ids'][$key];
}
}
and output will be like this
Array
(
[1] => Array //array key = id
(
['name'] => Name1,
['sum_quantity'] => 722,
['id'] => 1
)
[2] => Array
(
['name'] => Name2,
['sum_quantity'] => 2,
['id'] => 2
)
)
You can do this way :
$testArray['names'][0]='name1';
$testArray['names'][1]='name2';
$testArray['names'][2]='name1';
$testArray['ids'][0]=1;
$testArray['ids'][1]=2;
$testArray['ids'][2]=1;
$testArray['quantities'][0]=255;
$testArray['quantities'][1]=2;
$testArray['quantities'][2]=467;
echo "<pre>";
print_r($testArray);
$unqArray['names']=array();
$unqArray['ids']=array();
$unqArray['quantities']=array();
foreach($testArray['ids'] as $key=>$value)
{
if(!in_array($value,$unqArray['ids']))
{
$unqArray['names'][]=$testArray['names'][$key];
$unqArray['ids'][]=$testArray['ids'][$key];
$quantity=0;
foreach($testArray['ids'] as $keyId=>$valueId)
{
if($valueId==$value)
{
$quantity+=$testArray['quantities'][$keyId];
}
}
$unqArray['quantities'][]=$quantity;
}
}
print_r($unqArray);
echo "</pre>";

Repopulate a newer array with values from an older array- php

Hello Stack Overflow community,
I am trying to update values from array1 with a newly ordered array2 and output the final array as array2. I have been tying to figure this out for days now but cannot seem to get it. I am new to php as you can most likely tell. Can anyone help me?
Here are the two arrays- 'array1' is first and 'array2' follows:
Array (
[0] => Array
(
[id] => bbb
[hammer] => $1,000
)
[1] => Array
(
[id] => ccc
[hammer] => $678
)
[2] => Array
(
[id] => aaa
[hammer] => $222
)
) Array (
[0] => Array
(
[id] => aaa
[hammer] => GBP 135
)
[1] => Array
(
[id] => bbb
[hammer] => GBP 610
)
[2] => Array
(
[id] => ccc
[hammer] => GBP 413
)
)
Now my code tries to update the newly reordered 'id' values in the second array with the 'hammer' values from the first array. Here is my code:
foreach($array2 as $key => $val) {
$a = $array2[$key]['id'];
$hammer_a = $array2[$key]['hammer'];
foreach($array1 as $key => $val) {
$b = $array1[$key]['id'];
if($a===$b){
$hammer_b = $array1[$key]['hammer'];
$array2[$key]['hammer'] = $hammer_b;
}
}
}
However as this code stands I am left with an undesired result below. I would like 'id' 'aaa' to have its original 'hammer' value of $222 and so on for the other 'id's:
Array
(
[0] => Array
(
[id] => aaa
[hammer] => $1,000
)
[1] => Array
(
[id] => bbb
[hammer] => $678
)
[2] => Array
(
[id] => ccc
[hammer] => $222
)
)
Can anyone please tell me what I am doing wrong?
Change the name of $key and $val in the second loop. They are overriding each other.
Don't use $key in your inner foreach loop. You want the key for array2.
There is also this option, with a lot less code:
$items[0] = array('id'=>'bbb','hammer'=>'$1,000');
$items[1] = array('id'=>'ccc','hammer'=>'$678');
$items[2] = array('id'=>'aaa','hammer'=>'$222');
function cmp($a, $b) {
return strcmp($a['id'], $b['id']);
}
echo '<pre>';
var_dump($items);
usort($items, "cmp");
var_dump($items);
echo '</pre>';

Comparing&editing two tabdelimeted files with PHP?

I want to compare two tabdelimeted files. I take the files and converts them into two arrays with the following structure:
Array 1
Array
(
[0] => Array
(
[name] => name1
[qty] => 200
)
[1] => Array
(
[name] => name2
[qty] => 9
)
[2] => Array
(
[name] => name3
[qty] => 3
)
[3] => Array
(
[name] => name4
[qty] => 1
)
)
Array 2
Array
(
[0] => Array
(
[name] => name1
[qty] => 180
)
[1] => Array
(
[name] => name2
[qty] => 9
)
)
How can I compare these two arrays and where the value is different to replace the value in the array 2 with array of value 1.
The easiest way to do this would be to create an associative array for the second set of data, instead of the array format you has used above. Since you only seem to have two "columns", and these are effectively a key/value relationship this should be nice and easy.
This example takes the two input arrays you have generated to do it, but you can probably adjust this so that you create the associative array directly as you read the second:
// first create an associative array from the second indexed array
$secondAssoc = array();
foreach ($secondArray as $row) {
$secondAssoc[$row['name']] = $row['qty'];
}
/*
$secondAssoc now looks like:
Array
(
[name1] => 180
[name2] => 9
)
*/
// Now loop the first array and update it
foreach ($firstArray as $rowId => $row) {
if (isset($secondAssoc[$row['name']]) && $secondAssoc[$row['name']] != $row['qty']) {
$firstArray[$rowId]['qty'] = $secondAssoc[$row['name']];
}
}
/*
$firstArray now looks like this:
Array
(
[0] => Array
(
[name] => name1
[qty] => 180
)
[1] => Array
(
[name] => name2
[qty] => 9
)
[2] => Array
(
[name] => name3
[qty] => 3
)
[3] => Array
(
[name] => name4
[qty] => 1
)
)
*/
See it working.
EDIT Here is a version that also creates an array, $modifiedItems, that holds only the items that have changed:
// first create an associative array from the second indexed array
$secondAssoc = array();
foreach ($secondArray as $row) {
$secondAssoc[$row['name']] = $row['qty'];
}
// Now loop the first array and update it
$modifiedItems = array();
foreach ($firstArray as $rowId => $row) {
if (isset($secondAssoc[$row['name']]) && $secondAssoc[$row['name']] != $row['qty']) {
$firstArray[$rowId]['qty'] = $secondAssoc[$row['name']];
$modifiedItems[] = array('name'=>$row['name'],'qty'=>$secondAssoc[$row['name']]);
}
}
See it working.

Categories