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
Vikas Arora
1,6602 gold badges17 silver badges39 bronze badges
asked Jan 10, 2014 at 15:29
rnldpbln
6743 gold badges11 silver badges24 bronze badges
3 Answers 3
Arrays are zero-indexed. Which means 0 is the first item, 1 the second, etc.
Try <?php print_r($array[4]) ?> :)
answered Jan 10, 2014 at 15:31
Duroth
6,5812 gold badges22 silver badges23 bronze badges
Sign up to request clarification or add additional context in comments.
1 Comment
rnldpbln
undefined offset. really weird :(
$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
answered Jan 10, 2014 at 15:45
RaviRokkam
7899 silver badges16 bronze badges
Comments
By default, array values start with 0, so the 5th element would be #4:
print_r( $array[4] );
answered Jan 10, 2014 at 15:31
Peon
8,0417 gold badges63 silver badges104 bronze badges
1 Comment
rnldpbln
undefined offset. really weird :(
lang-php
print_r( $array[4] );?$array[4]to get the fifth array.