create array of arrays – PHP

  php

Q(Question):

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

A(Answer):

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

A(Answer):

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?

A(Answer):

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

A(Answer):

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

A(Answer):

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.

A(Answer):

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?

LEAVE A COMMENT