循环遍历每个键和php多维数组的值

时间:2014-06-02 13:31:51

标签: php arrays multidimensional-array

我有以下数组:

<pre>Array
(
    [student] => Array
        (
            [admission_no] => Array
                (
                    [isEmpty] => Please enter Admission No
                )

            [admission_date] => Array
                (
                    [isEmpty] => Please enter Admission Date
                )

        )

    [student_personal_details] => Array
        (
            [first_name] => Array
                (
                    [isEmpty] => Please enter First Name
                )

            [gender] => Array
                (
                    [isEmpty] => Please select Gender
                )

            [birth_date] => Array
                (
                    [isEmpty] => Please enter Birth Date
                )

        )

    [student_gardian_details] => Array
        (
            [first_name] => Array
                (
                    [isEmpty] => Please enter Gardian First Name
                )

        )

    [student_education_details] => Array
        (
            [roll_no] => Array
                (
                    [isEmpty] => Please enter Pin Code
                )

        )

)

我想使用while循环获取此数组中的所有字符串。我的意思是整个数组的键不是数组本身。

我尝试了以下代码:

private function getInternalErorrString($array) {
        while (list($var, $val) = each($array)) {
            if(is_array($var)) {
                $this->getInternalErorrString($var);
            } else {
                return $val;
            }
        }
    }

我可以有更好的主意吗?

4 个答案:

答案 0 :(得分:1)

或者,您可以使用SPL RecursiveArrayIterator来获取值。考虑这个例子:

$values = array( 'student' => array( 'admission_no' => array('isEmpty' => 'Please enter Admission No'), 'admission_date' => array('isEmpty' => 'Please enter Admission Date'), ), 'student_personal_details' => array( 'first_name' => array('isEmpty' => 'Please enter First Name'), 'gender' => array('isEmpty' => 'Please enter Gender'), 'birth_date' => array('isEmpty' => 'Please enter Birth Date'), ), 'student_gardian_details' => array( 'first_name' => array('isEmpty' => 'Please enter Gardian First Name'), ), 'student_education_details' => array( 'roll_no' => array('isEmpty' => 'Please enter Pin Code'), ),);

$iterator = new RecursiveIteratorIterator(new RecursiveArrayIterator($values));
foreach($iterator as $key => $value) {
    $data[] = $value;
}

示例输出:

Array
(
    [0] => Please enter Admission No
    [1] => Please enter Admission Date
    [2] => Please enter First Name
    [3] => Please enter Gender
    [4] => Please enter Birth Date
    [5] => Please enter Gardian First Name
    [6] => Please enter Pin Code
)

答案 1 :(得分:0)

您可以查看php函数array_values以及文档页面中的示例。

另见How to “flatten” a multi-dimensional array to simple one in PHP?

答案 2 :(得分:0)

简单的功能可以实现这个

function finalKeys($array)
{
    global $return;
    foreach($array as $key) {
        if (is_array($key)) {
            finalKeys($key);
        }
        else {
            $return[] = $key;
        }
    }

    return $return;
}

用法: finalKeys($multidimensional_array);

答案 3 :(得分:0)

我没有检查您的代码,但更改为您的以下代码将导致您的预期。如果您只想获取一组消息,请使用public var存储该值以代替回显它。

 private function getInternalErorrString($array) {
      foreach($array as $val) 
      {
         if(is_array($val)) {
            $this->getInternalErorrString($val);
         } 
         else {
            echo $val ."<br/>";
         }
      }
   }

   $this->getInternalErorrString($array);