如何访问数组中的某个数组:PHP

时间:2014-01-10 15:29:43

标签: php arrays json

我知道这很愚蠢,但我想知道如何访问这个数组中的FIFTH数组。

    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"
    }

似乎无法使用<?php echo $array[5]; ?>再次访问它,对于这个愚蠢的问题

3 个答案:

答案 0 :(得分:2)

数组是零索引的。这意味着0是第一项,1是第二项,等等。

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

答案 1 :(得分: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 

答案 2 :(得分:0)

默认情况下,数组值以0开头,因此第5个元素为#4:

print_r( $array[4] );