Looping through 2 arrays in php.

Let's say we have 2 arrays that are structured like so:
$items=array();
$items['names']=array();

$item = array();
$item['first_name'] = 'John';
$item['last_name'] = 'Smith';

$items['names'][]=$item;

$item = array();
$item['first_name'] = 'James';
$item['last_name'] = 'Doe';

$items['names'][]=$item;

$item = array();
$item['first_name'] = 'Frank';
$item['last_name'] = 'Jones';

$items['names'][]=$item;

$field=array();
$field['available_fields']=array(
								array('field_name'=>'first_name','field_type'=>'text','field_id'=>'first_name'),
								array('field_name'=>'last_name','field_type'=>'text','field_id'=>'last_name')
							);
							
$field['field_name']='names';

Open in new window


1 of these arrays will be used to determine the names of input fields ( $field array ) .  The second array is an example of the fields that already have a value.  Based on these 2 arrays, I need to build out text input fields that contain the name and values from array $items and then one empty text field.

The desired output would look something like this:
 
$html = '<div class="field_block">
		 <input type="text" name="first_name" id="" value="John">
		 <input type="text" name="last_name" id="" value="Smith">
		 </div>
		 <div class="field_block">
		 <input type="text" name="first_name" id="" value="James">
		 <input type="text" name="last_name" id="" value="Doe">
		 </div>
		 <div class="field_block">
		 <input type="text" name="first_name" id="" value="Frank">
		 <input type="text" name="last_name" id="" value="Jones">
		 </div>
		 <div class="field_block">
		 <input type="text" name="first_name" id="" value="">
		 <input type="text" name="last_name" id="" value="">
		 </div>';

Open in new window



Any guidance would be greatly appreciated!
theblanc0Asked:
Who is Participating?
I wear a lot of hats...

"The solutions and answers provided on Experts Exchange have been extremely helpful to me over the last few years. I wear a lot of hats - Developer, Database Administrator, Help Desk, etc., so I know a lot of things but not a lot about one thing. Experts Exchange gives me answers from people who do know a lot about one thing, in a easy to use platform." -Todd S.

Ray PaseurCommented:
Where did you get the structure of these arrays?  Is that something you can change?
0
theblanc0Author Commented:
No, unfortunately the structure can't really be changed.  The structure comes from a  template being stored in a database along with items that contain values based off of "instructions" in the template.  


0
Ray PaseurCommented:
OK.  I think I might have chosen a different structure to start with, but maybe we can still make it work.  Back in a few...
0
Introducing Cloud Class® training courses

Tech changes fast. You can learn faster. That’s why we’re bringing professional training courses to Experts Exchange. With a subscription, you can access all the Cloud Class® courses to expand your education, prep for certifications, and get top-notch instructions.

Ray PaseurCommented:
Here you go.  Not sure if you want to populate the id- attributes of the input tags, so I put the information into them.  You can take it out if you don't want it there.  And sorry about the goofy-looking indenting.  That's an unfortunate artifact of the way PHP HEREDOC syntax works.  If you could have the block terminator be the only thing on the line, without forcing it to start in position 1, it would really help readability.  But alas...

See http://www.laprbass.com/RAY_temp_theblanco.php

Happy New Year, ~Ray
<?php // RAY_temp_theblanco.php
error_reporting(E_ALL);
echo "<pre>";

// CREATE THE ITEMS ARRAY OF SUB-ARRAYS
$items = array();
$items['names'] = array();

$item = array();
$item['first_name'] = 'John';
$item['last_name']  = 'Smith';
$items['names'][]   = $item;

$item = array();
$item['first_name'] = 'James';
$item['last_name']  = 'Doe';
$items['names'][]   = $item;

$item = array();
$item['first_name'] = 'Frank';
$item['last_name']  = 'Jones';
$items['names'][]   = $item;

// ACTIVATE THIS TO VISUALIZE THE $items ARRAY
// print_r($items);

// CREATE THE FIELDS ARRAY
$field=array();
$field['available_fields'] = array
( array
  ( 'field_name' => 'first_name'
  , 'field_type' => 'text'
  , 'field_id'   => 'first_name'
  )
, array
  ( 'field_name' => 'last_name'
  , 'field_type' => 'text'
  , 'field_id'   => 'last_name'
  )
)
;

$field['field_name'] = 'names';

// ACTIVATE THIS TO VISUALIZE THE $field ARRAY
// print_r($field);



// CREATE THE OUTPUT HERE
$out = NULL;



// ITERATE OVER THE DATA TO PRODUCE THE PREPOPULATED FORM INPUT TAGS
foreach ($items[$field["field_name"]] as $item)
{
    $input_pair = NULL;

    // ITERATE OVER THE FIELD NAMES
    foreach ($field['available_fields'] as $available_field)
    {
        // USE HEREDOC NOTATION TO DEFINE THE INPUT STATEMENTS
        $input = <<<INPUT
<input type="{$available_field['field_type']}" name="{$available_field['field_name']}" id="{$available_field['field_id']}" value="{$item[$available_field['field_name']]}" />
INPUT;
        $input_pair .= $input;
    }

    // USE HEREDOC NOTATION TO DEFINE THE HTML
	$htmldiv = <<<HTMLDIV

<div class="field_block">
$input_pair
</div>
HTMLDIV;

    // ADD EACH HTML DIV TO THE OUTPUT STRING
    $out .= $htmldiv;
}



// ITERATE OVER THE DATA TO PRODUCE ONE EMPTY FORM INPUT TAG SET
foreach ($items[$field["field_name"]] as $item)
{
    $input_pair = NULL;

    // ITERATE OVER THE FIELD NAMES
    foreach ($field['available_fields'] as $available_field)
    {
        // USE HEREDOC NOTATION TO DEFINE THE INPUT STATEMENTS
        $input = <<<INPUT
<input type="{$available_field['field_type']}" name="{$available_field['field_name']}" id="{$available_field['field_id']}" value="" />
INPUT;
        $input_pair .= $input;
    }

    // USE HEREDOC NOTATION TO DEFINE THE HTML
	$htmxdiv = <<<HTMXDIV

<div class="field_block">
$input_pair
</div>
HTMXDIV;

    break;
}

// ADD ONE LAST HTML DIV TO THE OUTPUT STRING
$out .= $htmxdiv;


// SHOW THE WORK PRODUCT
echo htmlentities($out);

Open in new window

0

Experts Exchange Solution brought to you by

Your issues matter to us.

Facing a tech roadblock? Get the help and guidance you need from experienced professionals who care. Ask your question anytime, anywhere, with no hassle.

Start your 7-day free trial
theblanc0Author Commented:
Thanks - this is an excellent solution and the code provided is very easy to follow.  This one has had me for a day.
0
Ray PaseurCommented:
Thanks for the points - it's a great question, glad I could help.
0
It's more than this solution.Get answers and train to solve all your tech problems - anytime, anywhere.Try it for free Edge Out The Competitionfor your dream job with proven skills and certifications.Get started today Stand Outas the employee with proven skills.Start learning today for free Move Your Career Forwardwith certification training in the latest technologies.Start your trial today
PHP

From novice to tech pro — start learning today.