dsfds656545 2014-07-31 05:30
浏览 9
已采纳

显示数组中的每个项目而不使用循环[关闭]

Hi i have this certain php function. But i cannot figured it out how to output this one. This is the question // Given an array, display each item in the array without using a loop. (Do not use built in functions to do this, like PHP's print_r; use a recursive function to implement) and this is the code

<?php
  function print_array(array $input)
  {
  }
?>

can someone share ideas on this? Any help is muchly appreciated.

  • 写回答

1条回答 默认 最新

  • douyong5825 2014-07-31 05:32
    关注

    you can use implode

    $your_array = array("abc", "5", "xyz")
    
    $text  = implode(" ", $your_array); // implode with space you can use any other on this place
    
    echo $text;
    

    OUTPUT : abc 5 xyz

    FUNCTION :

    function print_array($your_array)
    {
       $text  = implode(" ", $your_array); // implode with space you can use any other on this place
    
        echo $text;
    }
    

    Recusive Method :

    but only used when the key of the array is numeric starting with 0, 1, 2 ....

    print_array($your_array);
    
    function print_array($your_array, $index=0)
    {   
        if(isset($your_array[$index]))
        {
            echo $your_array[$index]; 
            $index+=1;
            print_array($your_array, $index)
        }   
    }
    
    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论

报告相同问题?

悬赏问题

  • ¥50 我撰写的python爬虫爬不了 要爬的网址有反爬机制
  • ¥15 Centos / PETSc / PETGEM
  • ¥15 centos7.9 IPv6端口telnet和端口监控问题
  • ¥120 计算机网络的新校区组网设计
  • ¥20 完全没有学习过GAN,看了CSDN的一篇文章,里面有代码但是完全不知道如何操作
  • ¥15 使用ue5插件narrative时如何切换关卡也保存叙事任务记录
  • ¥20 海浪数据 南海地区海况数据,波浪数据
  • ¥20 软件测试决策法疑问求解答
  • ¥15 win11 23H2删除推荐的项目,支持注册表等
  • ¥15 matlab 用yalmip搭建模型,cplex求解,线性化处理的方法