douti19680318
2017-04-12 11:51
采纳率: 0%
浏览 84
已采纳

按值/键对数组排序 - 混合字符串和数值PHP

I need to sort an array. Both values and keys are the same and are strings. Basically, array looks similar to this:

 $test = array(
      '1' => '1',
      '2' => '2',
      '1.5' => '1.5',
      '3' => '3',
      '4' => '4',
      '3.5' => '3.5',
      '2XS' => '2XS',
      '2XL' => '2XL',
      'XXL' => 'XXL',
    );

Expected Output:

 Array
 (
     [0] => 1
     [1] => 1.5
     [2] => 2
     [5] => 3
     [6] => 3.5
     [7] => 4
     [3] => 2XL
     [4] => 2XS
     [8] => XXL
 )

Any ideas?

图片转代码服务由CSDN问答提供 功能建议

我需要对数组进行排序。 值和键都是相同的并且是字符串。 基本上,数组看起来类似于:

  $ test = array(
'1'=>'1',
'2'=>'2'  ,
'1.5'=>'1.5',
'3'=>'3',
'4'=>'4',
'3.5'=>'3.5',\  n'2XS'=>'2XS',
'2XL'=>'2XL',
'XXL'=>'XXL',
); 
   \  n 
 

预期输出:

 数组
(
 [0] => 1 
 [1] =>  ; 1.5 
 [2] => 2 
 [5] => 3 
 [6] => 3.5 
 [7] => 4 
 [3] => 2XL 
 [  4] => 2XS 
 [8] => XXL 
)
   
 
 

任何想法?

  • 写回答
  • 关注问题
  • 收藏
  • 邀请回答

3条回答 默认 最新

  • dsfo22654 2017-04-12 11:58
    已采纳

    PHP code demo

    <?php 
    $test = array(
          '1' => '1',
          '2' => '2',
          '1.5' => '1.5',
          '3' => '3',
          '4' => '4',
          '3.5' => '3.5',
          '2XS' => '2XS',
          '2XL' => '2XL',
          'XXL' => 'XXL',
        );
    $numericArray=array();
    $stringsArray=array();
    array_map(function($value) use (&$numericArray,&$stringsArray) {
            if(is_numeric($value))
            {
                $numericArray[]=$value;//Here we are putting numeric values
            }
            else
            {
                $stringsArray[]=$value;//Here we are putting string values
            }
    
    }, $test);
    sort($numericArray,SORT_NUMERIC);//sorting on numeric basis
    sort($stringsArray,SORT_REGULAR);//sorting on string basis
    $result=array_merge($numericArray,$stringsArray);//merging both of them
    print_r($result);
    

    Output:

    Array
    (
        [0] => 1
        [1] => 1.5
        [2] => 2
        [3] => 3
        [4] => 3.5
        [5] => 4
        [6] => 2XL
        [7] => 2XS
        [8] => XXL
    )
    
    已采纳该答案
    打赏 评论
  • douzi2333 2017-04-12 11:57
    <?php
    $test = array(
        '1' => '1',
        '2' => '2',
        '1.5' => '1.5',
        '3' => '3',
        '4' => '4',
        '3.5' => '3.5',
        '2XS' => '2XS',
        '2XL' => '2XL',
        'XXL' => 'XXL',
    );
    
    function sortNandS($array){
        $nums = [];
        $strings = [];
    
        foreach($array as $elm) {
            is_numeric($elm) ? $nums[]= $elm : $strings[]=$elm;
        }
        sort($nums,SORT_NUMERIC);
        sort($strings,SORT_REGULAR);
        $result=array_merge($nums,$strings);
    
        return $result;
    }
    
    print_r(sortNandS($test));
    
    打赏 评论
  • duankong8998 2017-04-12 12:10

    The solution using usort, is_numeric and strcmp functions:

    usort($test, function ($a, $b) {
        if (is_numeric($a) && is_numeric($b)) {
            return $a - $b;
        } else if (is_numeric($a) && !is_numeric($b)) {
            return -1;
        } else if (!is_numeric($a) && is_numeric($b)) {
            return 1;
        } else {
            return strcmp($a, $b);
        }
    });
    
    print_r($test);
    

    The output:

    Array
    (
        [0] => 1
        [1] => 1.5
        [2] => 2
        [3] => 3
        [4] => 3.5
        [5] => 4
        [6] => 2XL
        [7] => 2XS
        [8] => XXL
    )
    
    打赏 评论

相关推荐 更多相似问题