Sort merge recursive array - php

I have merge two array with php function "merge_recursive" (both came from sql query) all of them have a common field [date] is it possible to sort array[new] by [date] field ?
[new] => Array
(
[0] => Array
(
[user] => 8888
[user_image] => 0j1kjjzdv3ez07a0ee4lnmjb7_163.jpeg
[favorite_id] => 2
[date] => 1328579091
[images_id] => 4
[image] => 0j1kjjzdv3ez07a0ee4lnmjb7_163.jpeg
[text] =>
[favorite_user] => 8888
[favorite_user_image] => 0j1kjjzdv3ez07a0ee4lnmjb7_163.jpeg
)
[1] => Array
(
[user] => 8888
[user_image] => 0j1kjjzdv3ez07a0ee4lnmjb7_163.jpeg
[favorite_id] => 5
[date] => 1328578954
[images_id] => 2
[image] => flw3utn9igiqh7dtt2o61ydf8_174.jpeg
[text] => 3
[favorite_user] => 6666
[favorite_user_image] => flw3utn9igiqh7dtt2o61ydf8_174.jpeg
)
[2] => Array
(
[user] => 8888
[user_image] => 0j1kjjzdv3ez07a0ee4lnmjb7_163.jpeg
[image] => 0j1kjjzdv3ez07a0ee4lnmjb7_163.jpeg
[image_id] => 4
[date] => 1328579081
[text] =>
[image_date] => 1328577934
[comments] => 0
)
)
I want that aray will be sort like (example below)
[1] = Array
(
)
[0] = Array
(
)
[2] = Array
(
)
is it possible?

You can use array_multisort, which does exactly what you want.
$dates = array();
foreach ($new as $key => $value) { // $new is your array
$dates[$key] = $value['date'];
}
array_multisort($dates, SORT_ASC, $new);
This is easily extendible with multiple sort criteria.

You could try something like this:
http://us3.php.net/manual/en/function.uasort.php
function compareDate($a, $b) {
if ($a["date"] == $b["date"]) {
return 0;
}
return ($a["date"] < $b["date"]) ? -1 : 1;
}
uasort($input, compareDate);

Related

Merge subarrays of multidimensional array based on sub value

I have a multidimensional array like this, I need to merge subarrays with the same messageID value.
$myarray = Array (
[0] => Array
(
[messageId] => 5ACE9D8841
[sender] => john#doe.com
)
[1] => Array
(
[messageId] => 7EE67D8170
[sender] => dan#doe.com
)
[2] => Array
(
[messageId] => 8095FD8836
[sender] => cat#doe.com
)
[3] => Array
(
[messageId] => 7EE67D8170
[dest] => mitch#doe.com
[status] => sent
[date] => 2021-02-01 11:06:55
)
);
Expected result , [1] and [3] are merged into [1] because they share the same [messageId] :
Array
(
[0] => Array
(
[messageId] => 5ACE9D8841
[sender] => john#doe.com
)
[1] => Array
(
[messageId] => 7EE67D8170
[dest] => mitch#doe.com
[status] => sent
[date] => 2021-02-01 11:06:55
[sender] => dan#doe.com
)
[2] => Array
(
[messageId] => 8095FD8836
[sender] => cat#doe.com
)
)
I don't mind about the key index or the order.
EDIT : I've tried array_merge, array_merge_recursive and many others. Best result was obtained with
foreach ($myarray as $sub_arr) {
$result_arr = array_merge($result_arr, $sub_arr);
$result_arr = array_unique($result_arr);
}
It works but returns only the last iteration :
Array
(
[messageId] => 7EE67D8170
[dest] => mitch#doe.com
[status] => sent
[date] => 2021-02-01 11:06:55
[sender] => dan#doe.com
)
Regards
Try using array_reduce function with callback function:
$result = array_values(array_reduce($myarray, function($rows, $item){
if (array_key_exists('messageId', $item) && is_scalar($item['messageId'])) {
$rows = array_replace_recursive($rows ?? [], [$item['messageId'] => $item]);
}
return $rows;
}));
print_r($result);
fiddle

How To Merge/push array into one in php

code
$result = [];
foreach ($getAllAgent as $rkey => $rvalue) {
$found = -1;
for ($i = 0; $i < count($result); $i++) {
if ($result[$i]["brokerid"] == $rvalue["brokerid"]) {
$found = $i;
$result[$i]['get_agent_details'][] = $rvalue['get_agent_details']; //here to combine
break;
}
}
// if not found, create new
if ($found == -1) {
$result[] = $rvalue;
}
results
Array
(
[0] => Array
(
[id] => 2
[brokerid] => 2
[agentid] => 3
[addedby] => 1
[get_agent_details] => Array
(
[id] => 3
[name] => kenang
[ic] => 932132923
[phone] => 2313123
[0] => Array
(
[id] => 4
[name] => ivan
[ic] => 32992131
[phone] => 31231
)
)
)
)
I have one set of an array, and I loop it and restructure match the data based on ID. After that I will try to merge the same data into one array, I able add into one array. But it will not combine as one. The correct result should be as below.
[get_agent_details] => Array
(
[0] => Array(
[id] => 3
[name] => kenang
[ic] => 932132923
[phone] => 2313123
),
[1] => Array
(
[id] => 4
[name] => ivan
[ic] => 32992131
[phone] => 31231
)
)
Your problem is in this line:
$result[] = $rvalue;
Consider the case where you only have one item; this will result in:
Array
(
[0] => Array
(
[id] => 2
[brokerid] => 2
[agentid] => 3
[addedby] => 1
[get_agent_details] => Array
(
[id] => 1
[name] => Chesney Hawkes
[ic] => 932132923
[phone] => 2313123
)
)
)
But to be consistent, you need get_agent_details to be a list of items, that happens to have one entry:
Array
(
[0] => Array
(
[id] => 2
[brokerid] => 2
[agentid] => 3
[addedby] => 1
[get_agent_details] => Array
(
[0] => Array
(
[id] => 1
[name] => Chesney Hawkes
[ic] => 932132923
[phone] => 2313123
)
)
)
)
So you need to re-arrange your data, for instance by writing:
$rvalue['get_agent_details'] = [0 => $rvalue['get_agent_details']];
$result[] = $rvalue;
Then, since get_agent_details will already be a list when you encounter a second matching item, your existing code in the inner loop will do the right thing:
// Add an item to the list
$result[$i]['get_agent_details'][] = $rvalue['get_agent_details'];

PHP Compare Arrays and clear all key if one in a array is empty

I'm looking for a solution to compare multiplay arrays with each of them i want to unset in all arrays if one key is empty. So as a example if [keywords] is empty i want to unset in all arrays [keywords]. Here are my arrays that i print_r.
Array
(
[0] => Array
(
[id] => 1
[pid] => 3
[sorting] => 128
[tstamp] => 1503039725
[title] => test
[alias] => test-3
[author] => 1
[inColumn] => main
[keywords] =>
[showTeaser] =>
[teaserCssID] =>
[teaser] =>
[printable] =>
[customTpl] =>
[protected] =>
[groups] =>
[guests] =>
[cssID] =>
[space] =>
[published] => 1
[start] =>
[stop] =>
)
[1] => Array
(
[id] => 2
[pid] => 3
[sorting] => 256
[tstamp] => 1503045056
[title] => test 2
[alias] => test-2
[author] => 1
[inColumn] => main
[keywords] =>
[showTeaser] =>
[teaserCssID] => a:2:{i:0;s:0:"";i:1;s:0:"";}
[teaser] =>
[printable] =>
[customTpl] =>
[protected] =>
[groups] =>
[guests] =>
[cssID] => a:2:{i:0;s:0:"";i:1;s:0:"";}
[space] => a:2:{i:0;s:0:"";i:1;s:0:"";}
[published] => 1
[start] =>
[stop] =>
)
)
What i have tried so far is
print_r($arrResult);
foreach($arrResult as $Result)
{
foreach ($Result as $arrKey => $arrField)
{
if(!empty($arrField))
{
$arrAllowedField[$arrKey] = $arrKey;
}
}
}
That build a array that contains the key which has values. But the problem is, that it also add empty fields of a other array.
Thanks
// remove empty entries in each array
$ar = array_map('array_filter', $ar);
// find keys having not empty value at least in one array
$temp = array_intersect_key(...$ar);
// save only keys from temp array
foreach($ar as &$item) {
$item = array_intersect_key($item, $temp);
}
demo
It removes the all the empty values from the array
$arrResult = array_map('array_filter', $arrResult);
$arrResult = array_filter( $arrResult );
echo "<pre>";
print_r($arrResult);
Output
Array
(
[0] => Array
(
[id] => 1
[pid] => 3
[sorting] => 128
[tstamp] => 1503039725
[title] => test
[alias] => test-3
[author] => 1
[inColumn] => main
[published] => 1
)
)

Merging two array elements in one

How can I do to merge "artist" value with "title" value in one value?. I'm using PHP 5.2.4 if that helps.
I have this array
Array
(
[0] => Array
(
[artist] => Narcosis
[title] => Destruir
[duration] => 137
[date] => 1370807642
[genre_id] => 18
)
[1] => Array
(
[artist] => Ricardo Palma Fanjul
[title] => Mutant
[duration] => 347
[date] => 1448227909
[genre_id] => 18
)
)
The expected output would be like this, just show element "title" with twice values:
Array
(
[0] => Array
(
[title] => Narcosis - Destruir
[duration] => 137
[date] => 1370807642
[genre_id] => 18
)
[1] => Array
(
[title] => Ricardo Palma Fanjul - Mutant
[duration] => 347
[date] => 1448227909
[genre_id] => 18
)
)
Try this:
foreach ($mainArr as $index=>$subArr) {
$subArr['title'] = $subArr['artist'].' - '.$subArr['title'];
unset($subArr['artist']);
$mainArr[$index] = $subArr;
}
mainArr is the array declared above.
p.s. I have not tested this code yet
Try this:
$new = array_map(function($v) {
$v['title'] = $v['artist'].' - '.$v['title'];
unset($v['artist']);
return $v;
}, $arr);
OR
foreach ($arr as &$a) {
$a['title'] = $a['artist'].' - '.$a['title'];
unset($a['artist']);
}

how to merge key array on array

and i got a problem (its big for me) :(
ok, i have some array like ...
Array(
[0] => Array
(
[id] => 1
[order_sn] => EU/2011/04/PO/5
[total] => 65
)
[1] => Array
(
[id] => 1
[order_sn] => EU/2011/04/RS/4
[total] => 230
)
[2] => Array
(
[id] => 1
[order_sn] => EU/2011/04/RS/3
[total] => 130
)
[3] => Array
(
[id] => 2
[order_sn] => EU/2011/04/RS/2
[total] => 100
)
[4] => Array
(
[id] => 2
[order_sn] => EU/2011/04/RS/1
[total] => 60
)
)
how to merge them if the array have same key value ... ?
the result that i need got is like this ...
Array(
[0] => Array
(
[id] => 1
[detail] => Array
(
[0] => Array
(
[order_sn] => EU/2011/04/PO/5
[total] => 65
)
[1] => Array
(
[order_sn] => EU/2011/04/RS/4
[total] => 230
)
[2] => Array
(
[order_sn] => EU/2011/04/RS/3
[total] => 130
)
)
)
[2] => Array
(
[id] => 2
[detail] => Array
(
[0] => Array
(
[order_sn] => EU/2011/04/RS/2
[total] => 100
)
[1] => Array
(
[order_sn] => EU/2011/04/RS/1
[total] => 60
)
)
)
)
im very need some help here, and im working on PHP ... what method should i do for this case?
i try too searching on google and in here ... but i dont know the keyword >.<
Many thanks before :)
regard, Stecy
Try like this:
<?php
$result = array();
foreach ($my_array as $v) {
$id = $v['id'];
$result[$id]['id'] = $id;
$result[$id]['detail'][] = array(
'order_sn' => $v['order_sn'],
'total' => $v['total'],
);
}
You can just loop over the array and build a resulting one:
// $a is your array
$r=array();
foreach($a as $v)
$r[$v['id']][]=array('order_sn'=>$v['order_sn'], 'total'=>$v['total']);
echo'<pre>';
var_dump($r);
Since you do the paring by ID, it is wise to have it as the key and all the data associated with it as the value. There's no need to also have id and detail.
foreach($origianlArray as $key => $value){
$newArray[$value['id']]['id'] = $value['id'];
$newArray[$value['id']]['detail'][] = array('order_sn' => $value['order_sn'], 'total' => $value['total']);
}
Check out the PHP array_merge function.

Categories