如何从多维数组中检索键

时间:2015-10-24 09:31:44

标签: php arrays

我有一个多维数组,如下所示:

$permis =array(
 'employee' => array(
     'myprofile' => array(
        'default' => '1', 'personal' => '0', 'job' => '0', 'leave' => '0', 'permission' => '0', 'bonus & commision' => '0', 'document' => '0', 'emergency contact' => '0', 'benifits' => '0'
        )
    )
);

我想只得到关键值存储在如下表格中:

employee  myprofile default

employee  myprofile personal

employee myprofile  job

employee myprofile leave
等等。

如何在php中使用foreach获取此内容

4 个答案:

答案 0 :(得分:2)

试试这段代码。

<?php

    $permis =array(
     'employee' => array(
         'myprofile' => array(
            'default' => '1', 'personal' => '0', 'job' => '0', 'leave' => '0', 'permission' => '0', 'bonus & commision' => '0', 'document' => '0', 'emergency contact' => '0', 'benifits' => '0'
            )
        )
    );


    foreach($permis as $key => $myprofile){

        $loop1 = $key;
        foreach($myprofile as $key => $profile){
            $loop2 = implode(" ",array_keys($myprofile));
            foreach($profile as $key => $data){
                echo $loop1."    ".$loop2."    ".$key."<br />";
            }
        }

    }
    ?>

答案 1 :(得分:0)

如果您的数组结构是静态的,即始终如图所示,则以下内容将起作用:

foreach ($permis as $employee => $permit) {
  foreach ($permit as $profile => data) {
    foreach ($data as $item => $value) {
      $row = array($employee, $profile, $item);

      // Do what you like with $row here

    }
  }
}

答案 2 :(得分:0)

foreach($permis as $employee){
     foreach($employee as  $profile){
           $default = $profile['default'];
           //add here all other values;
     }

}

但是,如果每个员工只有一个配置文件,您可以这样做:

foreach ($permis as $employee){
  $default = $employee['myprofile']['default'];

}

希望这个帮助

答案 3 :(得分:0)

使用foreach循环:

$array = [1,2,3,4];
foreach($array as $key => $value){
    echo 'key=' . $key . '<br/>';
    echo 'value=' . $value. '<br/>';
}