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
Related
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.
$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);
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);
I have following arrays:
1) for total placed
Array
(
[0] => Array
(
[centers] => Array
(
[name] => delhi
[id] => 1
)
[0] => Array
(
[totalplaced] => 8
)
)
[1] => Array
(
[centers] => Array
(
[name] => mumbai
[id] => 2
)
[0] => Array
(
[totalplaced] => 1
)
)
)
2) for total working
Array
(
[0] => Array
(
[centers] => Array
(
[name] => delhi
[id] => 1
)
[0] => Array
(
[totalworking] => 4
)
)
[1] => Array
(
[centers] => Array
(
[name] => mumbai
[id] => 2
)
[0] => Array
(
[totalworking] => 1
)
)
)
3) for total trained
Array
(
[0] => Array
(
[centers] => Array
(
[name] => delhi
[id] => 1
)
[0] => Array
(
[totaltrained] => 8
)
)
[1] => Array
(
[centers] => Array
(
[name] => mumbai
[id] => 2
)
[0] => Array
(
[totaltrained] => 1
)
)
)
I wanted to merge these arrays so that the resultant array should look like this
[newarray] => Array(
[0] => Array (
[centers] => Array
(
[name] => delhi
[id] => 1
[totalplaced] => 8
[totalworking] => 4
[totaltrained] => 8
)
)
[1]=> Array(
[centers] => Array
(
[name] => mumbai
[id] => 2
[totalplaced] => 1
[totalworking] => 1
[totaltrained] => 1
)
)
)
This is the tabular representation of the above data which i want to display
centername totalplaced totalworking totaltrained
delhi 8 4 8
mumbai 1 1 1
Please help me on this.
Thanks
Pankaj Khurana
The difficulty here is that PHP's functions such as array_merge() and array_merge_recursive() will not merge data into numeric keys, but rather will re-key any duplicate numeric key. So for example given two arrays:
array(
'test' => 'abc',
0 => 'xyz'
);
array(
'test' => 'def',
0 => 'uvw'
);
Merging them together with array_merge() will produce an array like:
array(
'test' => 'def',
0 => 'xyz',
1 => 'uvw'
);
So, you need a custom function to be "additive" on any key, regardless of whether it is a string or numeric key. Try this:
function mixed_key_array_merge() {
$args = func_get_args();
$result = array();
foreach ($args as $arg) {
// discard non-array arguments; maybe this could be better handled
if (!is_array($arg)) {
continue;
}
foreach ($arg as $key => $value) {
if (!isset($result[$key])) {
$result[$key] = $value;
} else if (is_array($result[$key])) {
$result[$key] = call_user_func_array('mixed_key_array_merge',array($result[$key],$value));
}
}
}
return $result;
}