我想要一个函数来回显数组中定义的值,在同一个functon的参数中定义的相同数组,如下所示:
function something($somena = "rahul", $somepla = "delhi", $color = "red"){
$somena = array("amit", "rohit", "girish");
$somepla = array("bombay", "cochin", "agra");
$color = array("magenta", "mauve", "green");
return "my name is $somena[2], my place of birth is $somepla[1] and my color is $color[2]";
}
echo "<br/>",something();
echo "<br/>",something($somena[0]);
这里,第一个回声输出正常,但我在第二个回声中有问题,因为它无法识别$ somena [0])。请解释为什么会发生这种情况以及解决方法是什么。你可以看到我在追求的东西:我想继续通过对数组的特定调用来回显函数。所以它可以像:
echo "<br/>",something($somepla[1], $color[2]);
等等。
答案 0 :(得分:0)
有一件事我注意到你正在通过$ somena [0]来运作某些功能。但是这个数组没有定义它在函数范围中的定义。
首先删除这个,或者从函数中定义数组。 我测试了两个echo语句工作正常并提供相同的输出。
答案 1 :(得分:0)
function something($array){
$somena = array("amit", "rohit", "girish");
$somepla = array("bombay", "cochin", "agra");
$color = array("magenta", "mauve", "green");
return ('my name is ' . $somena[$array[0]] . ', my place of birth is ' .
$somepla[$array[1]] . ' and my color is ' . $color[$array[2]]);
}
$data = array(0,1,2);
echo "<br/>",something();
echo "<br/>",something($data);
输出:
我的名字是,我的出生地是,我的颜色是
我的名字是amit,我的出生地是科钦,我的颜色是绿色
function something($name,$place,$color){
$names = array('unknown','amit', 'rohit', 'girish');
$places = array('unknown','bombay', 'cochin', 'agra');
$colors = array('unknown','magenta', 'mauve', 'green');
settype($name,'int'); // if variable not passed, it will be set to zero (unknown)
settype($place,'int'); // or if passed as a string will and
settype($color,'int'); // be set to correct type for array
return ("my name is $names[$name], my place of birth is $places[$place], and my color is $colors[$color]");
}
$n = 1;
$p = 2;
$c = 3;
echo "<br/>",something($name,$place,$color);
echo "<br/>",something($n,$p,$c);
输出:
我的名字不详,我的出生地不详,我的颜色不详
我的名字是amit,我的出生地是cochin,我的颜色是绿色
答案 2 :(得分:0)
这可能很有用(链接到这里 - http://viper-7.com/QTx8mq):
<?php
// $add - if need to add new values to array
function something($somena_ID = 2, $somepla_ID = 1, $color_ID = 2,$add=0){
//Arrays:
$somena = array("amit", "rohit", "girish");
$somepla = array("bombay", "cochin", "agra" );
$color = array("magenta", "mauve", "green" );
//Overwrites:
if (!is_int($somena_ID)){ $somena_ID = (array_push ($somena, $somena_ID) - 1); $somena_ = 1;}else{$somena_ = 0;}
if (!is_int($somepla_ID)){ $somepla_ID = (array_push ($somepla, $somepla_ID) - 1); $somepla_ = 1;}else{$somepla_ = 0;}
if (!is_int($color_ID)){ $color_ID = (array_push ($color, $color_ID) - 1); $color_ = 1;}else{$color_ = 0;}
//return
if ($add){
return "my name is $somena[$somena_ID], my place of birth is $somepla[$somepla_ID] and my color is $color[$color_ID]";
}else{
return "my name is ". ($somena_ ?array_pop($somena) : $somena[$somena_ID]) .",
my place of birth is ".($somepla_ ?array_pop($somepla):$somepla[$somepla_ID])."
and my color is ". ($color_ ?array_pop($color) : $color[$color_ID]) ."";
}
}
echo "<br/>",something();
echo "<br/>",something(0);
echo "<br/>",something(0, "delhi", "red"); // return values and not changed arrays
echo "<br/>",something(0, "delhi", 2); // return values and not changed arrays
echo "<br/>",something("rahul", "delhi", "red",0); // return values and not changed arrays
echo "<br/>",something("rahul", "delhi", "red",1); // return values and add values to arrays
?>
输出:
my name is girish, my place of birth is cochin and my color is green my name is amit, my place of birth is cochin and my color is green my name is amit, my place of birth is delhi and my color is red my name is amit, my place of birth is delhi and my color is green my name is rahul, my place of birth is delhi and my color is red my name is rahul, my place of birth is delhi and my color is red