【问题标题】:How to sort a string by length of its words in php without using functions?如何在不使用函数的情况下按 php 中单词的长度对字符串进行排序?
【发布时间】:2015-06-30 12:19:29
【问题描述】:

我在一次采访中被问到一个问题,在不使用内置函数的情况下,在 php 中按单词的长度对字符串进行排序。不知道该怎么做。有人可以帮我解决这个问题吗?

字符串:按单词的长度对字符串进行排序

提前致谢

【问题讨论】:

  • 为什么这个问题被否决了?如果您能提出逻辑,请帮助我,否则这是一个真正的问题,我在任何地方都找不到它
  • 这是一个有点模糊的问题。如果禁止所有“内置函数”,那不也排除strlen()吗?它真的只是一个包含单词的字符串,还是一个字符串数组?
  • 用php按长度排序却不能用php??

标签: php logic


【解决方案1】:
 $array = array('harish', 'mohan', 'jaideep', 'hari');

    for ($i = 1; $i < count($array); $i++) {
        for ($j = $i; $j > 0; $j--) {
            if (strlen($array[$j]) < strlen($array[$j - 1])) {

                $tmp = $array[$j];
                $array[$j] = $array[$j - 1];
                $array[$j - 1] = $tmp;
            }
        }
    }

    var_dump($array);

【讨论】:

  • 我想你错过了那部分:没有使用内置函数
  • OP 并没有真正要求代码,而是一个基本的方法描述。
【解决方案2】:

你可以试试这个:

$string = "this is my test string";
$array = explode(" ", $string);
$result = array();
foreach ($array as $key => $string){
    $counter = 0;
    for($i = 0;;$i++){
        if (!isset($string[$i])){
            break;
        }
        $counter++;
    }
    $result[$counter][] = $string;
}

这会拆分您的字符串并将其放入一个数组中,其中的键是计数的字符。现在的问题是,您需要按键对数组进行排序,可以使用ksort 获取。 不知道你能不能用,如果不能,参考this answer (use of sort)或者this answer (no sort),这样应该可以解决问题(虽然我没测试过)。

【讨论】:

  • 没有使用内置函数
  • 任务是按长度对单词进行排序,所以我猜内置函数是指排序和数组函数。上面的例子中只使用了explode和isset,应该没问题。
【解决方案3】:

这是我提出的解决方案。我添加了一些 cmets。

<?php

/* First part split the string in their words and store them in an array called $words. 
* The key is the length of the word and the value is an array with all the words having the same length as the key.
* e.g
* Array
(
    [4] => Array( [0] => Sort )
    [1] => Array( [0] => a )
    [6] => Array( [0] => string, [1] => length)
    [2] => Array( [0] => by, [1] => of)
    [3] => Array( [0] => its )
)
*/
$str = "Sort a string by length of its words";

$word = '';
$i = 0;
$word_length = 0;
$words = [];
$max = -1;

while( isset($str[$i]) ) {  
    if( $str[$i] !== ' ' ){
        $word .= $str[$i];
        $word_length++;     
    }else{
        //This is going to save the size of the longhest word in the array:     
        $max = ($word_length > $max) ? $word_length : $max; 
        //store the
        $words[$word_length][] = $word;                     
        $word = '';
        $word_length = 0;
    }
    $i++;
}

//print_r($words); // uncomment this if you wanna see content of the array.


//The if-condition is for ascending order or descending order.
$order = "DESC"; // "ASC" | "DESC"
$res = '';
if( $order === "DESC") {
    for( $i = $max; $i>=0 ; $i--) {
        if( ! isset($words[$i]) ) { continue; } 
        foreach($words[$i] as $word){
            $res .= $word . ' ';
        }               
    }
}else {
    //ascending order:
    for( $i = 0; $i<=$max; $i++) {
        if( ! isset($words[$i]) ) { continue; } 
        foreach($words[$i] as $word){
            $res .= $word . ' ';
        }               
    }
}

echo $res . "\n";
?>

这是你想要的吗?

注意: isset、echo、print 等是 PHP 语言结构,而 print_r()、strlen() 等是内置函数。如果你有疑问,你可以看看这篇文章有什么不同。 What is the difference between a language construct and a "built-in" function in PHP?

【讨论】:

  • 是的,这正是我想要的 :) 感谢您的帮助。
猜你喜欢
  • 1970-01-01
  • 1970-01-01
  • 2020-03-07
  • 2018-03-24
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
相关资源
最近更新 更多