473,326 Members | 2,134 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,326 software developers and data experts.

create array of arrays

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
6 2225
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
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
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
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
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
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 thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

11
by: Gene | last post by:
I want to create a certain number of arrays on the same time to use, is it possible to do that?
21
by: yeti349 | last post by:
Hi, I'm using the following code to retrieve data from an xml file and populate a javascript array. The data is then displayed in html table form. I would like to then be able to sort by each...
104
by: Leszek | last post by:
Hi. Is it possible in javascript to operate on an array without knowing how mamy elements it has? What i want to do is sending an array to a script, and this script should add all values from...
5
by: Richard Lewis Haggard | last post by:
I am trying to create multi-dimensioned arrays in conventional ASP pages and pass these arrays as arguments to functions that are in a C# interop assembly. ASP complains because it doesn't...
24
by: Michael | last post by:
Hi, I am trying to pass a function an array of strings, but I am having trouble getting the indexing to index the strings rather than the individual characters of one of the strings. I have...
23
by: sandy | last post by:
I need (okay, I want) to make a dynamic array of my class 'Directory', within my class Directory (Can you already smell disaster?) Each Directory can have subdirectories so I thought to put these...
17
by: =?Utf-8?B?U2hhcm9u?= | last post by:
Hi Gurus, I need to transfer a jagged array of byte by reference to unmanaged function, The unmanaged code should changed the values of the array, and when the unmanaged function returns I need...
2
by: =?Utf-8?B?SHVzYW0=?= | last post by:
Hi EveryBody: I am using vb.net 2005 and the following code trying to create varible at runtime: For n=1 to 4 dim v+n.tostring(n) as double Next n the resone behaind using the previous...
4
by: Sunny | last post by:
Hi, Is there a way in javascript to create Dynamic arrays or arrays on fly. Something Like: var "ptsgN"+sd = new Array(); Here sd is incrementing by 1. I have lots of data that I am...
0
by: DolphinDB | last post by:
Tired of spending countless mintues downsampling your data? Look no further! In this article, you’ll learn how to efficiently downsample 6.48 billion high-frequency records to 61 million...
0
by: ryjfgjl | last post by:
ExcelToDatabase: batch import excel into database automatically...
0
isladogs
by: isladogs | last post by:
The next Access Europe meeting will be on Wednesday 6 Mar 2024 starting at 18:00 UK time (6PM UTC) and finishing at about 19:15 (7.15PM). In this month's session, we are pleased to welcome back...
0
by: jfyes | last post by:
As a hardware engineer, after seeing that CEIWEI recently released a new tool for Modbus RTU Over TCP/UDP filtering and monitoring, I actively went to its official website to take a look. It turned...
0
by: ArrayDB | last post by:
The error message I've encountered is; ERROR:root:Error generating model response: exception: access violation writing 0x0000000000005140, which seems to be indicative of an access violation...
1
by: PapaRatzi | last post by:
Hello, I am teaching myself MS Access forms design and Visual Basic. I've created a table to capture a list of Top 30 singles and forms to capture new entries. The final step is a form (unbound)...
1
by: CloudSolutions | last post by:
Introduction: For many beginners and individual users, requiring a credit card and email registration may pose a barrier when starting to use cloud servers. However, some cloud server providers now...
1
by: Defcon1945 | last post by:
I'm trying to learn Python using Pycharm but import shutil doesn't work
0
by: af34tf | last post by:
Hi Guys, I have a domain whose name is BytesLimited.com, and I want to sell it. Does anyone know about platforms that allow me to list my domain in auction for free. Thank you

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.