Php sort array ascending order - php

I have this array:
Array ( [0] => ../files/flv/1 [1] => ../files/flv/10 [2] => ../files/flv/2 [3] => ../files/flv/3 [4] => ../files/flv/4 [5] => ../files/flv/5 [6] => ../files/flv/6 [7] => ../files/flv/7 [8] => ../files/flv/8 [9] => ../files/flv/9 )
I need to sort it this way:
Array ( [0] => ../files/flv/1 [1] => ../files/flv/2 [2] => ../files/flv/3 [3] => ../files/flv/4 [4] => ../files/flv/5 [5] => ../files/flv/6 [6] => ../files/flv/7 [7] => ../files/flv/8 [8] => ../files/flv/9 [9] => ../files/flv/10 )
I tried to use sort($array,SORT_NUMERIC);, but no luck because of this prefix ../files/flv/
I know only this solution: $array2 = array_map('basename', $array); and then sort($array2,SORT_NUMERIC);
Is there any other solutions not so complex?

Use SORT_NATURAL instead of SORT_NUMERIC (requires PHP 5.4.0 or latest):
sort($array, SORT_NATURAL);
EDIT: I used this code to test it:
$array = array(
'../files/flv/1',
'../files/flv/10',
'../files/flv/2'
);
sort($array, SORT_NATURAL);
print_r($array);
It outputs:
Array
(
[0] => ../files/flv/1
[1] => ../files/flv/2
[2] => ../files/flv/10
)
EDIT 2: Alternatively you can use the natsort() function, it works on older versions too:
natsort($array);

If "../files/flv/" path is always same you can try using str_replace function on all elements and then do sorting using sort numeric and later add same path back to all elements.
So three steps are:
Use foreach loop over array and use str_replace() function on ecah element.
do sorting as you did using sort numeric.
Use foreach loop over array and put the constant path back as prefix.
To make it more perfect you can do this by creating your own function and pass path as parameter.

Have you tried without the SORT_NUMERIC flag?

Related

How to push static value at each index of array to create 2d array?

I am working in php where I need to add a static value at each index of existing single dimensional array so that after adding it will become multidimensional array.
Existing single dimensional array:
[checklists] => Array
(
[0] => 20
[1] => 50
[2] => 35
[3] => 23
[4] => 24
[5] => 21
[6] => 22
[7] => 27
[8] => 25
)
Static value to be inserted 90
After insertion array will look like this:
[checklists] => Array
(
[0] => Array(90,20)
[1] => Array(90,50)
[2] => Array(90,35)
[3] => Array(90,23)
[4] => Array(90,24)
[5] => Array(90,21)
[6] => Array(90,22)
[7] => Array(90,27)
[8] => Array(90,25)
)
I want to know is there any php builtin function through which i can achieve this or should i use loop?
You can use array_map, $static is your 90, $array is your array.
$array['checklists'] = array_map(function($v) use($static){
return [$static, $v];
}, $array['checklists']);
Demo: https://3v4l.org/3ugLR
Here we are using array_map to achieve the desired output.
Solution 1:
Try this code snippet here
$staticValue=90;
$array["checklists"]= array_map(function($value) use ($staticValue){
return array($staticValue,$value);
}, $array["checklists"]);
print_r($array);
Solution 2: try this simplest one
$staticValue=90;
foreach($array as &$value)
{
$value=array($staticValue,$value);
}
print_r($array);
To modify an array, array_walk can be used:
array_walk($array['checklists'], 'addStatic');
function addStatic(&$v) {
$v = [90, $v];
}
More easy way you can do this.
$array = array();
$array['checklist'][] = array(90, 20);
$array['checklist'][] = array(90, 50);

Summary, reorganizing and "flatten" the entire multi-dimensional array

Currently I have an array like :
Array(
[0] => Array([range]=>1-10 [count]=>3 [type]=>A)
[1] => Array([range]=>11-20 [count]=>6 [type]=>A)
[2] => Array([range]=>21-30 [count]=>5 [type]=>A)
[3] => Array([range]=>1-10 [count]=>5 [type]=>B)
[4] => Array([range]=>11-20 [count]=>3 [type]=>B)
[5] => Array([range]=>21-30 [count]=>8 [type]=>B)
[6] => Array([range]=>1-10 [count]=>4 [type]=>C)
[7] => Array([range]=>11-20 [count]=>3 [type]=>C)
[8] => Array([range]=>21-30 [count]=>6 [type]=>C)
[9] => Array([range]=>1-10 [count]=>3 [type]=>D)
[10] => Array([range]=>11-20 [count]=>7 [type]=>D)
And then I am trying to regroup/remake the array depends on their type and the expected output would be like :
Array(
[0] => Array([type]=>A [1-10]=>3 [11-20]=>6 [21-30]=>5)
[1] => Array([type]=>B [1-10]=>5 [11-20]=>3 [21-30]=>8)
[2] => Array([type]=>C [1-10]=>4 [11-20]=>3 [21-30]=>6)
[3] => Array([type]=>D [1-10]=>3 [11-20]=>7)
)
I have tried array_column but isn't what exactly I want...
Example Here.
Thanks in advance.
This should work for you:
Here I simply loop through the entire array and then check with isset() if the result array already has an innerArray with the same type (e.g $result["A"]), if not I add the type as value to the inner array (.e.g. $result["A"]["type"] = "A";).
After this check I simply add the range and count to each type (e.g. $result["A"]["1-10"] = 3;)
At the end I simply reindex the entire $result array with array_values().
<?php
foreach($arr as $k => $v) {
if(!isset($result[$v["type"]]))
$result[$v["type"]]["type"] = $v["type"];
$result[$v["type"]][$v["range"]] = $v["count"];
}
$result = array_values($result);
print_r($result);
?>
output:
Array
(
[0] => Array
(
[type] => A
[1-10] => 3
[11-20] => 6
[21-30] => 5
)
//...
)

Removing nth element from an array and reindex it

I have an array like below
Array
(
[0] => '13-Nov'
[1] => 'PUJA SUNUWAR'
[2] => '13-Nov'
[3] => '...301303'
[4] => 'TT1331600004\DLG'
[5] => '-10000.00'
[6] => '0'
[7] => '90000.00'
)
I need to remove 4th item of array and save it as
Array
(
[0] => '13-Nov'
[1] => 'PUJA SUNUWAR'
[2] => '13-Nov'
[3] => 'TT1331600004\DLG'
[4] => '-10000.00'
[5] => '0'
[6] => '90000.00'
)
i don't want to iterate over each elements of array. Is there any one shot function like array_pop to remove nth element of array?
use array_splice($array, 3, 1);
http://php.net/manual/en/function.array-splice.php
Is this a 2d-array? If so:
No, there is no built in function to do this. You could use ´array_walk´ with a custom callback but I doubt it would be faster than a simple foreach.
Else (if normal array):
unset( $aData[3] );
$aData = array_values( $aData );
Wich is faster then array_splice.
As mentioned above, there is no build in function for that.
You should use unset if you dont need to care about array indexing and array_values if should.
Also you should use value reference while array iterating to prevent internal array copyng while values modification. If dont, it can be one of perfomance break down reasons.
foreach ($yourDataSet as &$value) {
unset($value[2]);
//$value = array_values($value);
}
You shouldt use array_splice becouse of perfomance reasons.
p.s. Also you could check phpbench for perfomance test about different types of array iteration.

sort files in a directory based on partial file name in php

I am using glob() php function to find the files in a directory.
I am getting the following array as result:
Array
(
[0] => D:\xampp\htdocs\myproject\app\webroot\batch\temp\file_2012-08-09-0.jpeg
[1] => D:\xampp\htdocs\myproject\app\webroot\batch\temp\file_2012-08-09-1.jpeg
[2] => D:\xampp\htdocs\myproject\app\webroot\batch\temp\file_2012-08-09-10.jpeg
[3] => D:\xampp\htdocs\myproject\app\webroot\batch\temp\file_2012-08-09-11.jpeg
[4] => D:\xampp\htdocs\myproject\app\webroot\batch\temp\file_2012-08-09-2.jpeg
[5] => D:\xampp\htdocs\myproject\app\webroot\batch\temp\file_2012-08-09-3.jpeg
[6] => D:\xampp\htdocs\myproject\app\webroot\batch\temp\file_2012-08-09-4.jpeg
[7] => D:\xampp\htdocs\myproject\app\webroot\batch\temp\file_2012-08-09-5.jpeg
[8] => D:\xampp\htdocs\myproject\app\webroot\batch\temp\file_2012-08-09-6.jpeg
[9] => D:\xampp\htdocs\myproject\app\webroot\batch\temp\file_2012-08-09-7.jpeg
[10] => D:\xampp\htdocs\myproject\app\webroot\batch\temp\file_2012-08-09-8.jpeg
[11] => D:\xampp\htdocs\myproject\app\webroot\batch\temp\file_2012-08-09-9.jpeg
)
What I want is "To sort the files on the basis of partial text of file names". As in the above array, when you may realize I was supposed to expect the result to be sorted as the ...-0.jpeg, ...-1.jpeg, ...-2.jpeg not ...-0.jpeg, ...-1.jpeg, ...-10.jpeg.
I am expecting the following result:
Array
(
[0] => D:\xampp\htdocs\myproject\app\webroot\batch\temp\file_2012-08-09-0.jpeg
[1] => D:\xampp\htdocs\myproject\app\webroot\batch\temp\file_2012-08-09-1.jpeg
[2] => D:\xampp\htdocs\myproject\app\webroot\batch\temp\file_2012-08-09-2.jpeg
[3] => D:\xampp\htdocs\myproject\app\webroot\batch\temp\file_2012-08-09-3.jpeg
[4] => D:\xampp\htdocs\myproject\app\webroot\batch\temp\file_2012-08-09-4.jpeg
[5] => D:\xampp\htdocs\myproject\app\webroot\batch\temp\file_2012-08-09-6.jpeg
[7] => D:\xampp\htdocs\myproject\app\webroot\batch\temp\file_2012-08-09-7.jpeg
[8] => D:\xampp\htdocs\myproject\app\webroot\batch\temp\file_2012-08-09-8.jpeg
[9] => D:\xampp\htdocs\myproject\app\webroot\batch\temp\file_2012-08-09-9.jpeg
[10] => D:\xampp\htdocs\myproject\app\webroot\batch\temp\file_2012-08-09-10.jpeg
[11] => D:\xampp\htdocs\myproject\app\webroot\batch\temp\file_2012-08-09-11.jpeg
)
Kindly help, how I can do it using PHP with/without regular expression?
Use natsort function that sort an array using a "natural order" algorithm
As you have all strings in an array, simply use php's built-in function sort().
It would work like this:
sort($array);
Since all files are in the same directory, this should solve the incorrect order you have. If it doesn't, you can also try this:
//Get rid of directory
foreach($array as $key=>$value){
$value=str_replace('D:\xampp\htdocs\myproject\app\webroot\batch\temp\\','',$value); //Watch out, the last backslash has to be escaped with another backslash, otherwise the string will not end
}
//Sort only the filenames
$sort($array);
//Prepend directory again
foreach($array as $key=>$value){
$value='D:\xampp\htdocs\myproject\app\webroot\batch\temp\\'.$value;
}

How can I remove data from an array using regexp?

I have this array, $array :
Array
(
[0] => http://download.server.com/18821_SM_139.jpg
[1] => http://download.server.com/18821_SM_134.jpg
[2] => http://download.server.com/18821_SM_138.jpg
[3] => http://download.server.com/18821_SM_138.jpg
[4] => http://download.server.com/18821_ABS_132.jpg
[5] => http://download.server.com/18821_SM_138.jpg
)
and in this case, I am looking for any line that has ABS inside.
I could look for that by using the regexp http://.+ABS.+, and this will select the entire line.
But I still need to remove it from the array, not just replace it (or leave it empty.) But in this case, the array will become:
Array
(
[0] => http://download.server.com/18821_SM_139.jpg
[1] => http://download.server.com/18821_SM_134.jpg
[2] => http://download.server.com/18821_SM_138.jpg
[3] => http://download.server.com/18821_SM_138.jpg
[4] => http://download.server.com/18821_SM_138.jpg
)
Any ideas what method i need to use?
Thanks.
edit:
i am using OOP php
Use array_filter() with a custom callback.
Example:
function testABS($elem) {
return strpos($elem, 'ABS') === false;
}
print_r(array_filter($the_array, 'testABS'));
Note: This is a contrived example. You will need to adjust the logic in the callback function to meet your needs.
There's preg_grep:
$abs = preg_grep('/ABS/', $your_array);
and returns the matches as an array. It also has a flag to return only the non-matching entries, which is probably what you want: return all entries which do NOT have 'abs' in them.
What i understood is u want to remove that element from the array.
can do like this.
$arr = Array
(
[0] => http://download.server.com/18821_SM_139.jpg
[1] => http://download.server.com/18821_SM_134.jpg
[2] => http://download.server.com/18821_SM_138.jpg
[3] => http://download.server.com/18821_SM_138.jpg
[4] => http://download.server.com/18821_ABS_132.jpg
[5] => http://download.server.com/18821_SM_138.jpg
);
$new_arr = array();
foreach($arr as $link){
if(!preg_match('ABS', $link)){
$new_arr[] = $link;
}
}
//ths will give array with only 4 elements as '18821_ABS_132.jpg' will be removed
return $new_arr;

Categories