我的问题太扭曲了,我甚至不知道如何开始解释它。
让我们说我有几个assosiative数组(并不总是相同的数组:有时我有产品数组,有时我有市场数组,有时我有段数组等...)。 $ values是我经常得到的唯一数组!
$values = array ("0" => "1", "4" => "2", "5" => "3");
$products = array ("0" => "1", "1" => "1", "2" => "2", "3" => "1", "4" => "2", "5" => "3");
$markets = array ("0" => "1", "3" => "1", "4" => "2", "5" => "3");
...
我想用我得到的每个数组的值构建一个数组,其值与键匹配。 像
这样的东西$myArray = array ("0" => array ( "values" => "1", "products" => "1", "markets" => "1"),
"1" => array ( "products" => "1"),
"2" => array ( "products" => "2"),
"3" => array ( "products" => "1", "markets" => "1"),
"4" => array ( "values" => "2", "products" => "2", "markets" => 2),
...);
我尝试过这样的事情:
switch ($_POST["cpv_type"]) {
case "pClass":
$keyValue = $_POST["cpv_type"];
$objKey = "this->productClasses";
break;
case "pMarket":
$keyValue = $_POST["cpv_type"];
$objKey = "this->markets";
break;
case "pSegment":
$keyValue = $_POST["cpv_type"];
$objKey = "this->productSegments";
break;
case "pType":
$keyValue = $_POST["cpv_type"];
$objKey = "this->productTypes";
break;
default:
$keyValue = "products";
$objKey = "this->products";
break;
}
然后我做了一个预告:
// all values must be floats
if(!empty($this->value)){
foreach ($this->value as $key => &$curVal){
// if no value has been entered, exclude it and also associated product from validation
if (strlen(trim($curVal)) == 0) {
unset($this->value[$key]);
unset($this->products[$key]);
} else {
// This validates my variable
$curVal = TMS::checkVar($curVal, "dec", $_SESSION["dico"]->_VALUE_, 100, false);
// Store the value on existing array, associating "hoppValue" to the right key entry!
$logDetail[$keyValue][${$objKey}[$key]]["hoppValue"] = $curVal;
}
}
}
我的问题在于变量变量:
如何使用变量语法访问$this->productTypes[5]
?
我为$$objKey
,${$objKey}
,${$objKey}[$key]
,${$objKey[$key]}
,$$objKey[$key]
感谢您的帮助!
答案 0 :(得分:1)
您可以简单地获取数组$objKey = $this->productClasses
并用作$objKey[$key]
。您可以将$objKey
替换为$arrayClasses
或类似代码以获得良好的下载代码。
P.S。对不起我的英文。