dougaimian1143 2014-01-06 19:25
浏览 4

获取多维数组的内部数组

I have the following array:

 Array (
                [0] => Array 
                (
                    [example] => 'h'
                )
                [1] => Array 
                (
                    [example] => 'e'
                ) 
                [2] => Array 
                (
                    [example] => 'l'
                )
                [3] => Array 
                (
                    [example] => 'p'
                )
        )

And I am wondering how can I change this array to look like this instead.

Array( [example] => 'h', [example] => 'e', [example] => 'l', [example] => 'p')

I have tried using a nested foreach loop but using that I only get the values and not as an array.

  • 写回答

1条回答 默认 最新

  • dpzp5127 2014-01-06 19:31
    关注

    There are a couple of ways to reach the values of a multidimensional array:

    $array = array(
        0=>array('example'=>'h'),
        1=>array('example'=>'e'),
        2=>array('example'=>'l'),
        3=>array('example'=>'p')
    );
    

    1, If looping through the array:

    foreach($array as $key=>$value){
        echo $value['example'];
    }
    

    2, Call a value directly:

    echo $array[0]['example'];
    echo $array[1]['example'];
    echo $array[2]['example'];
    echo $array[3]['example'];
    

    It is impossible to create an array the way that you mentioned (having 4 'example' as keys). Take the following for example:

    $array['example'] = 'h';
    $array['example'] = 'e';
    $array['example'] = 'l';
    $array['example'] = 'p';
    echo $array['example'];
    

    The output would be p because you are simply overwriting the variable $array['example'] each time.

    评论

报告相同问题?

悬赏问题

  • ¥15 如何用Labview在myRIO上做LCD显示?(语言-开发语言)
  • ¥15 Vue3地图和异步函数使用
  • ¥15 C++ yoloV5改写遇到的问题
  • ¥20 win11修改中文用户名路径
  • ¥15 win2012磁盘空间不足,c盘正常,d盘无法写入
  • ¥15 用土力学知识进行土坡稳定性分析与挡土墙设计
  • ¥70 PlayWright在Java上连接CDP关联本地Chrome启动失败,貌似是Windows端口转发问题
  • ¥15 帮我写一个c++工程
  • ¥30 Eclipse官网打不开,官网首页进不去,显示无法访问此页面,求解决方法
  • ¥15 关于smbclient 库的使用