PHP多维数组问题

发布于 2024-11-30 07:00:11 字数 1132 浏览 0 评论 0原文

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

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

iam trying to build a multidimensional array.

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";
            }
        }
    }
}

so what iam trying to do is make an array of an array. so this is how its supposed to look

Array
(
  [1] => Array
    (
        [0] => 34
        [1] => 31
        [2] => 23
    )

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

i have tried numerous ways it doesnt work
basically i want it in this format VarName[ZoneNumbers][ZipCodeID]

so i can loop through it later on. so i can print like this $VarName[$n] then a array of all zipcodeID will print for Zone Number 1 in this case it will print 34,31,23

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

缪败 2024-12-07 07:00:11
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;
}
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;
}
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文