0

I know this is pretty stupid but i'm wondering how to access the FIFTH array inside this array.

    array(1) {
          [0] = > string(3)"913"
    }
    array(2) {
          [0] = > string(3)"913"
          [1] = > string(2)"95"
    }
    array(3) {
          [0] = > string(3)"913" 
          [1] = > string(2)"95"
          [2] = > string(1)"3"
    }
    array(4) {
          [0] = > string(3)"913"
          [1] = > string(2)"95"
          [2] = > string(1)"3"
          [3] = > string(1)"6"
    }
    array(5) {
          [0] = > string(3)"913"
          [1] = > string(2)"95"
          [2] = > string(1)"3"
          [3] = > string(1)"6"
          [4] = > string(1)"0"
    }

can't seem to access it with <?php echo $array[5]; ?> sorry again for the dumb question

11
  • 1
    How about print_r( $array[4] ); ? Commented Jan 10, 2014 at 15:30
  • 1
    Arrays in PHP are zero based. This means you would use $array[4] to get the fifth array. Commented Jan 10, 2014 at 15:31
  • undefined offset. really weird :( Commented Jan 10, 2014 at 15:33
  • Are you sure you are looking at the right array? Commented Jan 10, 2014 at 15:39
  • yep. the array $array contains the json above, i am really stumped by this. Commented Jan 10, 2014 at 15:41

3 Answers 3

2

Arrays are zero-indexed. Which means 0 is the first item, 1 the second, etc.

Try <?php print_r($array[4]) ?> :)

Sign up to request clarification or add additional context in comments.

1 Comment

undefined offset. really weird :(
1
$arr = array(
    array("913"),
    array("913", "95"),
    array("913", "95", "3"),
    array("913", "95", "3", "6"),
    array("913", "95", "3", "6", "0")
);

var_dump($arr);
//output
/*
array(5) {
  [0]=>
  array(1) {
    [0]=>
    string(3) "913"
  }
  [1]=>
  array(2) {
    [0]=>
    string(3) "913"
    [1]=>
    string(2) "95"
  }
  [2]=>
  array(3) {
    [0]=>
    string(3) "913"
    [1]=>
    string(2) "95"
    [2]=>
    string(1) "3"
  }
  [3]=>
  array(4) {
    [0]=>
    string(3) "913"
    [1]=>
    string(2) "95"
    [2]=>
    string(1) "3"
    [3]=>
    string(1) "6"
  }
  [4]=>
  array(5) {
    [0]=>
    string(3) "913"
    [1]=>
    string(2) "95"
    [2]=>
    string(1) "3"
    [3]=>
    string(1) "6"
    [4]=>
    string(1) "0"
  }
}
*/


print_r($arr[4]);
//Output : Array ( [0] => 913 [1] => 95 [2] => 3 [3] => 6 [4] => 0 ) 

// loop through 5th array
foreach($arr[4] as $key => $val) {
    echo $key." => ".$val."<br/>";
}

// Output
/*    
0 => 913
1 => 95
2 => 3
3 => 6
4 => 0
*/

echo "Third value : ".$arr[4][2];
//Third value : 3 

Comments

0

By default, array values start with 0, so the 5th element would be #4:

print_r( $array[4] );

1 Comment

undefined offset. really weird :(

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.