M. of CA
M. of CA

Reputation: 1546

PHP Multidimensional Array problem

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

Upvotes: 1

Views: 250

Answers (1)

Phil
Phil

Reputation: 164924

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

Upvotes: 1

Related Questions