sorting array of array in laravel - php

I have an array of array, I would like to sort it base on the number in each array
[
["Plastic and cosmetic surgery",90],
["Dermatology surgery",121],
["Infertility",134],
["Gynecology surgery",191],
["Hair transplant",92],
["Bariatrics and weight loss surgery",117],
["Dentistry",88]
]

There is an amazing and simple default laravel solution for this.
$collection = collect([
["Plastic and cosmetic surgery",90],
["Dermatology surgery",121],
["Infertility",134],
["Gynecology surgery",191],
["Hair transplant",92],
["Bariatrics and weight loss surgery",117],
["Dentistry",88]
]);
$sorted = $collection->sortBy(1);
$data = $sorted->values()->all();
dd($data);
Output
array:7 [▼
0 => array:2 [▼
0 => "Dentistry"
1 => 88
]
1 => array:2 [▼
0 => "Plastic and cosmetic surgery"
1 => 90
]
2 => array:2 [▼
0 => "Hair transplant"
1 => 92
]
3 => array:2 [▼
0 => "Bariatrics and weight loss surgery"
1 => 117
]
4 => array:2 [▼
0 => "Dermatology surgery"
1 => 121
]
5 => array:2 [▼
0 => "Infertility"
1 => 134
]
6 => array:2 [▼
0 => "Gynecology surgery"
1 => 191
]
]
You can check more functions like this in the documentation. I hope you will enjoy this solution.

Try this to sorting value by number.
Input
$array = [
["Plastic and cosmetic surgery",90],
["Dermatology surgery",121],
["Infertility",134],
["Gynecology surgery",191],
["Hair transplant",92],
["Bariatrics and weight loss surgery",117],
["Dentistry",88]
];
usort($array, 'sortByNumber');
function sortByNumber($a, $b)
{
$a = $a[1];
$b = $b[1];
if ($a == $b) return 0;
return ($a < $b) ? -1 : 1;
}
echo "<pre>";
print_r($array);
die;
Output
Array
(
[0] => Array
(
[0] => Dentistry
[1] => 88
)
[1] => Array
(
[0] => Plastic and cosmetic surgery
[1] => 90
)
[2] => Array
(
[0] => Hair transplant
[1] => 92
)
[3] => Array
(
[0] => Bariatrics and weight loss surgery
[1] => 117
)
[4] => Array
(
[0] => Dermatology surgery
[1] => 121
)
[5] => Array
(
[0] => Infertility
[1] => 134
)
[6] => Array
(
[0] => Gynecology surgery
[1] => 191
)
)

Related

How to find duplicate value in sample array

How to find duplicate value in sample array using php
Array
(
[id] => 644
[qty] => 1
[product] => 127
[super_attribute] => Array
(
[140] => 16
)
)
Array
(
[id] => 648
[qty] => 1
[product] => 111
[super_attribute] => Array
(
[140] => 18
)
)
Array
(
[id] => 652
[qty] => 1
[product] => 111
[super_attribute] => Array
(
[140] => 18
)
)
in this above array i want to find duplicate [product] => 111 and [140] => 18 . How can i achieve this?
One possible way to do this is to combine the two elements you're looking for into one longer key, then create a two-dimensional array listing all the IDs that have the new key.
In the code below I have multiplied the product number by 1000, added the value of super_attribute[140] and cast the result to a string. There are other ways you might obtain a new key depending on what you know about the data you're working with.
<?php
$arr = [
[
"id" => 644,
"qty" => 1,
"product" => 127,
"super_attribute" => [
"140" => 16
]
],
[
"id" => 648,
"qty" => 1,
"product" => 111,
"super_attribute" => [
"140" => 18
]
],
[
"id" => 652,
"qty" => 1,
"product" => 111,
"super_attribute" => [
"140" => 18
]
]
];
$dup = [];
foreach($arr as $subArr) {
// Calculate the new combined key
$newKey = (string)($subArr['product']*10000+$subArr["super_attribute"][140]);
// If we don't have this new key, create an array with the ID,
// otherwise, add the ID to the existing array for the new key.
if (isset($dup[$newKey])) {
$dup[$newKey][] = $subArr['id'];
} else {
$dup[$newKey] = [$subArr['id']];
}
}
var_dump($dup);
Output:
array (size=2)
1270016 =>
array (size=1)
0 => int 644
1110018 =>
array (size=2) //IDs with the combined key 111 and 18
0 => int 648
1 => int 652

PHP- years and months array keys and inside values sort

Here is an php array representing years and months:
array:3 [
2017 => array:2 [
0 => "2"
1 => "3"
]
2018 => array:2 [
0 => "1"
1 => "5"
]
2019 => array:3 [
0 => "10"
1 => "12"
2 => "6"
]
]
I want to sort it on the basis of key (descending) on first level and values (descending) on the second level. by this records of latest month in latest year will appear. So the output must be:
array:3 [
2019 => array:3 [
0 => "12"
1 => "10"
2 => "6"
]
2018 => array:2 [
0 => "5"
1 => "1"
]
2017 => array:2 [
0 => "3"
1 => "2"
]
]
This is just a question of applying krsort to the top-level of the array and rsort to each sub-level:
krsort($array);
array_walk($array, function (&$v) { rsort($v); });
Output:
Array
(
[2019] => Array
(
[0] => 12
[1] => 10
[2] => 6
)
[2018] => Array
(
[0] => 5
[1] => 1
)
[2017] => Array
(
[0] => 3
[1] => 2
)
)
Demo on 3v4l.org

Comparing two associative arrays values and replacing them

I'm facing issue while dealing with 2 associative arrays. I have two arrays, If id of Array 1 = id of Array 2 = id, then active_lession and active_learning_lession of Array 1 should be replaced by active_lession and active_learning_lession of Array 2 respectively.
Array 1 =>
array:3 [
0 => array:10 [
"id" => 3
"status" => "1"
"active_lession" => 0
"active_learning_lession" => 0
"learninglessions" => array:2 [
0 => array:12 [
"id" => 2
"language_id" => 1
"category_id" => 3
"sentence" => "अगर आपको अपना स्कूल का नाम पुछा जाए तो क्या कहेंगे"
"english_sentence" => "I am student of …… School."
]
1 => array:12 [
"id" => 27
"language_id" => 1
"category_id" => 3
"sentence" => "यह मेरा दोस्त/मित्र है"
"english_sentence" => "He is my friend."
]
]
]
1 => array:10 [
"id" => 4
"name" => "Module 2"
"image" => "public/icon/downloadxxx.jpeg"
"status" => "1"
"active_lession" => 10
"active_learning_lession" => 0
"learninglessions" => array:2 [
0 => array:12 [
"id" => 1
"language_id" => 1
"category_id" => 4
"sentence" => "अपना परिचय कैसे देंगे –"
"english_sentence" => "I am..."
]
]
]
2 => array:10 [
"id" => 5
"status" => "1"
"active_lession" => 0
"active_learning_lession" => 0
"learninglessions" => array:4 [
0 => array:12 [
"id" => 29
"language_id" => 1
"category_id" => 5
"sentence" => "यह एक बाग है ।"
"english_sentence" => "This is a Park."
]
1 => array:12 [
"id" => 34
"language_id" => 1
"category_id" => 5
"sentence" => "कैसा चल रहा है ?"
"english_sentence" => "How are things ?"
]
]
]
Array 2=>
array:3 [
0 => array:3 [
"id" => 3
"active_learning_lession" => 25
"active_lession" => 20
]
1 => array:3 [
"id" => 4
"active_learning_lession" => 20
"active_lession" => 15
]
]
Thus Expected Array will be
array:3 [
0 => array:10 [
"id" => 3
"status" => "1"
"active_lession" => 20
"active_learning_lession" => 25
"learninglessions" => array:2 [
0 => array:12 [
"id" => 2
"language_id" => 1
"category_id" => 3
"sentence" => "अगर आपको अपना स्कूल का नाम पुछा जाए तो क्या कहेंगे"
"english_sentence" => "I am student of …… School."
]
1 => array:12 [
"id" => 27
"language_id" => 1
"category_id" => 3
"sentence" => "यह मेरा दोस्त/मित्र है"
"english_sentence" => "He is my friend."
]
]
]
1 => array:10 [
"id" => 4
"name" => "Module 2"
"image" => "public/icon/downloadxxx.jpeg"
"status" => "1"
"active_lession" => 15
"active_learning_lession" => 20
"learninglessions" => array:2 [
0 => array:12 [
"id" => 1
"language_id" => 1
"category_id" => 4
"sentence" => "अपना परिचय कैसे देंगे –"
"english_sentence" => "I am..."
]
]
]
2 => array:10 [
"id" => 5
"status" => "1"
"active_lession" => 0
"active_learning_lession" => 0
"learninglessions" => array:4 [
0 => array:12 [
"id" => 29
"language_id" => 1
"category_id" => 5
"sentence" => "यह एक बाग है ।"
"english_sentence" => "This is a Park."
]
1 => array:12 [
"id" => 34
"language_id" => 1
"category_id" => 5
"sentence" => "कैसा चल रहा है ?"
"english_sentence" => "How are things ?"
]
]
]
Try this code..
$res = [];
foreach($x as $key => $xx)
{
foreach($y as $k => $yy)
{
if($xx['id'] == $yy['id'])
{
$res[$key] = $xx;
$res[$key]['active_lession'] = $yy['active_lession'];
$res[$key]['active_learning_lession'] = $yy['active_learning_lession'];
}
else
{
if(!array_key_exists($key,$res))
{
$res[$key] = $xx;
}
}
}
}
print_r($res);
OR
foreach($x as $key => $value)
{
foreach($y as $yy)
{
if($value['id'] == $yy['id'])
{
$x[$key]['active_learning_lession'] = $yy['active_learning_lession'];
$x[$key]['active_lession'] = $yy['active_lession'];
}
}
}
print_r($x);
Output will be
Array
(
[0] => Array
(
[id] => 3
[status] => 1
[active_lession] => 20
[active_learning_lession] => 25
[learninglessions] => Array
(
[0] => Array
(
[id] => 2
[language_id] => 1
[category_id] => 3
[sentence] => jhdbfhbs
[english_sentence] => I am student of …… School.
)
[1] => Array
(
[id] => 27
[language_id] => 1
[category_id] => 3
[sentence] => dbshbfjhf
[english_sentence] => He is my friend.
)
)
)
[1] => Array
(
[id] => 4
[name] => Module 2
[image] => public/icon/downloadxxx.jpeg
[status] => 1
[active_lession] => 15
[active_learning_lession] => 20
[learninglessions] => Array
(
[0] => Array
(
[id] => 1
[language_id] => 1
[category_id] => 4
[sentence] => jhbdsfhjferu
[english_sentence] => I am...
)
)
)
[2] => Array
(
[id] => 5
[status] => 1
[active_lession] => 0
[active_learning_lession] => 0
[learninglessions] => Array
(
[0] => Array
(
[id] => 29
[language_id] => 1
[category_id] => 5
[sentence] => jbfhgbdu
[english_sentence] => This is a Park.
)
[1] => Array
(
[id] => 34
[language_id] => 1
[category_id] => 5
[sentence] => jhsbdhjfbuyefr
[english_sentence] => How are things ?
)
)
)
)

how to sort array data in alphabetic key order in php

my collection of data in array which is shown below the index key is A,B,C but i want to store these key in "key" and its key letter's words in "dishes" key
array:3 [
"A" => array:4 [
0 => 37
1 => "Algerian"
2 => 6
3 => "American"
]
"B" => array:6 [
0 => 27
1 => "Belgian"
2 => 20
3 => "Brazilian"
]
and so on..
i wanna sort this array like aplhabetic order as shown below
array:10 [
0 => array:2 [
"key" => "A"
"dishes" => array:2 [
0=>array:2[
"id" => 37
"type" => "Algerian"
],
1=>array:2[
"id" => 6
"type" => "American"
]
]
]
1 => array:2 [
"key" => "B"
"dishes" => array:2 [
0=>array:2[
"id" => 27
"type" => "Belgian"
],
1=>array:2[
"id" => 20
"type" => "Brazilian"
]
]
]
and so on...
This would be a possible solution:
<?php
$input = [
'A' => [
0 => 37,
1 => "Algerian",
2 => 6,
3 => "American"
],
'B' => [
0 => 27,
1 => "Belgian",
2 => 20,
3 => "Brazilian"
]
];
$output = [];
array_walk($input, function($values, $key) use (&$output) {
$entry = [
'key' => $key,
'dishes' => []
];
foreach(array_chunk($values, 2) as $chunk) {
$entry['dishes'][] = [
'id' => $chunk[0],
'type' => $chunk[1]
];
}
$output[] = $entry;
});
print_r($output);
The output of above code obviously is:
Array
(
[0] => Array
(
[key] => A
[dishes] => Array
(
[0] => Array
(
[id] => 37
[type] => Algerian
)
[1] => Array
(
[id] => 6
[type] => American
)
)
)
[1] => Array
(
[key] => B
[dishes] => Array
(
[0] => Array
(
[id] => 27
[type] => Belgian
)
[1] => Array
(
[id] => 20
[type] => Brazilian
)
)
)
)
You have to loop through the original array to create your new structure. Then you can use the ksort function to sort them.
$newArr = new array();
for ($arr as $elem) {
$dishArr = new array();
for($elem['dishes'] as $dish) {
$dishArr[] = $dish['id'];
$dishArr[] = $dish['type'];
}
$newArr[$elem['key']] = $dishArr;
}
ksort($newArr);

How do I split up an array and set key names

I have an array of data like this that is passed to my controller via a form. It's being collected in a javascript function. It will always be email the name but there could be 2 sets or 100.
array:10 [▼
0 => "ken#email.com"
1 => "Ken"
2 => "robert#email.com"
3 => "Robert"
4 => "robert#email.com"
5 => "Robert"
6 => "mike#email.com"
7 => "Mike"
]
I'm currently doing this
$recipients = array_chunk($recipients, 2);
array:5 [▼
0 => array:2 [▼
0 => "ken#email.com"
1 => "Ken"
1 => array:2 [▼
0 => "robert#email.com"
1 => "Robert"
2 => array:2 [▼
0 => "robert#email.com"
1 => "Robert"
3 => array:2 [▼
0 => "mike#email.com"
1 => "Mike"
]
What I need though is this...
array:5 [▼
0 => array:2 [▼
email => "ken#email.com"
name => "Ken"
1 => array:2 [▼
email => "robert#email.com"
name => "Robert"
2 => array:2 [▼
email => "robert#email.com"
name => "Robert"
3 => array:2 [▼
email => "mike#email.com"
name => "Mike"
]
How? Thanks!
You can re-add the value back to the array with a key and remove its duplicate by the index.
$recipients = array(
0 => "ken#email.com",
1 => "Ken",
2 => "robert#email.com",
3 => "Robert",
4 => "robert#email.com",
5 => "Robert",
6 => "mike#email.com",
7 => "Mike"
);
$recipients = array_chunk($recipients, 2);
for ($i=0; $i < count($recipients); $i++)
{
$recipients[$i]['email'] = $recipients[$i][0];
$recipients[$i]['name'] = $recipients[$i][1];
unset($recipients[$i][0]);
unset($recipients[$i][1]);
}
It would result in the following output:
Array
(
[0] => Array
(
[email] => ken#email.com
[name] => Ken
)
[1] => Array
(
[email] => robert#email.com
[name] => Robert
)
[2] => Array
(
[email] => robert#email.com
[name] => Robert
)
[3] => Array
(
[email] => mike#email.com
[name] => Mike
)
)
I hope it helped you.
Yes you can do that.
<?php
$array = array(
"foo" => "bar",
"bar" => "foo",
100 => -100,
-100 => 100,
);
var_dump($array);
?>
add to an array with custom key
<?php
$array= array();
$array[]='name=>bob';
var_dump($array);
Array docs

Categories