dongtuan1980 2015-10-04 04:11
浏览 9
已采纳

Php,在php中执行操作,其中一位面试官问我,同样如此

Display the elements which has 5 on the right side(right most).

Input :{55,4,121,3333,65}
Output:{55,65}

2)display all the elements which has 2 digits in the first position and rest as it is.

Input:{122,3333,44,77777,9,13,5555}
Output:{44,13,122,3333,77777,9,5555}.

Please help me out. try to give answer in php.

  • 写回答

1条回答 默认 最新

  • duanqie8549 2015-10-04 04:31
    关注

    Sounds like something fun I would have to do in school. Here's the code fresh off the press!

    Working demo (until I remove it): http://blazerunner44.me/test/fun.php

    Question 1:

    function question1($input){
        $result = array();
        foreach($input as $number){
            $numberString = (string)$number;
            $lastNumber = (int)substr($numberString, -1);
            if($lastNumber == 5){
                array_push($result, $number);
            }
        }
        return $result;
    }
    
    $answer1 = json_encode(question1(array(55,4,121,3333,65)));
    echo($answer1);
    

    Question 2:

    function question2($input){
        $result = array();
        foreach($input as $number){
            $numberString = (string)$number;
            if(strlen($numberString) >= 2){
                array_push($result, $number);
            }
            elseif($numberString[0] == $numberString[1]){
                array_push($result, $number);
            }
        }
        return $result;
    }
    
    $answer2 = json_encode(question2(array(122,3333,44,77777,9,13,5555)));
    echo($answer2);
    

    Since your question wasn't very specific, let me know if you need me to explain how something works.

    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论

报告相同问题?

悬赏问题

  • ¥16 Qphython 用xlrd读取excel报错
  • ¥15 单片机学习顺序问题!!
  • ¥15 ikuai客户端多拨vpn,重启总是有个别重拨不上
  • ¥20 关于#anlogic#sdram#的问题,如何解决?(关键词-performance)
  • ¥15 相敏解调 matlab
  • ¥15 求lingo代码和思路
  • ¥15 公交车和无人机协同运输
  • ¥15 stm32代码移植没反应
  • ¥15 matlab基于pde算法图像修复,为什么只能对示例图像有效
  • ¥100 连续两帧图像高速减法