I'm new here in.
I have a quick question:
I have an array that looks like so -
$array = array(413, 913, 5);
foreach($array as $arr){
echo $arr . '<br />';
}
And the output is -
413
913
5
How can I use the usort() function to make the script output -
913
5
413
If you could help me that would be great! Thank you.
If you just want to sort reversed by digits from left to right, ie. as though they are strings
Just use a normal sort as string values:
$array = array(413, 913, 5);
rsort($array, SORT_STRING);
var_dump($array);
Related
I'm trying to sort numbers on given command line argument as sortnumb$ php phpsort.php 2 5 3 8and it should print as 2 3 5 8. I have tried following code but I don't know how to save given arguments in an array to use "sort" command in PHP. please advice
$argv[1]
$numbers = array($argv[1]);
sort($numbers);
$arrlength=count($numbers);
for($x=0;$x<$arrlength;$x++)
{
echo $numbers[$x];
echo "\n";
}
$argv contains the array of numbers that you are passing, but $argv[0] contains the script name. You can read about here in the PHP manual.
So you can take out the array items you are interested in, using array_slice:
$numbers = array_slice($argv, 1);
The second parameter says to slice from the second item of the index.
Then the rest of the code is correct as you had it.
Although you can use a foreach loop instead (read about it here):
sort($numbers);
foreach ($numbers as $number) {
echo $number."\n";
}
the argument already saved at $numbers array
you can use $numbers variable to do whatever you want
You can make array using the ' ' (space delimiter) and then use any of the sorting function on that array
I have two array like this :
$array1 = array(1,1,2,3,3,4,5); //remember that i have two '1' value in this array
$array2 = array($url1, $url2, $url3, $url4, $url5, $url6);
I wish to find the lowest/highest value in $array1 then link to $url1/$url5 like this :
1 or 5
How I can make this happen using PHP? Any help would be greatly appreciated
Thanks
Use the max() & min() function
max — Find highest value
min — Find lowest value
Example code:
$max = max($array);
$min = min($array);
If the array is already sorted, use $array2[0] and $array2[count($array2)-1].
If it's not already sorted, you can use this to sort the arrays.
array_multisort($array1, SORT_NUMERIC, $array2);
$lowest = $array2[0];
$highest = $array2[count($array2)-1];
As far as I understand you need this 2 functions: max, min
try this
$maxValueKeys = array_keys($array1, max($array1)); // Your min value indexes
$minValueKeys = array_keys($array1, min($array1)); // Your max value indexes
But it isn't an associative array
I am using str_split() to split a long strings into an array of length 16 each. And I'm assigning the returned array to one in my function. Like this:
$myarray = str_split($string, 16);
The problem is that I want the indexing of $myarray to start from a number other than 0, say 50. Currently I'm doing this:
foreach($myarray as $id => $value)
{
$myarray[$id + 50] = $value;
unset($myarray[$id]);
}
Is there a better solution? Because the arrays and strings I'm dealing with are very long. Thanks
You can use array_pad().
$myarray = str_split($string, 16);
$myarray = array_pad($myarray, -(size($myarray)+50), null);
It will fill the first 50 elements with nulls and push the rest of the array forward by 50 elements.
I'm using glob to array my sub directories
<?php
$items = glob("../albums/*", GLOB_ONLYDIR);
foreach($items as $item) {
echo "$item\n ";
}
?>
he orders them by the alphabet, i'd like to order them by numbers
if i got sub-directories named 1 , 3 , 5 , 10
the will be arranged like this : 10, 1, 3, 5
i want them to be like this 1, 3, 5, 10
is there an option? thanks
EDIT:
now i found natsort($files);
and when im using it with:
$items = array_slice(glob('albums/*', GLOB_ONLYDIR), 0, 4);
natsort($items);
and my folders are 995-1000
it gives me this array : 995, 996, 997, 1000
Take a look at natsort()
use natsort to sort the array in natural order:
natsort($array);
You could sort the array after the glob:
sort($items, SORT_NUMERIC);
You can achieve this by using array sorting natsort() like this;
$items = glob("../albums/*", GLOB_ONLYDIR);
natsort($items);
Now if print this array then you get the proper result by this.
print_r($items);
I want to create an array of numbers: 10, 9,8...to 1. But when I echo $numbers, I get "Array" as the output as to the numbers.
There is probably a simple thing I missed, can you please tell me. thanks!
$numbers=array();
for ($i=10; $i>0; $i--){
array_push($numbers, $i);
}
echo $numbers;
To output a string:
echo implode(', ', $numbers);
for debugging purposes use print_r or var_dump.
No you are not missing anything. Of course $numbers is an array.
If you do print_r($numbers) than you see what elements are in the array.
It very much depends what you want to do with array in the end. You can for example also loop over the array values:
foreach($numbers as $number) {
//whatever you want to do
echo $number;
}
If you only want to print theses 10 numbers you can also just do:
for ($i=10; $i>0; $i--){
echo $i;
}
As I said it depends on what you want to do :)
Depends on what you want the output to look like.
For debugging purposes, this should work:
print_r($numbers);
For a "prettier" output:
foreach ($numbers as $key => $value)
echo $key . "=" . $value
First of all, you can create that array much easier using range()
$numbers = range( 10, 1 );
Secondly, only scalars can be echoed directly - complex values like objects and arrays are casted to strings prior to this. The string cast of an array is always "Array".
If you want to see the values, you have to flatten the array somehow, to which you have several options - implode() might be what you're looking for.
echo implode( ',', $numbers );
For debugging purposes you might like:
echo '<pre>' . print_r($numbers, true) . '</pre>';
As it's output is clear without having to look at page source.