marry array elements

PHP programming forum. Ask questions or help people concerning PHP code. Don't understand a function? Need help implementing a class? Don't understand a class? Here is where to ask. Remember to do your homework!

Moderator: General Moderators

Post Reply
AikenD
Forum Newbie
Posts: 3
Joined: Mon Jan 24, 2011 3:56 pm

marry array elements

Post 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
User avatar
social_experiment
DevNet Master
Posts: 2793
Joined: Sun Feb 15, 2009 11:08 am
Location: .za

Re: marry array elements

Post 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.
?>
“Don’t worry if it doesn’t work right. If everything did, you’d be out of a job.” - Mosher’s Law of Software Engineering
User avatar
John Cartwright
Site Admin
Posts: 11470
Joined: Tue Dec 23, 2003 2:10 am
Location: Toronto
Contact:

Re: marry array elements

Post by John Cartwright »

We had a thread on this exactly yesterday.

See my response here.
User avatar
AbraCadaver
DevNet Master
Posts: 2572
Joined: Mon Feb 24, 2003 10:12 am
Location: The Republic of Texas
Contact:

Re: marry array elements

Post 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]">
mysql_function(): WARNING: This extension is deprecated as of PHP 5.5.0, and will be removed in the future. Instead, the MySQLi or PDO_MySQLextension should be used. See also MySQL: choosing an API guide and related FAQ for more information.
User avatar
John Cartwright
Site Admin
Posts: 11470
Joined: Tue Dec 23, 2003 2:10 am
Location: Toronto
Contact:

Re: marry array elements

Post by John Cartwright »

Abra approach is better. I'm subjective because I don't ever deal with HTML :(
AikenD
Forum Newbie
Posts: 3
Joined: Mon Jan 24, 2011 3:56 pm

Re: marry array elements

Post 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
User avatar
AbraCadaver
DevNet Master
Posts: 2572
Joined: Mon Feb 24, 2003 10:12 am
Location: The Republic of Texas
Contact:

Re: marry array elements

Post 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.
mysql_function(): WARNING: This extension is deprecated as of PHP 5.5.0, and will be removed in the future. Instead, the MySQLi or PDO_MySQLextension should be used. See also MySQL: choosing an API guide and related FAQ for more information.
AikenD
Forum Newbie
Posts: 3
Joined: Mon Jan 24, 2011 3:56 pm

Re: marry array elements

Post 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
Post Reply