显示阵列中的每个项目,而不使用环 [英] display each item in the array without using a loop
问题描述
您好,我有这个特定的PHP函数。但我无法理解了它如何输出这一项。这就是问题//给定的阵列,显示阵列中的每个项目,而无需使用一个循环。 (不要用内置的功能来做到这一点,像PHP的print_r的;使用递归函数来实现)
这就是code
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)
{
}
?>
可以在这个别人分享的想法?任何帮助是长久AP preciated。
can someone share ideas on this? Any help is muchly appreciated.
推荐答案
您可以使用破灭
$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
OUTPUT : abc 5 xyz
功能:
function print_array($your_array)
{
$text = implode(" ", $your_array); // implode with space you can use any other on this place
echo $text;
}
Recusive方法:
,但只有当用于该阵列的关键是数字从0开始,1,2 ....
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)
}
}
这篇关于显示阵列中的每个项目,而不使用环的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!