PHP多维数组问题



我正在尝试构建一个多维数组。

public function saveRateTemplateData($RateTemplateInfo)
{
    $RateTemplateID = $RateTemplateInfo['id'];
    $Zones = $RateTemplateInfo['premium_zones'];
    //$ZoneZipCodeIDs[] = array();
    for ($n = 1; $n <= $RateTemplateInfo['premium_zones']; $n++) {
        $ZoneNum = 'zone' . $n;
        $ZipCodeArray = explode(",",$_POST[$ZoneNum]);
        $ZipCodeIDs=array();
        foreach ($ZipCodeArray as $v) {
            $v = intval(trim($v));
            if (strlen($v) == 5) {
                array_push($ZipCodeIDs, $this->addZipCode($v));  
            } else {
                echo "it isnt 5";
            }
        }
    }
}

所以我要做的是创建一个数组的数组。这就是它的样子

Array
(
  [1] => Array
    (
        [0] => 34
        [1] => 31
        [2] => 23
    )
  [2] => Array
    (
        [0] => 18
        [1] => 4
        [2] => 35
        [3] => 1
    )
)

我已经尝试了很多方法,但它不起作用基本上我想在这个格式VarName[ZoneNumbers][ZipCodeID]

以便稍后可以循环它。所以我可以像这样打印$VarName[$n]那么就会打印出区域1的所有zipcodeID数组在本例中它会打印出34 31 23

public function saveRateTemplateData($RateTemplateInfo)
{
    $RateTemplateID = $RateTemplateInfo['id'];
    $zones = array(); // you weren't using this so I'll use it to hold the data
    for ($n = 1; $n <= $RateTemplateInfo['premium_zones']; $n++) {
        $ZoneNum = 'zone' . $n;
        // create an array under the zone number for holding the IDs
        $zones[$n] = array();
        $ZipCodeArray = explode(",",$_POST[$ZoneNum]);
        foreach ($ZipCodeArray as $v) {
            $v = (int) trim($v);
            if (strlen($v) == 5) {
                $zones[$n][] = $this->addZipCode($v);
            } else {
                // use exceptions for exceptional circumstances
                throw new RuntimeException(sprintf('Invalid zone ID "%s"', $v));
            }
        }
    }
    return $zones;
}

相关内容

  • 没有找到相关文章

最新更新