I have an array which looks like
Array
(
[0] => Array
(
[id] => 39662
[points] => 24
[subject] => 112
)
[1] => Array
(
[id] => 39609
[points] => 24
[subject] => 87
)
[2] => Array
(
[id] => 39610
[points] => 23
[subject] => 77
)
[3] => Array
(
[id] => 39608
[points] => 23
[subject] => 87
)
[4] => Array
(
[id] => 39606
[points] => 22
[subject] => 60
)
[5] => Array
(
[id] => 39604
[points] => 19
[subject] => 75
)
[6] => Array
(
[id] => 39595
[points] => 18
[subject] => 60
)
[7] => Array
(
[id] => 39605
[points] => 18
[subject] => 47
)
[8] => Array
(
[id] => 39650
[points] => 17
[subject] => 87
)
[9] => Array
(
[id] => 39660
[points] => 17
[subject] => 55
)
)
Now I want to sort then based on count of key subject. You can see that subjuet = 87 have 3 records and subject = 60 has two records, so all three records of 87 should display first , after this records of 60 , then others.
I tried array_multisort but its not giving expected result.
Thanks
As per your desired output, you just need to array_map() with array_multisort(),
Example:
<?php
// Test Array
$array = array(
array('id'=>39662,'points'=>'24','subject'=>112),
array('id'=>39609,'points'=>'24','subject'=>87),
array('id'=>39610,'points'=>'23','subject'=>77),
array('id'=>39608,'points'=>'23','subject'=>87),
array('id'=>39606,'points'=>'22','subject'=>60),
array('id'=>39604,'points'=>'19','subject'=>75),
array('id'=>39595,'points'=>'18','subject'=>60),
array('id'=>39605,'points'=>'18','subject'=>47),
array('id'=>39650,'points'=>'17','subject'=>87),
array('id'=>39660,'points'=>'17','subject'=>55),
);
$newArr = array(); // initialize the new Array
foreach ($array as $key => $value)
{
$newArr[$value['subject']][] = $value;
}
array_multisort(array_map('count', $newArr), SORT_DESC, $newArr); // using array_multisort() and Sort as DESC order by using array_map()
echo "<pre>";
print_r($newArr);
?>
Result:
Array
(
[0] => Array
(
[0] => Array
(
[id] => 39609
[points] => 24
[subject] => 87
)
[1] => Array
(
[id] => 39608
[points] => 23
[subject] => 87
)
[2] => Array
(
[id] => 39650
[points] => 17
[subject] => 87
)
)
[1] => Array
(
[0] => Array
(
[id] => 39606
[points] => 22
[subject] => 60
)
[1] => Array
(
[id] => 39595
[points] => 18
[subject] => 60
)
)
[2] => Array
(
[0] => Array
(
[id] => 39604
[points] => 19
[subject] => 75
)
)
[3] => Array
(
[0] => Array
(
[id] => 39605
[points] => 18
[subject] => 47
)
)
[4] => Array
(
[0] => Array
(
[id] => 39610
[points] => 23
[subject] => 77
)
)
[5] => Array
(
[0] => Array
(
[id] => 39660
[points] => 17
[subject] => 55
)
)
[6] => Array
(
[0] => Array
(
[id] => 39662
[points] => 24
[subject] => 112
)
)
)
Try the following method for your case
$data[] = array('points' => 67, 'subject' => 2);
$data[] = array('points' => 86, 'subject' => 1);
$data[] = array('points' => 85, 'subject' => 6);
$data[] = array('points' => 98, 'subject' => 2);
$data[] = array('points' => 86, 'subject' => 6);
$data[] = array('points' => 67, 'subject' => 7);
// Obtain a list of columns
foreach ($data as $key => $row) {
$subject[$key] = $row['subject'];
}
// Sort the data with volume descending, edition ascending
// Add $data as the last parameter, to sort by the common key
array_multisort($subject, SORT_DESC, $data);
Example usage with my array from the php manual.
Output of the above was
Array
(
[0] => Array
(
[points] => 67
[subject] => 7
)
[1] => Array
(
[points] => 85
[subject] => 6
)
[2] => Array
(
[points] => 86
[subject] => 6
)
[3] => Array
(
[points] => 67
[subject] => 2
)
[4] => Array
(
[points] => 98
[subject] => 2
)
[5] => Array
(
[points] => 86
[subject] => 1
)
)
function array_sort($array, $key){
$new_array = array();
$sortable_array = array();
if (count($array) > 0) {
foreach ($array as $k => $v) {
if (is_array($v)) {
foreach ($v as $k2 => $v2) {
if ($k2 == $key) {
$sortable_array[$k] = $v2;
}
}
} else {
$sortable_array[$k] = $v;
}
}
asort($sortable_array);
}
foreach ($sortable_array as $k => $v) {
$new_array[$k] = $array[$k];
}
}
return $new_array;
}
array_sort($a, 'subjects');
//$a is array which want to sort
Related
I need to subtract the qt from two arrays based on id and type, keeping the array complete.
How do I do in php to be able to subtract these arrays?
I have 2 arrays:
=> this is the first array with multiple key "down"
Array
(
[0] => Array
(
[id] => 26
[loc] => 1
[type] => down
[qt] => 12
)
[1] => Array
(
[id] => 32
[loc] => 1
[type] => down
[qt] => 34
)
[2] => Array
(
[id] => 26
[loc] => 2
[type] => down
[qt] => 5
)
[3] => Array
(
[id] => 86
[loc] => 3
[type] => down
[qt] => 45
)
[4] => Array
(
[id] => 23
[loc] => 9
[type] => down
[qt] => 3
)
[5] => Array
(
[id] => 23
[loc] => 3
[type] => down
[qt] => 99
)
)
=> this is the second array with multiple key "up"
Array
(
[0] => Array
(
[id] => 26
[loc] => 1
[type] => up
[qt] => 5
)
[1] => Array
(
[id] => 86
[loc] => 3
[type] => up
[qt] => 27
)
[2] => Array
(
[id] => 23
[loc] => 9
[type] => up
[qt] => 3
)
)
=> I need cubtract "qt" (if "id" and "loc" are the same then subtract the "qt")
Array
(
[0] => Array
(
[id] => 26
[loc] => 1
[type] => total
[qt] => 7
)
[1] => Array
(
[id] => 32
[loc] => 1
[type] => total
[qt] => 34
)
[2] => Array
(
[id] => 26
[loc] => 2
[type] => total
[qt] => 5
)
[3] => Array
(
[id] => 86
[loc] => 3
[type] => total
[qt] => 18
)
[4] => Array
(
[id] => 23
[loc] => 9
[type] => total
[qt] => 0
)
[5] => Array
(
[id] => 23
[loc] => 3
[type] => down
[qt] => 99
)
)
Try this out
function findMatch($array, $id, $loc)
{
foreach ($array as $key => $item) {
if ($item["id"] == $id and $item["loc"] == $loc) {
return $key;
}
}
return false;
}
$total = [];
foreach($down as $d) {
$matched = findMatch($up, $d["id"], $d["loc"]);
if($matched !== false){
$total[] = array_replace($d, [
"type" => "total",
"qt" => ($d["qt"] - $up[$matched]["qt"])
]);
} else {
$total[] = array_replace($d, ["type" => "total"]);
}
}
print_r($total);
This array type of out I required.
Array
(
[2018-03-25] => Array
(
[0] => Array
(
[loyalty_transcation_id] => 33
[user_id] => 58
[bill_copy_image_path] => 03252018_182712.jpg
[create_date] => 2018-03-25 19:27:12
[name] => bernd koch
)
[1] => Array
(
[loyalty_transcation_id] => 3
[user_id] => 21
[bill_copy_image_path] => 09132017_044456.jpg
[create_date] => 2018-03-25 05:44:56
[name] => Rahul Upadhyay
)
)
[2017-09-27] => Array
(
[0] => Array
(
[loyalty_transcation_id] => 10
[user_id] => 24
[bill_copy_image_path] => 09272017_115913.jpg
[create_date] => 2017-09-27 12:59:13
[name] => raj#ot.com
)
[1] => Array
(
[loyalty_transcation_id] => 3
[user_id] => 21
[bill_copy_image_path] => 09132017_044456.jpg
[create_date] => 2017-09-27 05:44:56
[name] => Rahul Upadhyay
)
)
[2017-09-28] => Array
(
[0] => Array
(
[loyalty_transcation_id] => 10
[user_id] => 24
[bill_copy_image_path] => 09272017_115913.jpg
[create_date] => 2017-09-28 12:59:13
[name] => raj#ot.com
)
)
I want to save all data using date as key and if one date has multiple array than merge data into one the same key. Key should not be different. And All other which having same date with not repeated values also should have save with same procedure. Below is code which I've written.
$dups = $new_arr = array();
foreach ($query->rows as $key => $val) {
$new = date('Y-m-d', strtotime($val['create_date']));
if (!isset($new_arr[$new])) {
$new_arr[$new] = $val;
//die;
} else {
if (isset($dups[$new])) {
$dups[$new][] = $val;
//print_r($dups[$new]);
//echo "\n";
} else {
//$dups[$new] = array($val);
$dups[$new] = array($new_arr[$new], $value);
}
}
}
//echo "<pre>";
print_R($dups);
die;
The code is returning only the data for repeated values and not returning data which are not repeating. Also keys are returning fine but the values are merging from another date. Any help will be appreciated.
Input Array Which we want to sort:
Array
(
[0] => Array
(
[loyalty_transcation_id] => 36
[user_id] => 23
[bill_copy_image_path] => 05082018_144348.jpg
[create_date] => 2018-05-08 15:43:48
[name] => Admin
)
[1] => Array
(
[loyalty_transcation_id] => 35
[user_id] => 18
[bill_copy_image_path] => 04052018_160009.jpg
[create_date] => 2018-04-05 17:00:09
[name] => gurpreet.singh#outlook.de
)
[2] => Array
(
[loyalty_transcation_id] => 33
[user_id] => 58
[bill_copy_image_path] => 03252018_182712.jpg
[create_date] => 2018-03-25 19:27:12
[name] => bernd koch
)
[3] => Array
(
[loyalty_transcation_id] => 32
[user_id] => 57
[bill_copy_image_path] => 03252018_160706.jpg
[create_date] => 2018-03-25 17:07:06
[name] => alex
)
)
This should work:
$responseArr = array();
foreach($inputArr as $input) {
$dateKey = date('Y-m-d', strtotime($input['create_date']));
if(!isset($responseArr[$dateKey])) {
$responseArr[$dateKey] = array();
}
array_push($responseArr[$dateKey], $input);
}
I have an PHP array like this:
Array
(
[0] => Array
(
[option_id] => 21
[header_image] => logo.png
)
[1] => Array
(
[option_id] => 21
[menu1] => About
)
[2] => Array
(
[option_id] => 22
[menu2] => Speaker
)
[3] => Array
(
[option_id] => 22
[menu3] => Agenda
)
[4] => Array
(
[option_id] => 22
[menu4] => Venue
)
[5] => Array
(
[option_id] => 23
[menu5] => Hotel
)
[6] => Array
(
[option_id] => 23
[menu6] => Sponsors
)
)
I want array like this:
Array
(
[0] => Array
(
[option_id] => 21
[header_image] => logo.png
[menu1] => About
)
[1] => Array
(
[option_id] => 22
[menu2] => Speaker
[menu3] => Agenda
[menu4] => Venue
)
[2] => Array
(
[option_id] => 23
[menu5] => Hotel
[menu6] => Sponsors
)
)
I want to bind array having same option_id.
How can I achieve this?
You need to loop over the array with foreach
<?php
$output = array();
if (! empty($arr)) {
foreach ($arr as $elem) {
if (! empty($elem)) {
foreach ($elem as $k => $v) {
$output[$elem['option_id']][$k] = $v;
}
}
}
}
I have a 2D-array which contains product id, product name and company id, now i want to make group of company having product information and can mail to company with product details, i have data in array like
Array
(
[0] => Array
(
[id] => 5363
[proname] => pro1
[company_id] => 101
)
[1] => Array
(
[id] => 5364
[proname] => pro2
[company_id] => 105
)
[2] => Array
(
[id] => 5366
[proname] => pro3
[company_id] => 102
)
[3] => Array
(
[id] => 5367
[proname] => pro4
[company_id] => 101
)
[4] => Array
(
[id] => 5368
[proname] => pro5
[company_id] => 105
)
[5] => Array
(
[id] => 5369
[proname] => pro6
[company_id] => 105
)
[6] => Array
(
[id] => 3847
[proname] => pro7
[company_id] => 102
)
[7] => Array
(
[id] => 3849
[proname] => pro8
[company_id] => 105
)
[8] => Array
(
[id] => 5371
[proname] => pro9
[company_id] => 101
)
[9] => Array
(
[id] => 5383
[proname] => pro10
[company_id] => 102
)
[10] => Array
(
[id] => 5385
[proname] => pro11
[company_id] => 105
)
)
And i want to make array chunks on the basis of company_id thus final array should be 3-d array and output should like this
array(
[0]=>Array(
[0] => Array
(
[id] => 5363
[proname] => pro1
[company_id] => 101
)
[1] => Array
(
[id] => 5367
[proname] => pro4
[company_id] => 101
)
[2] => Array
(
[id] => 5371
[proname] => pro9
[company_id] => 101
)
)
[1]=Array
(
[0] => Array
(
[id] => 5364
[proname] => pro2
[company_id] => 105
)
[1] => Array
(
[id] => 5368
[proname] => pro5
[company_id] => 105
)
[2] => Array
(
[id] => 5369
[proname] => pro6
[company_id] => 105
)
[3] => Array
(
[id] => 3849
[proname] => pro8
[company_id] => 105
)
[4] => Array
(
[id] => 5385
[proname] => pro11
[company_id] => 105
)
)
[2]=Array(
[0] => Array
(
[id] => 5366
[proname] => pro3
[company_id] => 102
)
[1] => Array
(
[id] => 3847
[proname] => pro7
[company_id] => 102
)
[2] => Array
(
[id] => 5383
[proname] => pro10
[company_id] => 102
)
)
)
aim is to make group as for company.
try this:
the easiest way:
$newarray = array();
foreach($array as $values) {
$newarray[$values['company_id']][] = $values;
}
$newarray = array_values($newarray);
another way:
<?php
$newarray = array();
foreach($array as $key=>$val){// here $array is your given array.
if(isset($newarray[$val['company_id']])){
array_push($newarray[$val['company_id']],$val);
}else{
$newarray[$val['company_id']][0]=$val;
}
}
$newarray = array_values($newarray);
echo "<pre>";
print_r($newarray);
echo "</pre>";
?>
Loop through the array.
Here, you get second dimension arrays consisting of third level (our expected arrays).
Check if the key is company_id and append to a new array we have created before the loop.
$newArr = array();
foreach ($arr as $array) {
foreach ($array as $k=>$v) {
if ($k == 'company_id') {
$newArr[$k][] = $array;
}
}
}
print_r($newArr); // Your new array()
$groupBycompany = [];
if ($companies) {
foreach ($companies as $keys => $values) {
if (isset($values['company_id'])){
$groupByCompany[$values['company_id']][] = $values;
}
}
}
I have an array,
$arr=(
[0] => Array
(
[groupid] => 1
[groupname] => Red
[members] => Array
(
[0] => Array
(
[mid] => 9
[name] => Anith
)
[1] => Array
(
[mid] => 11
[name] => Aravind
)
[2] => Array
(
[mid] => 10
[name] => Lekshmi
)
)
)
[1] => Array
(
[groupid] => 2
[groupname] => Blue
[members] => Array
(
[0] => Array
(
[mid] => 6
[name] => Yamuna
)
[1] => Array
(
[mid] => 2
[name] => Kamala K
)
[2] => Array
(
[mid] => 13
[name] => Sooraj K
)
)
)
I want to check [mid] => 2 is in the array..If it exists
I want to delete it(ie. unset the array )-----
[1] => Array
(
[mid] => 2
[name] => Kamala K
)
;;;
eg:--unset($arr[1]['members'][2];
This should do the trick
foreach ($arr as $group => $subarray) {
foreach ($subarray['members'] as $k => $v) {
if ($v['mid'] == 2) {
unset($arr[$group]['members'][$k]);
break;
}
}
}
var_dump($arr);
If you feel like getting crafty, you could do something like this:
// note: requires PHP >= 5.3
foreach ($arr as $key => &$value) {
$value['members'] = array_filter(
$value['members'],
function($member) {
return $member['mid'] != 2;
}
);
}
var_dump($arr);