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.
Related
I try to compare arrays and divide it for three different arrays based on subarray with key "ProductID"
Input data look like:
Cat Array:
Array
(
[0] => Array
(
[catID] => Array
(
[0] => 65
[1] =>66
)
[discount] => 10
[productID] => Array
(
[0] => 10887
[1] => 8508
[2] => 8056
)
[startDate] => 05/12/2022 12:00 am
[endDate] => 10/12/2022 12:00 am
)
)
Tax Array:
Array
(
[0] => Array
(
[taxID] => 2436
[discount] => 5
[productID] => Array
(
[0] => 8018
[1] => 8009
)
)
[1] => Array
(
[taxID] => 2438
[discount] => 10
[productID] => Array
(
[0] => 13184
[1] => 8009
)
)
)
When in tax array does not exists any productID from Cat Array, output for this array should look like:
$taxNoDuplicates = [
'discount' => '',
'productIds' => []
];
When in Cat Array does not exists any productID from Tax Array, output for this array should look like:
$catNoDuplicates = [
'discount' => '',
'startDate' => '',
'endDate' => '',
'productIds' => []
];
And if duplicates exsits in Cat Array and Tax Array output for this array should look like: :
$taxAndCatCombine = [
'discountTax' => '',
'discountCat' => '',
'startDate' => '',
'endDate' => '',
'productIds' => []
];
Is it possible to create output arrays the way I provided?
I'm using print_r(array_unique($array, SORT_REGULAR)); on the array below but it does not work.
I'm trying to filter out the redundant data.
Notice that [Order] and its key value pairs are all the same. But [Transaction] and its key value pairs are unique.
I need to get the [Order] element data and combine it with the 3 different [Transaction] elements.
My array
Array
(
[0] => Array
(
[Order] => Array
(
[PO] => TR11214
[OrderID] => 242856952012
)
[Transaction] => Array
(
[TransPO] => TR11211
[TransactionPrice] => 91.17
)
)
[1] => Array
(
[Order] => Array
(
[PO] => TR11214
[OrderID] => 242856952012
)
[Transaction] => Array
(
[TransPO] => TR11212
[TransactionPrice] => 180.41
)
)
[2] => Array
(
[Order] => Array
(
[PO] => TR11214
[OrderID] => 242856952012
)
[Transaction] => Array
(
[TransPO] => TR11213
[TransactionPrice] => 209.99
)
)
)
The final array I need will look something like this.
Array
(
[Order] => Array
(
[PO] => TR11214
[OrderID] => 242856952012
)
[Transaction] => Array
(
[0] => Array
(
[TransPO] => TR11211
[TransactionPrice] => 91.17
)
[1] => Array
(
[TransPO] => TR11212
[TransactionPrice] => 180.41
)
[2] => Array
(
[TransPO] => TR11213
[TransactionPrice] => 209.99
)
)
)
I can flatten the original array and then use array_unique, but wanted to see if there is a better way to accomplish what I need.
my code:
$myarray = array(
0 => array(
"Order" => array("PO" => "TR11214", "OrderID" => 242856952012),
"Transaction" => array("TransPO" => "TR11211", "TransactionPrice" => 91.17)
),
1 => array(
"Order" => array("PO" => "TR11214", "OrderID" => 242856952012),
"Transaction" => array("TransPO" => "TR11212", "TransactionPrice" => 180.41)
),
2 => array(
"Order" => array("PO" => "TR11214", "OrderID" => 242856952012),
"Transaction" => array("TransPO" => "TR11213", "TransactionPrice" => 209.99)
)
);
print_r(array_unique($myarray, SORT_REGULAR));
If you want to determine how many unique values of the Order element there are in your array, you need to apply array_unique only to the Order elements, which you can do using array_column:
$unique_orders = count(array_unique(array_column($myarray, 'Order'), SORT_REGULAR));
You can process your array using a list of keys which have non-unique values to generate an array, while other keys will have just a single value:
$non_unique_keys = ['Transaction'];
$output = array();
foreach (array_keys($myarray[0]) as $key) {
if (in_array($key, $non_unique_keys)) {
$output[$key] = array_column($myarray, $key);
}
else {
$output[$key] = $myarray[0][$key];
}
}
print_r($output);
Example Output:
Array (
[Order] => Array (
[PO] => TR11214
[OrderID] => 242856952012
)
[Sales Tax] => Array (
[PO] => TR11214
[SalesTaxAmount] => 0
)
[Transaction] => Array (
[0] => Array (
[TransPO] => TR11211
[TransactionPrice] => 91.17
)
[1] => Array (
[TransPO] => TR11212
[TransactionPrice] => 180.41
)
[2] => Array (
[TransPO] => TR11213
[TransactionPrice] => 209.99
)
)
)
Demo on 3v4l.org
array_unique() is intended for single dimensional arrays. If you want to use it on a multi-dimentional array, you should consider using usort() instead. Then you'll need to iterate through the array in reverse manually, searching for duplicates and removing them.
I have this array $allYearData
Now i´d like to split it into 12 parts, seperated by month.
So i use PHP preg_grep() with a foreach loop to search for data.
foreach($allYearData as $key){
$janData[] = preg_grep("/^2015-01-.*$/", $key);
}
How can i fetch all data in each key?
As you can see in $janData i get empty keys where there is no hits. And then only the date if there is a hit.
I´d like $janData to be a new array with key 0 to contain the first hit and all data/values in that key. Like $allYearData but only with the hits from preg_grep.
$allYEarData:
Array (
[0] => Array (
[id] => 7811
[objekt_element] => 23050-121-1_3105
[objekt_nr] => 23050-121-1
[element_nr] => 3105
[vart] => B.Avf
[vem] => Blå
[anteckn] =>
[datum] => 2015-09-29 18:00:19
)
[1] => Array (
[id] => 7812
[objekt_element] => 23050-121-1_3107
[objekt_nr] => 23050-121-1
[element_nr] => 3107
[vart] => B.Avf
[vem] => Blå
[anteckn] =>
[datum] => 2015-09-29 18:00:22
)
[2]...
$janData
Array (
[0] => Array ( )
[1] => Array ( )
[2] => Array ( )
[3] => Array ( )
[4] => Array ( )
[5] => Array (
[datum] => 2015-01-16 04:17:01
)
[6] => Array (
[datum] => 2015-01-16 04:16:57
)
[7]
I´d like $janData to be filled
Array (
[0] => Array (
[id] => 7811
[objekt_element] => 23050-121-1_3105
[objekt_nr] => 23050-121-1
[element_nr] => 3105
[vart] => B.Avf
[vem] => Blå
[anteckn] =>
[datum] => 2015-01-16 18:00:19
)
$key in you foreach is array, so select where is a date you want to hit and use $key to add to result array.
foreach($allYearData as $key){
if (preg_grep("/^2015-01-.*$/", $key['datum']){
$janData[] = $key;
}
}
I have the following two arrays...
1) how could i get only the different key->value one?
2) how can i insert to mysql the second array?
// first array
$aa = Array
(
[t_a] => Array
(
[0] => Array
(
[f_c] => LAL
[p_r] => RN
[id] =>
[gender] => m
)
)
[t_b] => Array
(
)
[t_l] => Array
(
[0] => Array
(
[p_lev] => 2
[p_date] =>
[p_r] =>
)
)
[t_r] => Array
(
[0] => Array
(
[I_r] => 19
)
)
// second array
$bb = Array
(
[t_a] => Array
(
[0] => Array
(
[f_c] => NAN
[p_r] => RN
[id] => 1214125
[gender] => m
)
)
[t_b] => Array
(
)
[t_l] => Array
(
[0] => Array
(
[p_lev] => 2
[p_date] => 21
[p_r] => 25
)
)
[t_r] => Array
(
[0] => Array
(
[I_r] => 19
)
)
I have used the array_diff function but i get NULL.
please some one help?
$aa=(array)$aa;
$bb=(array)$bb;
$result=array_diff($aa,$bb);
It's unclear what you want. Please give an example or your desired output. Here's one possibility:
$ser_aa = array_map(function($e){return serialize($e);}, $aa);
$ser_bb = array_map(function($e){return serialize($e);}, $bb);
$diff = array_diff($ser_aa, $ser_bb);
$out = array_map(function($e){return unserialize($e);}, $diff);
print_r($out);
Output:
Array
(
[t_a] => Array
(
[0] => Array
(
[f_c] => LAL
[p_r] => RN
[id] =>
[gender] => m
)
)
[t_l] => Array
(
[0] => Array
(
[p_lev] => 2
[p_date] =>
[p_r] =>
)
)
)
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);