1

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

2
  • What's in the addZipCode() method? Commented Aug 17, 2011 at 23:35
  • @Phil its my attempt to normalize my data. all it does it check if the zip code is in another table. if it is then it return the zip code ID. if it isnt then it adds it to the other table and then return back the ID. Commented Aug 17, 2011 at 23:41

1 Answer 1

1
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;
}
Sign up to request clarification or add additional context in comments.

Comments

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.