Sort function that sorts alphabetical and numbers - php

I have an array that looks like this:
Array ( [0] => Array ( [id] => 1103 [age] => 37 [gen] => C:3:2:5:1:4 ) [1] => Array ( [id] => 1104 [age] => 37 [gen] => A:3:1:4:1 ) [2] => Array ( [id] => 1105 [age] => 36 [gen] => A:3:2:3:2 ) [3] => Array ( [id] => 1106 [age] => 32 [gen] => B:2:5:1:2:2 ) [4] => Array ( [id] => 1107 [age] => 31 [gen] => C:3:4:4:5:3 [5] => Array ( [id] => 1104 [age] => 37 [gen] => A:3:1:4:1:6 ))
I want to sort the array by the gen key so the result will be like this:
A:3:1:4:1:6
A:3:2:3:2
B:2:5:1:2:2
C:3:2:5:1:4
C:3:4:4:5:3
I tried this code:
uasort($newArray, function($a, $b) {
return strnatcasecmp($a['ngen'], $b['ngen']);
});
But canĀ“t get the correct result. Any ideas?

This function will help you to sort the array, just pass the key by which the array is to be sort.
function build_sorter($key)
{
return function ($a, $b) use ($key) {
return strnatcmp($a[$key], $b[$key]);
};
}
Example Code:
$results = array(0 => array ( 'id' => 1103, 'age' => 37, 'gen' => 'C:3:2:5:1:4' ),
1 => array ( 'id' => 1104, 'age' => 37, 'gen' => 'A:3:1:4:1' ),
2 => array ( 'id' => 1105, 'age' => 36, 'gen' => 'A:3:2:3:2' ),
3 => array ( 'id' => 1106, 'age' => 32, 'gen' => 'B:2:5:1:2:2' ),
4 => array ( 'id' => 1107, 'age' => 31, 'gen' => 'C:3:4:4:5:3' ),
5 => array ( 'id' => 1104, 'age' => 37, 'gen' => 'A:3:1:4:1:6' )
);
usort($results, build_sorter('gen'));
echo "<pre>"; print_r($results) ;
Just call build_sorter('gen') in usort callback and pass key gen
This will hive you :
Array
(
[0] => Array
(
[id] => 1104
[age] => 37
[gen] => A:3:1:4:1
)
[1] => Array
(
[id] => 1104
[age] => 37
[gen] => A:3:1:4:1:6
)
[2] => Array
(
[id] => 1105
[age] => 36
[gen] => A:3:2:3:2
)
[3] => Array
(
[id] => 1106
[age] => 32
[gen] => B:2:5:1:2:2
)
[4] => Array
(
[id] => 1103
[age] => 37
[gen] => C:3:2:5:1:4
)
[5] => Array
(
[id] => 1107
[age] => 31
[gen] => C:3:4:4:5:3
)
)

<?php
$ar = [
["id" => 1103, "age" => 37, "gen" => "C:3:2:5:1:4"],
["id" => 1104, "age" => 37, "gen" => "A:3:1:4:1"],
["id" => 1105, "age" => 36, "gen" => "A:3:2:3:2"],
["id" => 1106, "age" => 32, "gen" => "B:2:5:1:2:2"],
["id" => 1107, "age" => 31, "gen" => "C:3:4:4:5:3"],
["id" => 1108, "age" => 37, "gen" => "A:3:1:4:1:6"],
];
usort($ar, function ($a, $b) {
return strcmp($a['gen'], $b['gen']);
});
foreach($ar as $r)
echo($r['id'] . " " . $r['gen'] . PHP_EOL);
Will give you:
1104 A:3:1:4:1
1108 A:3:1:4:1:6
1105 A:3:2:3:2
1106 B:2:5:1:2:2
1103 C:3:2:5:1:4
1107 C:3:4:4:5:3

Related

Merge array by id and sum amount

I'm trying to merge and calc only by choosen key. For example I have the following array:
$array = array();
$array['Fruit'][] = ['id' => 17, 'name' => 'seasonal fruit', 'type' => 'Fruit', 'measurement' => '', 'amount' => 2];
$array['Fruit'][] = ['id' => 17, 'name' => 'seasonal fruit', 'type' => 'Fruit', 'measurement' => '', 'amount' => 1];
$array['protein'][] = ['id' => 16, 'name' => 'soy yogurt', 'type' => 'protein', 'measurement' => '', 'amount' => 2];
$array['protein'][] = ['id' => 18, 'name' => 'oilseed', 'type' => 'protein', 'measurement' => 'grip', 'amount' => 2];
$array['protein'][] = ['id' => 18, 'name' => 'oilseed', 'type' => 'protein', 'measurement' => 'grip', 'amount' => 1];
$array['lipid'][] = ['id' => 19, 'name' => 'coconut powder', 'type' => 'lipid', 'measurement' => 'Tablespoon', 'amount' => 2];
$array['lipid'][] = ['id' => 20, 'name' => 'chocolate powder', 'type' => 'lipid', 'measurement' => 'Tablespoon', 'amount' => 2];
$array['lipid'][] = ['id' => 38, 'name' => 'chocolate square', 'type' => 'lipid', 'measurement' => '', 'amount' => 1];
Final output should look like this :
Array (
[0] => Array (
[id] => 17
[name] => seasonal fruit
[type] => Fruit
[measurement] =>
[amount] => 3
)
[1] => Array (
[id] => 16
[name] => soy yogurt
[type] => protein
[measurement] =>
[amount] => 2
)
[2] => Array (
[id] => 18
[name] => oilseed
[type] => protein
[measurement] => grip
[amount] => 3
)
[3] => Array (
[id] => 19
[name] => coconut powder
[type] => lipid
[measurement] => Tablespoon
[amount] => 2
)
[4] => Array (
[id] => 20
[name] => chocolate powder
[type] => lipid
[measurement] => Tablespoon
[amount] => 2
)
[5] => Array (
[id] => 38
[name] => chocolate square
[type] => lipid
[measurement] =>
[amount] => 1
)
)
I tried the following :
$courses = [];
foreach ($array as $key => $item) {
if (!key_exists($key, $courses)) {
$courses[$key] = [];
}
foreach ($item as $row) {
$id = $row['id'];
if (!key_exists($id, $courses[$key])) {
$courses[$key][$id] = $row;
continue;
}
$r = &$courses[$key][$id];
$r['amount'] += $row['amount'];
}
}
Output:
Array (
[Fruit] => Array (
[17] => Array (
[id] => 17
[name] => seasonal fruit
[type] => Fruit
[measurement] =>
[amount] => 3
)
)
[protein] => Array (
[16] => Array (
[id] => 16
[name] => soy yogurt
[type] => protein
[measurement] =>
[amount] => 2
)
[18] => Array (
[id] => 18
[name] => oilseed
[type] => protein
[measurement] => grip
[amount] => 3
)
)
[lipid] => Array (
[19] => Array (
[id] => 19
[name] => coconut powder
[type] => lipid
[measurement] => Tablespoon
[amount] => 2
)
[20] => Array (
[id] => 20
[name] => chocolate powder
[type] => lipid
[measurement] => Tablespoon
[amount] => 2
)
[38] => Array (
[id] => 38
[name] => chocolate square
[type] => lipid
[measurement] =>
[amount] => 1
)
)
)
You were not far off, but everything you are interested in ins inside the inner foreach, that way you dont get the Fruit and Protien etc involved
$courses = [];
foreach( $array as $sub){
foreach ($sub as $item) {
if ( array_key_exists($item['id'], $courses) ) {
// dup, so just add to the amount
$courses[$item['id']]['amount'] += $item['amount'];
} else {
//New entry
$courses[$item['id']] = $item;
}
}
}
// if its important to have indexes starting at 0 for the result
$courses = array_values($courses);

how to get a slice of a multidimensional array using an array with index keys

I have an array
$arr = [
[
'id' => 10,
'name' => 'John',
'occupation' => 'engineer',
'points' => 10
],
[
'id' => 10,
'name' => 'John',
'occupation' => 'librarian',
'points' => 14
],
[
'id' => 7,
'name' => 'Sarah',
'occupation' => 'artist',
'points' => 21
],
[
'id' => 7,
'name' => 'Sarah',
'occupation' => 'teacher',
'points' => 17
],
[
'id' => 10,
'name' => 'John',
'occupation' => 'butcher',
'points' => 7
],
[
'id' => 7,
'name' => 'Sarah',
'occupation' => 'engineer',
'points' => 9
],
[
'id' => 25,
'name' => 'Andreea',
'occupation' => 'judge',
'points' => 11
]
];
And I use this built in functions to get unique ids:
$people = array_column($arr, 'id', 'id');
And then I use a foreach to get every occurrence of each user in the main array $arr:
foreach($people as $id){
$keys = array_keys(array_column($arr, 'id'), $id);
}
This is the return:
Array
(
[0] => 0
[1] => 1
[2] => 4
)
Array
(
[0] => 2
[1] => 3
[2] => 5
)
Array
(
[0] => 6
)
Now in order to build a small array for each person I could loop trough this small arrays that contain the keys from the main array and get the values and ending up with small slices.
But, how can I get the actual $arr values for each person instead of getting just the keys? (using as little resources as possible)
I need the result to be like this:
Array
(
[10] => Array
(
[0] => Array
(
[id] => 10
[name] => John
[occupation] => engineer
[points] => 10
)
[1] => Array
(
[id] => 10
[name] => John
[occupation] => librarian
[points] => 14
)
[2] => Array
(
[id] => 10
[name] => John
[occupation] => butcher
[points] => 7
)
)
[7] => Array
(
[0] => Array
(
[id] => 7
[name] => Sarah
[occupation] => artist
[points] => 21
)
[1] => Array
(
[id] => 7
[name] => Sarah
[occupation] => teacher
[points] => 17
)
[2] => Array
(
[id] => 7
[name] => Sarah
[occupation] => engineer
[points] => 9
)
)
[25] => Array
(
[0] => Array
(
[id] => 25
[name] => Andreea
[occupation] => judge
[points] => 11
)
)
)
P.S: I don't need to keep the key index the same.
You can do it in a more efficient way,Demo
$result = [];
foreach($arr as $v){
$result[$v["id"]][] = $v;
}
print_r($result);
use a function of group by like:
$byGroup = group_by("id", $arr);
function group_by($key, $data) {
$result = array();
foreach($array as $val) {
if(array_key_exists($key, $val)){
$result[$val[$key]][] = $val;
}else{
$result[""][] = $val;
}
}
return $result;
}

Remove parent array including the children if the children value found empty

I have this array structure.
I'm trying to remove those whole array if I found the [empevalpptwo] empty or null, so if the [empevalpptwo] is empty the whole [4] => Array should be remove.
Right now i'm just trying to get the parent index so I can just remove it by index
Is there any good solution like filtering recursively?
$kra = array_column($ppform_plan->toArray(), 'kra');
$emp_eval_pptwo = array_search('', array_column($kra, 'empevalpptwo'));
//should return 4
Array
(
[4] => Array
(
[id] => 50
[user_id] => 6282
[specific_user_id] => 6281
[eval_cat_id] => 2
[format_cat] => 1
[title] => This istesting
[desc] =>
[weight] => 50
[bsc_weight_group] =>
[bsc_rating] =>
[sequence] =>
[created_at] => 2019-05-22 10:55:23
[updated_at] => 2019-05-22 10:55:23
[kra] => Array
(
[0] => Array
(
[id] => 77
[user_id] => 6282
[bsc_id] => 50
[index] => 0
[kra_title] => ttes lang muna
[kra_desc] =>
[kra_weight] => 25
[sat] => 521
[at] => 4
[ot] => 535
[rating_per_kra] =>
[rating_per_kra_cat] =>
[net_weighting] => 5
[rank] => 1
[remarks] =>
[indicator_text] =>
[created_at] =>
[updated_at] =>
[empevalpptwo] =>
)
[1] => Array
(
[id] => 78
[user_id] => 6282
[bsc_id] => 50
[index] => 1
[kra_title] => talga e2 pa o
[kra_desc] =>
[kra_weight] => 25
[sat] => 5
[at] => 2
[ot] => 4
[rating_per_kra] =>
[rating_per_kra_cat] =>
[net_weighting] => 4
[rank] => 2
[remarks] =>
[indicator_text] =>
[created_at] =>
[updated_at] =>
[empevalpptwo] =>
)
)
)
)
Something akin to:
<?php
$items =
[
1 =>
[
'foo'=>
[
[
'bar' => '',
],
[
'bar' => '',
]
]
],
2 =>
[
'foo'=>
[
[
'bar' => 'baz'
]
]
]
];
foreach ($items as $k => $item)
if(empty(array_filter(array_column($item['foo'], 'bar'))))
unset($items[$k]);
var_export($items);
Output:
array (
2 =>
array (
'foo' =>
array (
0 =>
array (
'bar' => 'baz',
),
),
),
)
You should try this :
$data = array_map('array_filter', $data);
$data = array_filter($data);
UPDATED
and for checking inner values you can use this :
$array = array_filter($array, function($v) { return !empty($v['empevalpptwo']); });
Orginal
Array
(
[con] => Array
(
[address] => Array
(
[city] => ahmedabad
)
[personalinfo] =>
)
)
Result :
Array
(
[con] => Array
(
[address] => Array
(
[city] => ahmedabad
)
)
)

Get 3 element per category From array in PHP

I have 2 arrays category and product_to_category (Structure shown below)
[category] => Array
(
[0] => 299
[2] => 342
[3] => 134
[4] => 333
[5] => 347
[9] => 296
)
And
[product_to_category] => Array
(
[0] => Array
(
[product_id] => 23895
[category_id] => 296
[relevance] => 77.73432159423828
)
[1] => Array
(
[product_id] => 17218
[category_id] => 296
[relevance] => 77.73432159423828
)
[2] => Array
(
[product_id] => 23896
[category_id] => 347
[relevance] => 77.73432159423828
)
....So On (5000+ records)
i have almost 5000+ records in product_to_category array, But i want to pick only 3 products from P2C array per Category. For Example category_id=299 so 3 products of 299 will be added to my new array and same for other categories.
Is there any possible way to do that .
Try this query to fetch record you wanted,
select p2c.product_id, p2c.category_id, p2c.relevance
from category c
LEFT JOIN product_to_category p2c ON p2c.category_id = category.category_id
GROUP BY p2c.product_id, p2c.category_id
HAVING count(*) <= 3
ORDER BY category_id, product_id
You can refer table category to category table and product_to_category to your table name in database.
$category = [0 => 299, 2 => 342, 3 => 134, 4 => 333, 5 => 347, 9 => 296];
$product_to_category = Array
(
0 => Array
(
'product_id' => 23895,
'category_id' => 296,
'relevance' => 77.73432159423828
),
1 => Array
(
'product_id' => 17218,
'category_id' => 296,
'relevance' => 77.73432159423828
),
2 => Array
(
'product_id' => 23896,
'category_id' => 347,
'relevance' => 77.73432159423828
),
3 => Array
(
'product_id' => 23897,
'category_id' => 296,
'relevance' => 77.73432159423828
),
4 => Array
(
'product_id' => 23899,
'category_id' => 296,
'relevance' => 77.73432159423828
),
5 => Array
(
'product_id' => 23894,
'category_id' => 347,
'relevance' => 77.73432159423828
),
6 => Array
(
'product_id' => 23892,
'category_id' => 347,
'relevance' => 77.73432159423828
),
7 => Array
(
'product_id' => 23833,
'category_id' => 347,
'relevance' => 77.73432159423828
)
);
$new_product_to_category = [];
foreach ($category as $categories) {
$count_category_ids = [];
$i=0;
foreach ($product_to_category as $product_to_categories){
if ($categories == $product_to_categories['category_id']){
$count_category_ids[] = $product_to_categories['category_id'];
if (count($count_category_ids) < 4) {
$new_product_to_category[$i]['product_id'] = $product_to_categories['product_id'];
$new_product_to_category[$i]['category_id'] = $product_to_categories['category_id'];
$new_product_to_category[$i]['relevance'] = $product_to_categories['relevance'];
}
}
$i++;
}
}
echo "<pre>";
print_r($new_product_to_category);
Output shows like this :
Array
(
[2] => Array
(
[product_id] => 23896
[category_id] => 347
[relevance] => 77.734321594238
)
[5] => Array
(
[product_id] => 23894
[category_id] => 347
[relevance] => 77.734321594238
)
[6] => Array
(
[product_id] => 23892
[category_id] => 347
[relevance] => 77.734321594238
)
[0] => Array
(
[product_id] => 23895
[category_id] => 296
[relevance] => 77.734321594238
)
[1] => Array
(
[product_id] => 17218
[category_id] => 296
[relevance] => 77.734321594238
)
[3] => Array
(
[product_id] => 23897
[category_id] => 296
[relevance] => 77.734321594238
)
)
$categories = array(6, 5, 3, 4, 2, 1);
$products = array(
array('product_id' => 231, 'category_id' => 1, 'relevance' => 321),
array('product_id' => 232, 'category_id' => 4, 'relevance' => 322),
array('product_id' => 233, 'category_id' => 2, 'relevance' => 323),
array('product_id' => 234, 'category_id' => 4, 'relevance' => 324),
array('product_id' => 235, 'category_id' => 4, 'relevance' => 325),
array('product_id' => 236, 'category_id' => 2, 'relevance' => 326),
array('product_id' => 237, 'category_id' => 1, 'relevance' => 327),
array('product_id' => 238, 'category_id' => 4, 'relevance' => 328),
array('product_id' => 239, 'category_id' => 1, 'relevance' => 329),
array('product_id' => 240, 'category_id' => 1, 'relevance' => 330)
);
$categoryWiseProduct = array();
foreach ($products as $p) {
foreach ($categories as $c) {
if ($c == $p['category_id']) {
if (isset($categoryWiseProduct[$c]) && count($categoryWiseProduct[$c]) > 2) {
break;
}
$categoryWiseProduct[$c][] = $p;
}
}
}
echo'<pre>';print_r($categoryWiseProduct);die;
Output will be:
Array
(
[1] => Array
(
[0] => Array
(
[product_id] => 231
[category_id] => 1
[relevance] => 321
)
[1] => Array
(
[product_id] => 237
[category_id] => 1
[relevance] => 327
)
[2] => Array
(
[product_id] => 239
[category_id] => 1
[relevance] => 329
)
)
[4] => Array
(
[0] => Array
(
[product_id] => 232
[category_id] => 4
[relevance] => 322
)
[1] => Array
(
[product_id] => 234
[category_id] => 4
[relevance] => 324
)
[2] => Array
(
[product_id] => 235
[category_id] => 4
[relevance] => 325
)
)
[2] => Array
(
[0] => Array
(
[product_id] => 233
[category_id] => 2
[relevance] => 323
)
[1] => Array
(
[product_id] => 236
[category_id] => 2
[relevance] => 326
)
)
)
try this code
$collection = array();
foreach($arr as $key => $value){
if(
isset($collection[$value['category_id']])
&&
count($collection[$value['category_id']]) >= 3
) continue;
$collection[$value['product_id']][] = $value['product_id'];
}
In this way, you will get 3 products from each category.
You can add more condition if you need, like check the product id you can use inside if condition
&& in_array($product_id, $collection[$value['category_id']])

php - sorting by points and then alphabetically

I have an array:
Array (
[0] => Array
( [points] => 10
[id] => 58
[nazwa] => auser1 )
[1] => Array
( [points] => 15
[id] => 36
[nazwa] => cuser2 )
[2] => Array
( [points] => 15
[id] => 57
[nazwa] => buser3 )
[3] => Array
( [points] => 20
[id] => 56
[nazwa] => duser4 )
[4] => Array
( [points] => 20
[id] => 54
[nazwa] => euser5 ))
I would like to sort this array by points and then alphabetically by nazwa.
How can I do this?
I would like to create final points table for Russia Cup!
if you want to sort your multidimensional array in sequence first points then with name then you have to create your multidimensional array in same sequence format
example: first element should be points, second name, last id. Refer following sequence.
$array = [ [ 'points' => 10, 'nazwa' => 'auser1', 'id' => 58 ],
[ 'points' => 15, 'nazwa' => 'cuser2', 'id' => 36 ],
[ 'points' => 15, 'nazwa' => 'buser3', 'id' => 57 ],
[ 'points' => 20, 'nazwa' => 'duser4', 'id' => 56 ],
[ 'points' => 20, 'nazwa' => 'euser5', 'id' => 54 ]];
array_multisort( $array );
print_r(($array));
Output:
Array
(
[0] => Array
(
[points] => 10
[nazwa] => auser1
[id] => 58
)
[1] => Array
(
[points] => 15
[nazwa] => buser3
[id] => 57
)
[2] => Array
(
[points] => 15
[nazwa] => cuser2
[id] => 36
)
[3] => Array
(
[points] => 20
[nazwa] => duser4
[id] => 56
)
[4] => Array
(
[points] => 20
[nazwa] => euser5
[id] => 54
)
)

Categories