PHP: How to access array element values using array-index

前端 未结 2 1615
天命终不由人
天命终不由人 2021-01-21 19:42

How to access array element values using array-index?



        
相关标签:
2条回答
  • 2021-01-21 19:57

    array_values is function you are looking for

    Examples:

    <?php
    $json = '{
        "dynamic":{
           "pageCount":"12",
           "tableCount":"1"
        }
    }';
    
    $arr = json_decode($json, true);
    echo $arr['dynamic']['pageCount']; // working
    
    $arr = array_values($arr);
    echo $arr[0]['pageCount']; // NOW working
    
    ?>
    
    0 讨论(0)
  • 2021-01-21 20:14
    $arr = json_decode($json, true);
    foreach ($arr as $key => $value) {
        if (isset($value['pageCount'])) {
            //do something with the page count
        }
    }
    

    If the structure is always a single nested JS object:

    $obj = current($arr);
    echo $obj['pageCount'];
    
    0 讨论(0)
提交回复
热议问题