Dissecting a Multidimensional (dis)Array

Hi all,

PHP scrub here. I have a form which allows a user to input data into as many duplicate fields as necessary and then it’s stored in separate tables on a MySQL database. When it comes to working with just one field that can be duplicated n-times, I have no trouble. I can loop through and insert the data as needed by doing something like so:


if (isset($_POST['submit'])) {

$this = escape($_POST['this']);
$that = escape($_POST['that']);

$sql = "INSERT INTO table SET this='$this', that='$that'";
$result = mysqli_query($link, $sql);

if(!$result) {
 // begin catastrophic meltdown
}

$last_id = mysqli_insert_id($link);

if ($_POST['field-thats-duplicated']) {

foreach ($_POST['field-thats-duplicated'] as $key=>$value) {

$sql = "INSERT INTO another-table SET field='$value', user_id='$last_id'";
$result = mysqli_query($link, $sql);

if(!$result) {
 // cue Chuck Norris karate-chop
   }
 }
} else {
// Nothing added, it's Hammer time.
 }
}

My problem arises when allowing a user to duplicate n-times, a fieldset which contains three input fields. Here is a chunk of what I am receiving from my new form:


array(12) {
    ["name"]=> string(18) "Firstname Lastname"
    ["email"]=> string(16) "email@domain.web"
    ["licensure"]=> string(10) "California"
    ["languages"]=> string(26) "English, Spanish, Japanese"
    
    ["education"]=> array(1) {
         ["education"]=> array(3) {
        	    [0]=> array(3) {
                       	["edu_date"]=> string(11) "1996 - 2000"
                        ["edu_degree"]=> string(7) "Diploma"
                        ["edu_detail"]=> string(11) "High School"
                        }
                            
                    [1]=> array(3) {
                    	["edu_date"]=> string(11) "2000 - 2004"
                        ["edu_degree"]=> string(17) "Bachelor's Degree"
                        ["edu_detail"]=> string(26) "San Diego State University"
                        }
                            
                    [2]=> array(3) {
                    	["edu_date"]=> string(11) "2004 - 2008"
                    	["edu_degree"]=> string(9) "Doctorate"
                        ["edu_detail"]=> string(24) "Leo's Beautician School"
                        }
                    }
                }

and so on...

I’m confused as to how I can go about dissecting the above “education” array into chunks to be inserted into its respected table/rows. Any insight or links would be greatly appreciated. My searches online so far have been in vain. Please, speak slowly though, I am “special”; it’s written on my helmet.

Thank you for your time. :slight_smile:

Perfect! Thank you, webgypsy. With a bit of tweaking I was able to achieve what I was after. Many thanks again for your input; it’s much appreciated. :slight_smile: