简体   繁体   中英

Loop through each key and value of php multidimensional array

I have the following 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
                )

        )

)

I want to get all the strings in this array using while loop. I mean keys of the whole array that are not an array itself.

I tried the following code:

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

Can I have any better idea?

Alternatively, you could use SPL RecursiveArrayIterator to get the values. Consider this example:

$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;
}

Sample Output:

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
)

You can check the php function array_values and the examples in the documentation page.

See also How to “flatten” a multi-dimensional array to simple one in PHP?

simple function can achive this

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

    return $return;
}

Usage: finalKeys($multidimensional_array);

I didn't checked your code but change to your below code will result what you expected. If you want to get an array of messages only then use public var to store the value in place of echoing it.

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

   $this->getInternalErorrString($array);

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM