0

I have got the below array

Array
(
    [0] => Array
        (
            [0] => Contact Number
            [1] => 35443545
        )

    [1] => Array
        (
            [0] => Address
            [1] => vvvv
        )

)

I would like to display as

Contact Number
35443545
<hr>
Address
vvvv

My code

foreach($address_box_content as $k=>$address)
{
    echo '<h2>'.$address[$k].'</h2><p>'.$address[$k+1].'</p>';
}

But for some reason it is printing the first 2 lines and then displaying a notice 'Undefined offset:2'

4
  • 2
    echo '<h2>'.$address[0].'</h2><p>'.$address[1].'</p><hr>'; Commented Jul 29, 2014 at 13:11
  • You need Array Flattern function. Commented Jul 29, 2014 at 13:11
  • 1
    Should rename your vars, name $address to $property and it will be more clear Commented Jul 29, 2014 at 13:12
  • 1
    @t3chguy Superb....so stupid I am. Thanks. Commented Jul 29, 2014 at 13:13

4 Answers 4

2

What you have is an array. One whose elements are (also) arrays. Each array that you have stored has 2 elements, 0 and 1.

When you loop, $k is the index of the main (outer) array. Its value doesn't make any sense in the inner array. You just need to loop over the outer array, and print the 0 and 1 elements from the inner one.

foreach($address_box_content as $address)
{
    echo '<h2>'.$address[0].'</h2><p>'.$address[1].'</p>';
}
Sign up to request clarification or add additional context in comments.

Comments

2

Well you're getting undefined offset error because you're using $k which is the index of the outer array

You can do something like:

foreach($address_box_content as $addresses){
    foreach($addresses as $address){
        echo '<h2>', $address, '</h2><p>', $address, '</p>';
    }
}

And if you want to get the index of inner array:

foreach($address_box_content as $addresses){
    foreach($addresses as $key => $address){
        echo '<h2>', $address[$key], '</h2><p>', $address[$key], '</p>';
    }
}

Comments

1
This is your array 
$data = Array
(
    [0] => Array
        (
            [0] => Contact Number
            [1] => 35443545
        )

    [1] => Array
        (
            [0] => Address
            [1] => vvvv
        )

)
simple way to print array 
for($i=0;$<count($data);$i++)
{
  echo  "<h2>".$data[$i][$0]."</h2>"."<p>".$value[$i][1]."</p>"."<hr />";
}

Comments

0

well, for each element you define a key and a value which are $k and $address.

$k will be 0 and 1, $address will be 0 and 1, and then 0 and 1.

the undefined offset error is because you call $k plus 1 that on the second foeach iteration try to access to position 1+1 (2) doesn't find anything.

you could use something like:

foreach($address_box_content as $data => $value) {

    echo    "<h2>" . $value[0] . "</h2>"
          . "<p>" . $value[1] . "</p>"
          . "<hr />";

}

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.