1

I am noticing some strange behavior while looping through some data. I'm sure it's something simple, but I can't seem to be able to find the bug. I have the following logic:

            <?php
            print 'dumping data : <BR>';
            var_dump($portvlan);
            print '<BR>';
            print 'looping through data: <BR>';
                foreach ($portvlan as $vlandetail){
                    echo 'Vlanid: '.$vlandetail['VlanId'].'<BR>';
                    echo 'Name: '.$vlandetail['Name'].'<BR>';
                    echo 'Mode: '.$vlandetail['Mode'].'<BR>';
                }
            ?>

This is the output that I'm getting:

dumping data :
array(3) { ["VlanId"]=> string(2) "33" ["Name"]=> string(6) "USR_33" ["Mode"]=> string(6) "Access" }
looping through data:
Vlanid: 3
Name: 3
Mode: 3
Vlanid: U
Name: U
Mode: U
Vlanid: A
Name: A
Mode: A

What I was expecting was to see it print a row with 3 cells, with the following values: 33, USR_33, Access.

Can you tell me where I'm going wrong? Thanks.

EDIT 1

This logic works fine when the $portvlan array has more than one entry. For example, on another set of data, the var_dump gives this result:

array(6) { [0]=> array(3) { ["VlanId"]=> string(1) "1" ["Name"]=> string(1) "1" ["Mode"]=> string(5) "Trunk" } [1]=> array(3) { ["VlanId"]=> int(2) ["Name"]=> int(2) ["Mode"]=> string(5) "Trunk" } [2]=> array(3) { ["VlanId"]=> int(3) ["Name"]=> int(3) ["Mode"]=> string(5) "Trunk" } [3]=> array(3) { ["VlanId"]=> int(4) ["Name"]=> int(4) ["Mode"]=> string(5) "Trunk" } [4]=> array(3) { ["VlanId"]=> int(5) ["Name"]=> int(5) ["Mode"]=> string(5) "Trunk" } [5]=> array(3) { ["VlanId"]=> string(2) "33" ["Name"]=> string(2) "33" ["Mode"]=> string(5) "Trunk" } } 

And the loop logic works fine.

2
  • 1
    Are you intending for $portvlan to be an array of vlans? If so, then your data should be an array of arrays, i.e., array(array("VlanID" => "33", "Name" => "USR_33", "Mode" => "Access")). How are you retrieving the source data? Commented Jan 23, 2013 at 0:57
  • @mellamokb can you post this as an answer? Commented Jan 23, 2013 at 1:01

2 Answers 2

1

$vlandetail will be populated with a different item from the array on every iteration of the loop. You shouldn't be treating $vlandetail as an array. Just use it directly.

To get the key name of the array entry, you have to change the loop structure to this:

foreach ($portvlan as $key => $vlandetail) {
     echo $key . ': ' . $vlandetail . '<br>';
}
Sign up to request clarification or add additional context in comments.

2 Comments

the code only seems to be failing when i only have one record in the $portvlan. If I have multiple records, it loops through fine with the current logic. ?
Then the problem is probably in the code where you are adding data in the portvlan array.
0

How you get your $portvlan?

If you can know is $portvlan is not an array of $vlandetail, you can simply do this,

$portvlan = array($portvlan);
// start loop

Or you can do this before you loop it, to make sure $portvlan is a numerical array formed by $vlandetail

$portvlan = (isset($portvlan[0]))?$portvlan:array($portvlan);
// start loop

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.