你好,我想使用 php 代码返回数组,它没有给出任何输出
请看一下代码
<?php
header('Content-Type: text/plain');
$a=array();
function showCombinations($string, $traits, $i)
{
//print_r($i);
if ($i >= count($traits)) {
$a[]=trim($string) . "\n";
return $a;
} else {
foreach ($traits[$i] as $trait) {
//print_r($trait[$i]);
showCombinations("$string $trait", $traits, $i + 1);
}
}
}
$traits = array
(
array('Happy', 'Sad', 'Angry', 'Hopeful'),
array('Outgoing', 'Introverted'),
array('Tall', 'Short', 'Medium'),
array('Handsome', 'Plain', 'Ugly')
);
//print_r($traits);exit;
echo showCombinations(' ', $traits, 0);
?>
读取 PHP 变量范围。使用
$GLOBALS[]
。
function showCombinations($string, $traits, $i)
{ //print_r($i);
if ($i >= count($traits))
{
$GLOBALS["a"][]=trim($string) . "\n";
return $a;
}
else
{
foreach ($traits[$i] as $trait)
//print_r($trait[$i]);
showCombinations("$string $trait", $traits, $i + 1);
}
}
在你的
else
部分,你需要写:
return showCombinations("$string $trait", $traits, $i + 1);
您的代码然后输出:
Array
使用
print_r
来查看数组的内容:
print_r(showCombinations(' ', $traits, 0));
输出以下内容:
Array
(
[0] => Happy Outgoing Tall Handsome
)