简体   繁体   English

PHP重新排列字符串数组

[英]php reorder array of strings

I have an array of strings corresponding to names of images in a directory. 我有一个与目录中图像名称相对应的字符串数组。

Here is an example: 这是一个例子:

    array(3) { [0]=> string(5) "3.png" [1]=> string(5) "2.jpg" [2]=> string(6) "4.jpeg" }

How could I reorder that array so that the numbers before the extensions increases such as the example below: 我如何重新排序该数组,以便扩展名之前的数字增加,例如以下示例:

    array(3) { [0]=> string(5) "2.jpg" [1]=> string(5) "3.png"  [2]=> string(6) "4.jpeg" }

use sort function: 使用sort功能:

$array = array(
    '2.png',
    '4.png',
    '1.png',
);

sort($array);

print_r($array);

Output: 输出:

Array ( [0] => 1.png [1] => 2.png [2] => 4.png )

For more detail have a look at: PHP Array Sorting 有关更多详细信息,请参阅PHP数组排序

Here is the neat function to manipulate the position (index) of any existing element in the array: 这是一个整洁的函数,用于操纵数组中任何现有元素的位置(索引):

$sampleArray = array( 'a', 'b', 'c', 'd', 'e' );
print_r($sampleArray);
print_r(arrayMoveElement('c',$sampleArray,1));
exit;

function arrayMoveElement($element, &$array, $position=0){
    $index = array_search($element, $array);      // Search for the element in the array and returns its current index
    if($index == false){                        // Make sure the element is present in the array
        return false;
    }
    else{
        unset($array[$index]);                      // Removes the element from the array
        $array = array_values($array);                  // Re-sorts the indexes
        if(!is_int($position)){return false;}           // Position of the element that should be inserted must be a valid integer (index)
        array_splice($array, $position, 0, $element);   // Inserts the element to the desired position (index) starting from 0 

        return $array;
    }
}// END function arrayMoveElementFirst($element, &$array){

The output: 输出:

Array ( [0] => a [1] => b [2] => c [3] => d [4] => e ) 数组([0] => a [1] => b [2] => c [3] => d [4] => e)

Array ( [0] => a [1] => c [2] => b [3] => d [4] => e ) 数组([0] => a [1] => c [2] => b [3] => d [4] => e)

Notice that the position parameter is optional and if omitted the function just moves the element to the beginning of the array. 请注意,position参数是可选的,如果省略该函数,则仅将元素移到数组的开头。 Also, it may be negative integer in which case the position (index) of the element is calculated from its end. 同样,它可以是负整数,在这种情况下,从元素的末端开始计算元素的位置(索引)。

There is a validation that makes sure the element exists in the array, and that the new position is provided as integer value. 有一个验证可确保该元素存在于数组中,并确保新位置以整数值形式提供。

See the code comments for more details. 有关更多详细信息,请参见代码注释。

Use sort() or asort(); 使用sort()或asort();

<?php
    $fruits = array("lemon", "orange", "banana", "apple");
    sort($fruits);
    foreach ($fruits as $key => $val) {
        echo "fruits[" . $key . "] = " . $val . "\n";
    }
?>

fruits[0] = apple
fruits[1] = banana
fruits[2] = lemon
fruits[3] = orange

you can find more here: http://php.net/manual/en/array.sorting.php 您可以在此处找到更多信息: http : //php.net/manual/en/array.sorting.php

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM