I want to sort elements of an array which have string values (word) according to word length.
I am performing insertion sort:
$str="welcome to php";
$st=explode(" ",$str);
$a=count($st);
for($i=0;$i<$a;$i++)
{
for($j=0;$j<$a;$j++)
{
if(strlen($st[$j])<strlen($st[$j+1]))
{$t=$st[$j];
$st[$j]=$st[$j+1];
$st[$j+1]=$t;}
}}
So the problem is $st[$j+1]. It doesn't get next value of array. It gives undefined offset. How can I get next value of array?
Somthing like this should work for you:
<?php
function lengthSort($a, $b){
return strlen($b) - strlen($a);
}
$str = "welcome to php";
$st = explode(" ", $str);
usort($st,'lengthSort');
var_dump($st);
?>
Output:
array(3) {
[0]=>
string(7) "welcome"
[1]=>
string(3) "php"
[2]=>
string(2) "to"
}
Also as Nick J suggested take a look at the foreach loop! It's very powerful!
You can define your own comparison function using php's usort function.
See http://php.net/manual/en/function.usort.php
So all you have to do is write your own function that compares strings by their length, then call usort and pass in your function.
Related
I am working with PHP,I have array and i want to change position of array, i want to display matching value in first position,For example i have following array
$cars=('Toyota','Volvo','BMW');
And i have variable $car="BMW" And i want to match this variable with array and if match then this array value should be at first in array
so expected result is (matching record at first position)
$cars=('BMW','Volvo','Toyota');
How can i do this ?
You can use array_search and array_replace for this purpose. try below mentioned code
$cars=array(0 =>'Toyota',1 =>'Volvo',2 =>'BMW');
$car="BMW";
$resultIndex = array_search($car, $cars); //get index
if($resultIndex)
{
$replacement = array(0 =>$car,array_search($car, $cars)=>$cars[0]); //swap with 0 index
$cars = array_replace($cars, $replacement); //replace
}
print_r($cars);
This can be solved in one line with array_merge and array_unique array functions.
$cars=['Toyota','Volvo','BMW'];
$car="BMW";
$cars2 = array_unique(array_merge([$car],$cars));
//$cars2: array(3) { [0]=> string(3) "BMW" [1]=> string(6) "Toyota" [2]=> string(5) "Volvo" }
$car is always at the beginning of the new array due to array_merge. If $car already exists in the $cars array, array_unique will remove it. If $car is not present in the $cars array, it is added at the beginning. If this behavior is not desired, you can use in_array to test whether $car is also contained in the $cars array.
The simplest is to "sort" by "Value = BMW", "Value != BMW".
The function that sorts and resets the keys (i.e. starts the resulting array from 0, which you want) is usort (https://www.php.net/manual/en/function.usort.php)
So your comparison function will be If ($a == "BMW") return 1, elseif ($b == "BMW") return -1, else return 0; (Paraphrased, don't expect that to work exactly - need to leave a bit for you to do!)
I want to create a function to print a list of 50 random floats from 0 to 1.
The function to print one random float is simply :
function random_from_0_to_1()
{
return (float)rand() / (float)getrandmax();
}
But how do I get a list of 50 numbers in descending order?
I want to use usort() function, but I am not sure how to use it with a list of 50 random floats.
Generate a array of floats, sort with sort(), then reverse the array to give descending order.
So, using your function:
<?php
function random_from_0_to_1()
{
return (float)rand() / (float)getrandmax();
}
$arr = [];
for ($i=0;$i<50;$i++) {
$arr[] = random_from_0_to_1();
}
sort($arr); // sorts ascending
$arr = array_reverse($arr);
var_dump($arr);
Output:
array(50) {
[0]=>
float(0.9991139778863238)
[1]=>
float(0.9733540797482031)
[2]=>
float(0.9620095835821748)
[3]=>
float(0.9390542404442347)
[4]=>
float(0.9368096925023989)
[5]=>
float(0.9321818514411253)
[6]=>
float(0.9321091510039331)
...
Demo: https://3v4l.org/NJvGu
[Edit]
Since you've specifically asked for a version with usort(), try this, which substitutes usort() for sort() and array_reverse():
<?php
function random_from_0_to_1()
{
return (float)rand() / (float)getrandmax();
}
$arr = [];
for ($i=0;$i<50;$i++) {
$arr[] = random_from_0_to_1();
}
usort($arr, function($a,$b){return $b<=>$a;}); // Note parameters reversed in spaceship comparison
var_dump($arr);
Demo: https://3v4l.org/qn7Ka
i have a booking system that uses for bookings an array in this form:
array(6) {
[0]=> array(5) {
["date"]=> string(10) "08/12/2016"
["start"]=> string(5) "15:00"
["end"]=> string(5) "16:00"
["booked_by"]=> string(5) "mark"
["id"]=> string(1) "4"
}
I used this function to sort it by date and to get all bookings in an array:
function getBooking($aula) {
global $client;
$keys = $client->keys("Calendar:aula$aula:*");
$booking = array();
foreach ($keys as $key => $value) {
$result = $client->hGetAll($value);
array_push($booking, $result);
}
usort($booking, function($a1, $a2) {
$v1 = strtotime(str_replace("/", "-", $a1['data']));
$v2 = strtotime(str_replace("/", "-", $a2['data']));
return $v1 - $v2; // $v2 - $v1 to reverse direction
});
return $booking;
}
I need to sort it also by "start" key to show them in a calendar. I tried adding another usort but it doesn't work. If you need more info please write me back
Thanks in advance for you help
As #Don't Panic says, if you can do the sorting as part of your database request go for that.
Otherwise, if you are stuck with the array as is - you can extract the data from the date and start strings, put it in the correct order, then use strcmp() when you sort. For example,
usort($booking, function($a, $b) {
// extract year, month and day from date
list($a_month, $a_day, $a_year) = explode('/', $a['date']);
list($b_month, $b_day, $b_year) = explode('/', $b['date']);
// compare the correctly ordered strings
return strcmp($a_year.$a_month.$a_day.$a['start'], $b_year.$b_month.$b_day.$b['start']);
});
Some reference if some of the code is unfamiliar
strcmp() http://php.net/manual/en/function.strcmp.php
list() http://php.net/manual/en/function.list.php
The #Steve solution is great. But you can also use array_multisort and array_column:
array_multisort(
array_map('strtotime', array_column($booking, 'date')),
array_column($booking, 'start'),
$booking
);
Here is working demo.
While in this particular situation it is not optimal, it can be more suitable when you need, for example, to sort by date ascending, but by start descending, or otherwise.
Nevertheless, such operations better to be done inside the database.
EDIT
As #Don't Panic said in the comments, the sorting by date can be tricky because of format. To compensate for this we can array_map 'date' column with strtotime.
I'm trying to use this cookie - current=["1","2","4"] - as a usable array in PHP.
At the moment I can echo these values, but can't use them as an array. How would I convert these values into a usable PHP array?
$currentUsers = $_COOKIE['current'];
echo $currentUsers;
print_r(array_values($currentUsers));
$array = explode(",",$currentUsers);
Var_dump($array);
Echo $array[0]; // 1
Echo $array[1]; // 2
Echo $array[2]; // 4
Edit: Not sure if the " is actually a part of the cookie values?
If it is you could use str_replace('"', '', $currentUsers); to remove the " from the values if you do it before the explode.
Edit2: as Ash pointed out I missed a part on the answer.
Here is the complete code:
$str = substr(str_replace('"', '', $currentUsers),1,-1);
$array = explode(",",$str);
Var_dump($array);
Echo $array[0]; // 1
Echo $array[1]; // 2
Echo $array[2]; // 4
Another solution, if the values are always numbers as in the example:
preg_match_all("/(\d+)/", $currentUsers, $array);
Simple one liner. IF it's always numbers
Working example http://www.phpliveregex.com/p/fuO
Click preg match all button first
Use eval()
$current = [];
eval('$'.$currentUsers);
var_dump($current);
For those with lightweight downvote, the output:
[root#mypc]# php test.php
array(3) {
[0]=>
string(1) "1"
[1]=>
string(1) "2"
[2]=>
string(1) "4"
}
array(2) {
[0]=>
object(stdClass)#144 (2) {
["id"]=>
string(1) "2"
["name"]=>
string(5) "name1"
}
[1]=>
object(stdClass)#145 (2) {
["id"]=>
string(1) "4"
["name"]=>
string(5) "name2"
}
}
I want to add key and value (for example [distance] = 100;) to the objects in the array. After this I want to sort on the distance values. How can I achieve this?
To get structure such as yours you can do:
$arr = array();
$arr[0]->id = "2";
$arr[0]->name = "name1";
$arr[1]->id = "4";
$arr[1]->name = "name2";
To add "distance" you can do:
$arr[0]->distance = 100;
$arr[1]->distance = 200;
To sort you can use decorate/sort/undecorate pattern:
$arr = array_map(create_function('$o', 'return array($o->distance, $o);'), $arr); // transform array of objects into array of arrays consisted of sort key and object
sort($arr); // sort array of arrays
$arr = array_map('end', $arr); // take only last element from each array
Or you can use usort() with custom comparison function like this:
function compareDistanceFields($a, $b) {
return $a->distance - $b->distance;
}
usort($arr, "compareDistanceFields");
$my_array[0]->distance = 100;
$my_array[0]->distance = 101;
usort($my_array, "cmp");
function cmp($a, $b)
{
if ($a->distance == $b->distance)
return 0;
return ($a->distance > $b->distance) ? 1: -1;
}
What you have here is an array of hashes; that is, each element of your array is a hash (a structure containing elements, each of which is identified by a key).
In order to add a key and value, you just assign it, like this:
$array[0]["distance"]=100;
$array[1]["distance"]=300;
#and so on
PHP hashes and arrays in general are documented here.
Now, in order to sort your array (each of whose elements is a hash), you need to use the "uasort" function, which allows you to define a comparison function; in this comparison function you define the behavior you want, which is sorting on the value of the distance key.
Something like this:
// Comparison function, this compares the value of the "distance" key
// for each of the two elements being compared.
function cmp($a, $b) {
if ($a["distance"] == $b["distance"]) {
return 0;
}
return ($a["distance"] < $b["distance"]) ? -1 : 1;
}
Once this is defined, you call uasort like this:
uasort($array, 'cmp');
Find more documentation on uasort here.