Page 1 of 1

marry array elements

Posted: Mon Jan 24, 2011 4:18 pm
by AikenD
Hi there,
I am using HTML forms to post values from the data entry page. When they arrive on the second page - I have to extract them from the POST array. This is fine, but I now have to merge them somehow - although a simple array_merge is not what I mean - I need to be able to
When I use print_r to view the POSTed data (within the Post Array), I see:
[namesUnem] => Array
(
[0] => Brian
[1] => Richard
[2] => Sarah
)
[commentsUnem] => Array
(
[0] => Brian Comments here
[1] => Richard Comments should be entered here
[2] => Sarah's Comments were also entered here
)
I would need to use some sort of merging function in order to output the following:
array(
[Name] => Brian [Comment] => Brian Comments here
[Name] => Richard [Comment] => Richard Comments should be entered here
[Name] => Sarah [Comment] => Sarah's Comments were also entered here
)
In English, I need to marry each element in separate arrays so that they form a record - element 1 from array 1 with element 1 from array 2, element 2 from array 1 with element 2 from array 2,

Any help greatly appreciated

AikenD

Re: marry array elements

Posted: Mon Jan 24, 2011 4:28 pm
by social_experiment
You are looking to create an associative array.

Code: Select all

<?php
 $arrayName['nameUnem'] = 'Brian';
 $arrayName['commentsUnem'] = 'Brian comments here';
 // and so forth.
?>

Re: marry array elements

Posted: Mon Jan 24, 2011 4:30 pm
by John Cartwright
We had a thread on this exactly yesterday.

See my response here.

Re: marry array elements

Posted: Mon Jan 24, 2011 4:41 pm
by AbraCadaver
Why not change the form so that you get the array that you need?

Code: Select all

<input type="text" name="data[0][name]">
<input type="text" name="data[0][comment]">
<input type="text" name="data[1][name]">
<input type="text" name="data[1][comment]">

Re: marry array elements

Posted: Mon Jan 24, 2011 4:43 pm
by John Cartwright
Abra approach is better. I'm subjective because I don't ever deal with HTML :(

Re: marry array elements

Posted: Mon Jan 24, 2011 4:50 pm
by AikenD
social_experiment wrote:You are looking to create an associative array.

Code: Select all

<?php
 $arrayName['nameUnem'] = 'Brian';
 $arrayName['commentsUnem'] = 'Brian comments here';
 // and so forth.
?>
Hi - many thanks for the quick answers,

I think this is indeed what I need - but no idea how to achieve this...

I am reading through the other thread just now and trying to understand what is happening (I am a bit of a newbie...)

Many thanks

AikenD

Re: marry array elements

Posted: Mon Jan 24, 2011 4:58 pm
by AbraCadaver
This is just a quick example, needs more to check the array length or if the key exists, etc:

Code: Select all

$i = 0
foreach($_POST['namesUnem'] as $name) {
   $row[$i]['name'] = $name;
   $row[$i]['comment'] = $_POST['commentsUnem'][$i];
   $i++;
}
However I would go with changing the form. This makes it much easier and you don't have to go through these gyrations.

Re: marry array elements

Posted: Mon Jan 24, 2011 5:02 pm
by AikenD
AbraCadaver wrote:Why not change the form so that you get the array that you need?

Code: Select all

<input type="text" name="data[0][name]">
<input type="text" name="data[0][comment]">
<input type="text" name="data[1][name]">
<input type="text" name="data[1][comment]">

The Form is dynamic, new rows are added per jave script - so I would need the array numbers to be dynamically added per Javascript : Here is the code I use at the moment:
<form method="post" action="#">
<div id="unemployedComments">
<input id="nameUnem" type="text" name="nameUnem" size="20"> /
<input id="commentUnem" type="text" name="commentUnem" style="width:500px">
</div>
<p><span><a id="minusUnem" href="">[-]</a>&nbsp;<a id="plusUnem" href="">[+ Add another name / comment]</a></span></p>
</form>
Many thanks

AikenD