Array difference associative array - php

$invitedfrnds0 = Array
( [0] => Array
(
[fb_user_id] => 100000058716604
[accept_status] => 0
)
[1] => Array
(
[fb_user_id] => 100000063917115
[accept_status] => 0
)
[2] => Array
(
[fb_user_id] => 100000261361844
[accept_status] => 0
)
[3] => Array
(
[fb_user_id] => 100005502043347
[accept_status] => 0
)
)
$invitedfrnds2 = Array
(
[0] => Array
(
[fb_user_id] => 100005502043347
[accept_status] => 2
)
)
here i have two array $invitedfrnds0 and $invitedfrnds2, there is some matching fb_user_id in these two arrays, if any matching is found i need to delete the matching record form first array. After that i need to merge these two arrays
result array will look like this way.
$resultarray = Array
( [0] => Array
(
[fb_user_id] => 100000058716604
[accept_status] => 0
)
[1] => Array
(
[fb_user_id] => 100000063917115
[accept_status] => 0
)
[2] => Array
(
[fb_user_id] => 100000261361844
[accept_status] => 0
)
[3] => Array
(
[fb_user_id] => 100005502043347
[accept_status] => 2
)
)
I have searched a lot for this, tried some
$resultarray = array_diff($invitedfrnds0,$invitedfrnds2);
$resultarray = array_map('array_diff_assoc', $invitedfrnds0, $invitedfrnds2);
But not getting it right, please help me to solve this issue, thanks

You have to use array_merge_recursive(), not array_diff():
array_merge_recursive($invitedfrnds2, $invitedfrnds0);

Using array_udiff you could achieve this.
$invitedfrnds0 = array(
array('fb_user_id' => 100000058716604, 'accept_status' => 0),
array('fb_user_id' => 100000063917115, 'accept_status' => 0),
array('fb_user_id' => 100000261361844, 'accept_status' => 0),
array('fb_user_id' => 100005502043347, 'accept_status' => 0),
);
$invitedfrnds2 = array(
array('fb_user_id' => 100005502043347, 'accept_status' => 2),
);
// remove all matched values.
$result = array_udiff($invitedfrnds0, $invitedfrnds2, function($a, $b)
{
return $a['fb_user_id'] - $b['fb_user_id'];
});
// add values from the second array.
$result = array_merge($result, $invitedfrnds2);
var_dump($result);

Related

Can I Join Two Array - PHP

I want to combine the two arrays, which have the same structure.
This my first array :
[rows] => Array (
[0] => Array (
[kdregister] => 10865701
[pagu] => 339.4157454
[real] => 328.633577646
[real2] => 328.633577646
)
)
This is the second array :
[row] => Array (
[0] => Array (
[kdregister] => 10865701
[kegiatan] => name the game
[pagu] => 0
[real] => 0
[real2] => 0
)
)
and i have same value
[kdregister] => 10865701
and I want to have results like this
[row] => Array (
[0] => Array (
[kdregister] => 10865701
[kegiatan] => name the game
[pagu] => 0
[real] => 0
[real2] => 0
[pagu] => 339.4157454
[real] => 328.633577646
[real2] => 328.633577646
)
Can you help me guys? Thanks!!
Try this man, this work for me !
$array_merge = array_merge($array1,$array2);
$array = array();
foreach ($array_merge['row'] as $key1 => $value1) {
foreach ($array_merge['rows'] as $key2 => $value2) {
if ($value1['kdregister'] == $value2['kdregister']) {
$array[$key2] = array_merge($value1,$value2);
}
}
}
The simplest approach would be array_merge_recursive() but this will not create the array you wanted as that would be impossible. It would create a usable array containing 2 occurances of the data in the rows array
// setup your example arrays
$a1['rows'][] = ['kdregister' => 10865701, 'pagu' => 339.4157454, 'real' => 328.633577646, 'real2' => 328.633577646];
$a2['rows'][] = ['kdregister' => 10865701, 'kegiatan'=> 'Name of game', 'pagu' => 0, 'real' => 0, 'real2' => 0];
$a3 = array_merge_recursive($a1,$a2);
print_r($a3);
RESULT
Array
(
[rows] => Array
(
[0] => Array
(
[kdregister] => 10865701
[pagu] => 339.4157454
[real] => 328.633577646
[real2] => 328.633577646
)
[1] => Array
(
[kdregister] => 10865701
[kegiatan] => Name of game
[pagu] => 0
[real] => 0
[real2] => 0
)
)
)
In order to merge two arrays, you need to use array_merge method and to filter out unique characters you need to do something like this:
$ab = array_unique(array_merge($a, $b));

How to group posted array names by index in PHP

I am currently working on a project where the fields scale when clicking on the "Add" button.
I am grouping each field like this: name="packaging[]", name="packaging[1]", name="packaging[2]" and so on. When I submit the form, this is how the data looks like when posted:
Array
(
[packaging] => Array
(
[0] => 1
[1] => 2
)
[quantity] => Array
(
[0] => 1
[1] => 2
)
[total-weight] => Array
(
[0] => 1
[1] => 2
)
[length] => Array
(
[0] => 1
[1] => 2
)
)
Using PHP I would like to convert the above code to look like this:
Array
(
[0] => Array
(
[packaging] => 1,
[quantity] => 1,
[total-weight] => 1,
[length] => 1,
)
[1] => Array
(
[packaging] => 2,
[quantity] => 2,
[total-weight] => 2,
[length] => 2,
)
)
Any help would be greatly appreciated.
Try this....
$array=array();
foreach($data as $key=>$value){
foreach($value as $k=>$val){
$array[$k][$key]=$val;
}
}
DEMO
Try this code:
$rows = array ('packaging' => array ('0'=> 1,'1' => 2),'quantity' => array('0'=> 1,'1' => 2),'total-weight' => array ('0'=> 1,'1' => 2),
'length' =>array ('0'=> 1,'1' => 2)
);
$res_array = array();
$total_records = count($rows['packaging']);
for($i=0;$i<$total_records;$i++)
{
$res_array[] = array('packaging'=>$rows['packaging'] [$i],'quantity'=>$rows['quantity'][$i],
'total-weight'=>$rows['total-weight'][$i],'length'=>$rows['length'] [$i]);
}
print_r($res_array);

PHP: Merge 2 arrays

I've been trying the following for a day now and can't get it to work.
I'm getting information from a paginated source (say 3 pages in this example. So I've got 3 arrays to merge:
Array
(
[status] => Active
[nrEntries] => 6
[entries] => Array
(
[0] => Array
(
[sgname] => Merc
[timeentered] => 2016-02-08 04:30:00
)
[1] => Array
(
[sgname] => bystander
[timeentered] => 2016-03-17 20:55:00
)
)
)
Array
(
[status] => Active
[nrEntries] => 6
[entries] => Array
(
[0] => Array
(
[sgname] => Elvis
[timeentered] => 2016-03-08 04:30:00
)
[1] => Array
(
[sgname] => marcAR
[timeentered] => 2016-03-07 20:55:00
)
)
)
Array
(
[status] => Active
[nrEntries] => 6
[entries] => Array
(
[0] => Array
(
[sgname] => Killer
[timeentered] => 2016-03-09 05:30:00
)
[1] => Array
(
[sgname] => MyName
[timeentered] => 2016-03-05 21:45:00
)
)
)
The result I am looking for is to merge them into 1 array that I can return as a result.
Array
(
[status] => Active
[nrEntries] => 6
[entries] => Array
(
[0] => Array
(
[sgname] => Merc
[timeentered] => 2016-02-08 04:30:00
)
[1] => Array
(
[sgname] => bystander
[timeentered] => 2016-03-17 20:55:00
)
[2] => Array
(
[sgname] => Elvis
[timeentered] => 2016-03-08 04:30:00
)
[3] => Array
(
[sgname] => marcAR
[timeentered] => 2016-03-07 20:55:00
)
[4] => Array
(
[sgname] => Killer
[timeentered] => 2016-03-09 05:30:00
)
[5] => Array
(
[sgname] => MyName
[timeentered] => 2016-03-05 21:45:00
)
)
)
The problem that I'm running into is that with array_merge it won't work because of the identical index numbers of the records.
I tried the following, but that doesn't work either.
<?PHP
// add child array to the end of $result
for ($i=0 ; $i<2; $i++) {
$result['entries'][($page*2)+$i][] = $resultChild['entries'][$i];
}
?>
$a1['entries'] = array_merge_recursive($a1['entries'], $a2['entries'], $a3['entries']);
Try this:
$array1 = array(
'status' => 'Active',
'nrEntries' => 6,
'entries' => array(
array(
'sgname' => 'Merc',
'timeentered' => '2016-02-08 04:30:00'
),
array(
'sgname' => 'bystander',
'timeentered' => '2016-03-17 20:55:00'
)
)
);
$array2 = array(
'status' => 'Active',
'nrEntries' => 6,
'entries' => array(
array(
'sgname' => 'Elvis',
'timeentered' => '2016-03-08 04:30:00'
),
array(
'sgname' => 'marcAR',
'timeentered' => '2016-03-07 20:55:00'
)
)
);
$result = array_merge_recursive($array1, $array2);
$result['status'] = array_unique($result['status'])[0];
$result['nrEntries'] = array_unique($result['nrEntries'])[0];
echo "<pre>";
print_r($result);
echo "</pre>";
This gives the following:
Array
(
[status] => Active
[nrEntries] => 6
[entries] => Array
(
[0] => Array
(
[sgname] => Merc
[timeentered] => 2016-02-08 04:30:00
)
[1] => Array
(
[sgname] => bystander
[timeentered] => 2016-03-17 20:55:00
)
[2] => Array
(
[sgname] => Elvis
[timeentered] => 2016-03-08 04:30:00
)
[3] => Array
(
[sgname] => marcAR
[timeentered] => 2016-03-07 20:55:00
)
)
)
Hope this helps.
array_merge() is not recursive so it will replace your entries array as a whole. There is a function called array_merge_recursive(), but that won't work in your case either, since it will create arrays under status and nrEntries containing the values from all arrays.
What you need to do is to merge the 'big' page arrays, and then merge the entries separately. This could look like this:
// Keep all pages in an array
$pages = [$pageOne, $pageTwo, $pageThree];
// Merge the page arrays
$result = array_merge(...$pages);
// Clear entries as they only contain the data from the last page
$result['entries'] = [];
// Merge entries with the entries of each page separately
foreach ($pages as $page) {
$result['entries'] = array_merge($result['entries'], $page['entries']);
}
This is the simplest example I could think of. I hope it helps you in understanding what is going on, so you can refactor it to suit your needs.
As long as your entries arrays have numeric keys starting from 0, array_merge will append the values instead of replacing them.

PHP - sorting multidimensional array using array_multisort

I have this multidimensional array
Array
(
[0] => Array
(
[at] => 0
[bt] => 9
)
[1] => Array
(
[at] => 3
[bt] => 5
)
[2] => Array
(
[at] => 0
[bt] => 3
)
)
I want to sort it by key "at" so I tried this code
//$process = array() given above
$p = array();
foreach ($process as $key => $row) {
$p[$key] = $row['at'];
}
array_multisort($p, SORT_NUMERIC, SORT_ASC, $process);
and I get this result
Array
(
[0] => Array
(
[at] => 0
[bt] => 3
)
[1] => Array
(
[at] => 0
[bt] => 9
)
[2] => Array
(
[at] => 3
[bt] => 5
)
)
It sorts the "at" BUT it also sorts "bt".
How can I sort this array on key "at" ONLY?
Like this:
Array
(
[0] => Array
(
[at] => 0
[bt] => 9
)
[1] => Array
(
[at] => 0
[bt] => 3
)
[2] => Array
(
[at] => 3
[bt] => 5
)
)
Thank you.
EDIT:
As for the answer below by callmemath
When I only have this on my array,
Array
(
[0] => Array
(
[at] => 0
[bt] => 9
)
[1] => Array
(
[at] => 0
[bt] => 3
)
)
How can I prevent it from sorting since I only want to sort it by key "at". And nothing to sort there since they are both 0.
Use usort :
$array = array(
array('at' => 0, 'bt' => 9),
array('at' => 3, 'bt' => 5),
array('at' => 0, 'bt' => 3)
);
usort($array, function($a, $b) {
return $a['at'] - $b['at'];
});
var_dump($array);
Try it on Php online

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);

Categories