By using this site, you agree to our updated Privacy Policy and our Terms of Use. Manage your Cookies Settings.
455,748 Members | 1,427 Online
Bytes IT Community
+ Ask a Question
Need help? Post your question and get tips & solutions from a community of 455,748 IT Pros & Developers. It's quick & easy.

create array of arrays

P: n/a
How is this done in php? I've tried several variations of the following:

contact_array[] =
($_POST['contact_relation'],
$_POST['contact_first_name'],
$_POST['contact_last_name'],
$_POST['contact_phone'],
$_POST['contact_email']);

The ultimate goal is to insert the array into a table in a Mysql db.

Thanks,
Bart

Jul 17 '05 #1
Share this Question
Share on Google+
6 Replies


P: n/a
On Wed, 24 Mar 2004 15:35:38 -0500, Bart Nessux <ba*********@hotmail.com>
wrote:
How is this done in php? I've tried several variations of the following:

contact_array[] =
($_POST['contact_relation'],
$_POST['contact_first_name'],
$_POST['contact_last_name'],
$_POST['contact_phone'],
$_POST['contact_email']);

The ultimate goal is to insert the array into a table in a Mysql db.


http://uk.php.net/manual/en/function.array.php

$contact_array = array(
$_POST['contact_relation'],
$_POST['contact_first_name'],
$_POST['contact_last_name'],
$_POST['contact_phone'],
$_POST['contact_email']
);

--
Andy Hassall <an**@andyh.co.uk> / Space: disk usage analysis tool
http://www.andyh.co.uk / http://www.andyhsoftware.co.uk/space
Jul 17 '05 #2

P: n/a
Andy Hassall wrote:
On Wed, 24 Mar 2004 15:35:38 -0500, Bart Nessux <ba*********@hotmail.com>
wrote:

How is this done in php? I've tried several variations of the following:

contact_array[] =
($_POST['contact_relation'],
$_POST['contact_first_name'],
$_POST['contact_last_name'],
$_POST['contact_phone'],
$_POST['contact_email']);

The ultimate goal is to insert the array into a table in a Mysql db.

http://uk.php.net/manual/en/function.array.php

$contact_array = array(
$_POST['contact_relation'],
$_POST['contact_first_name'],
$_POST['contact_last_name'],
$_POST['contact_phone'],
$_POST['contact_email']
);

--
Andy Hassall <an**@andyh.co.uk> / Space: disk usage analysis tool
http://www.andyh.co.uk / http://www.andyhsoftware.co.uk/space


That works well. However, I don't understand how to get to the elements
of the inner arrays. I can see all of the arrays, but only the elements
of the last array when I do this:

$contact_array = array(
$_POST['contact_relation'],
$_POST['contact_first_name'],
$_POST['contact_last_name'],
$_POST['contact_phone'],
$_POST['contact_email']);
foreach ($contact_array as $E)
{
echo $E; echo '<br>';
}
foreach ($E as $e)
{
echo $e; echo '<br>';
}

Any more advice?

Jul 17 '05 #3

P: n/a
On Wed, 24 Mar 2004 16:37:38 -0500, Bart Nessux <ba*********@hotmail.com>
wrote:
Andy Hassall wrote:
On Wed, 24 Mar 2004 15:35:38 -0500, Bart Nessux <ba*********@hotmail.com>
wrote:
How is this done in php? I've tried several variations of the following:

contact_array[] =
($_POST['contact_relation'],
$_POST['contact_first_name'],
$_POST['contact_last_name'],
$_POST['contact_phone'],
$_POST['contact_email']);

The ultimate goal is to insert the array into a table in a Mysql db.

http://uk.php.net/manual/en/function.array.php

$contact_array = array(
$_POST['contact_relation'],
$_POST['contact_first_name'],
$_POST['contact_last_name'],
$_POST['contact_phone'],
$_POST['contact_email']
);


That works well. However, I don't understand how to get to the elements
of the inner arrays. I can see all of the arrays, but only the elements
of the last array when I do this:

$contact_array = array(
$_POST['contact_relation'],
$_POST['contact_first_name'],
$_POST['contact_last_name'],
$_POST['contact_phone'],
$_POST['contact_email']);
foreach ($contact_array as $E)
{
echo $E; echo '<br>';
}
foreach ($E as $e)
{
echo $e; echo '<br>';
}

Any more advice?


There are no inner arrays, that's a one-dimensional array - what layout were
you expecting? Maybe you need to keep the [] on then as in your original post,
so $contact_array[] = array(...) ?

var_dump($contact_array) may also be useful; put <pre></pre> tags around it to
get the best effect.

--
Andy Hassall <an**@andyh.co.uk> / Space: disk usage analysis tool
http://www.andyh.co.uk / http://www.andyhsoftware.co.uk/space
Jul 17 '05 #4

P: n/a
Bart Nessux wrote:
Andy Hassall wrote:
On Wed, 24 Mar 2004 15:35:38 -0500, Bart Nessux <ba*********@hotmail.com>
wrote:

How is this done in php? I've tried several variations of the following:

contact_array[] =
($_POST['contact_relation'],
$_POST['contact_first_name'],
$_POST['contact_last_name'],
$_POST['contact_phone'],
$_POST['contact_email']);

The ultimate goal is to insert the array into a table in a Mysql db.


http://uk.php.net/manual/en/function.array.php

$contact_array = array(
$_POST['contact_relation'],
$_POST['contact_first_name'],
$_POST['contact_last_name'],
$_POST['contact_phone'],
$_POST['contact_email']
);

--
Andy Hassall <an**@andyh.co.uk> / Space: disk usage analysis tool
http://www.andyh.co.uk / http://www.andyhsoftware.co.uk/space

That works well. However, I don't understand how to get to the elements
of the inner arrays. I can see all of the arrays, but only the elements
of the last array when I do this:

$contact_array = array(
$_POST['contact_relation'],
$_POST['contact_first_name'],
$_POST['contact_last_name'],
$_POST['contact_phone'],
$_POST['contact_email']);
foreach ($contact_array as $E)
{
echo $E; echo '<br>';
}
foreach ($E as $e)
{
echo $e; echo '<br>';
}

Any more advice?


I don't know if it's related but with a clean indentation, you'd see
that the behaviour you complain about is exactly what it should be.

/* loop on each $contact_array */
foreach ($contact_array as $E) {
echo $E; echo '<br>';
}

/* loop on each item of the last item of $contact_array */
foreach ($E as $e) {
echo $e; echo '<br>';
}

If you want to walk through nested arrays, you need to use nested loops:

foreach ($contact_array as $contact) {
echo $contact . "<br>\n";

/* check before, so you won't get an error */
if (is_array($contact)) {
/* and here is the nested loop */
foreach ($contact as $item) {
echo $item . "<br>\n";
}
}
}

Now if $item is an array itself, you'd need 3 nested loops, and so on...
The usual generalisation is to use recursion :

function r_echo($anyarray)
{
echo $anyarray . "<br>\n";
if (is_array($anyarray)) {
foreach($anyarray as $item) {
r_echo($item);
}
}
}

Now there are functions like print_r and var_dump() that already do this...

HTH
Bruno

Jul 17 '05 #5

P: n/a
Bruno Desthuilliers wrote:
Bart Nessux wrote:
Andy Hassall wrote:
On Wed, 24 Mar 2004 15:35:38 -0500, Bart Nessux
<ba*********@hotmail.com>
wrote:
How is this done in php? I've tried several variations of the
following:

contact_array[] =
($_POST['contact_relation'],
$_POST['contact_first_name'],
$_POST['contact_last_name'],
$_POST['contact_phone'],
$_POST['contact_email']);

The ultimate goal is to insert the array into a table in a Mysql db.


http://uk.php.net/manual/en/function.array.php

$contact_array = array(
$_POST['contact_relation'],
$_POST['contact_first_name'],
$_POST['contact_last_name'],
$_POST['contact_phone'],
$_POST['contact_email']
);

--
Andy Hassall <an**@andyh.co.uk> / Space: disk usage analysis tool
http://www.andyh.co.uk / http://www.andyhsoftware.co.uk/space


That works well. However, I don't understand how to get to the
elements of the inner arrays. I can see all of the arrays, but only
the elements of the last array when I do this:

$contact_array = array(
$_POST['contact_relation'],
$_POST['contact_first_name'],
$_POST['contact_last_name'],
$_POST['contact_phone'],
$_POST['contact_email']);
foreach ($contact_array as $E)
{
echo $E; echo '<br>';
}
foreach ($E as $e)
{
echo $e; echo '<br>';
}

Any more advice?

I don't know if it's related but with a clean indentation, you'd see
that the behaviour you complain about is exactly what it should be.

/* loop on each $contact_array */
foreach ($contact_array as $E) {
echo $E; echo '<br>';
}

/* loop on each item of the last item of $contact_array */
foreach ($E as $e) {
echo $e; echo '<br>';
}

If you want to walk through nested arrays, you need to use nested loops:

foreach ($contact_array as $contact) {
echo $contact . "<br>\n";

/* check before, so you won't get an error */
if (is_array($contact)) {
/* and here is the nested loop */
foreach ($contact as $item) {
echo $item . "<br>\n";
}
}
}

Now if $item is an array itself, you'd need 3 nested loops, and so on...
The usual generalisation is to use recursion :

function r_echo($anyarray)
{
echo $anyarray . "<br>\n";
if (is_array($anyarray)) {
foreach($anyarray as $item) {
r_echo($item);
}
}
}

Now there are functions like print_r and var_dump() that already do this...

HTH
Bruno


Thanks guys. Both var_dump() and print_r do what I need. Thanks for the
example function too.

Jul 17 '05 #6

P: n/a
AJ
put the second foreach inside the first:

foreach ($contact_array as $E)
{
echo $E; echo '<br>';
foreach ($E as $e)
{
echo $e; echo '<br>';
}
}
On Wed, 24 Mar 2004 16:37:38 -0500, Bart Nessux <ba*********@hotmail.com>
wrote:
That works well. However, I don't understand how to get to the elements
of the inner arrays. I can see all of the arrays, but only the elements
of the last array when I do this:

$contact_array = array(
$_POST['contact_relation'],
$_POST['contact_first_name'],
$_POST['contact_last_name'],
$_POST['contact_phone'],
$_POST['contact_email']);
foreach ($contact_array as $E)
{
echo $E; echo '<br>';
}
foreach ($E as $e)
{
echo $e; echo '<br>';
}

Any more advice?


Jul 17 '05 #7

This discussion thread is closed

Replies have been disabled for this discussion.