473,625 Members | 2,628 Online
Bytes | Software Development & Data Engineering Community
+ Post

Home Posts Topics Members FAQ

create array of arrays

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

contact_array[] =
($_POST['contact_relati on'],
$_POST['contact_first_ name'],
$_POST['contact_last_n ame'],
$_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 2246
On Wed, 24 Mar 2004 15:35:38 -0500, Bart Nessux <ba*********@ho tmail.com>
wrote:
How is this done in php? I've tried several variations of the following:

contact_arra y[] =
($_POST['contact_relati on'],
$_POST['contact_first_ name'],
$_POST['contact_last_n ame'],
$_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_relati on'],
$_POST['contact_first_ name'],
$_POST['contact_last_n ame'],
$_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*********@ho tmail.com>
wrote:

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

contact_arr ay[] =
($_POST['contact_relati on'],
$_POST['contact_first_ name'],
$_POST['contact_last_n ame'],
$_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_relati on'],
$_POST['contact_first_ name'],
$_POST['contact_last_n ame'],
$_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_relati on'],
$_POST['contact_first_ name'],
$_POST['contact_last_n ame'],
$_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*********@ho tmail.com>
wrote:
Andy Hassall wrote:
On Wed, 24 Mar 2004 15:35:38 -0500, Bart Nessux <ba*********@ho tmail.com>
wrote:
How is this done in php? I've tried several variations of the following:

contact_arra y[] =
($_POST['contact_relati on'],
$_POST['contact_first_ name'],
$_POST['contact_last_n ame'],
$_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_relati on'],
$_POST['contact_first_ name'],
$_POST['contact_last_n ame'],
$_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_relati on'],
$_POST['contact_first_ name'],
$_POST['contact_last_n ame'],
$_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($conta ct_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*********@ho tmail.com>
wrote:

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

contact_array[] =
($_POST['contact_relati on'],
$_POST['contact_first_ name'],
$_POST['contact_last_n ame'],
$_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_relati on'],
$_POST['contact_first_ name'],
$_POST['contact_last_n ame'],
$_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_relati on'],
$_POST['contact_first_ name'],
$_POST['contact_last_n ame'],
$_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($cont act)) {
/* 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($anyarra y)
{
echo $anyarray . "<br>\n";
if (is_array($anya rray)) {
foreach($anyarr ay 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*********@ho tmail.com>
wrote:
How is this done in php? I've tried several variations of the
following:

contact_array[] =
($_POST['contact_relati on'],
$_POST['contact_first_ name'],
$_POST['contact_last_n ame'],
$_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_relati on'],
$_POST['contact_first_ name'],
$_POST['contact_last_n ame'],
$_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_relati on'],
$_POST['contact_first_ name'],
$_POST['contact_last_n ame'],
$_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($cont act)) {
/* 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($anyarra y)
{
echo $anyarray . "<br>\n";
if (is_array($anya rray)) {
foreach($anyarr ay 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*********@ho tmail.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_relati on'],
$_POST['contact_first_ name'],
$_POST['contact_last_n ame'],
$_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
1558
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
3196
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 column. Once the array elements are split, what is the best way to sort them? Thank you. //populate data object with data from xml file. //Data is a comma delimited list of values var jsData = new Array(); jsData = {lib: "#field...
104
16922
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 that array Could you show me a little example how to do this? Thanks.
5
2131
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 recognize that the created array is the same as the declared interface argument. The C# function is expecting to receive a 2 dimensioned array and is declared like this: public object InteropFunc( string s1, object arValues ); What should the ASP...
24
3436
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 declared an array as: char *stringArray = {"one","two","three","a"}; When I pass the array using:
23
7394
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 in an array. The application compiles but aborts without giving me any useful information. What I suspect is happening is infinite recursion. Each Directory object creates an array of Subdirectories each of which has an array of...
17
7239
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 to show the array data to the end user. Can I do that? How?
2
2239
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 code is to create
4
5416
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 putting in arrays.
0
8251
marktang
by: marktang | last post by:
ONU (Optical Network Unit) is one of the key components for providing high-speed Internet services. Its primary function is to act as an endpoint device located at the user's premises. However, people are often confused as to whether an ONU can Work As a Router. In this blog post, we’ll explore What is ONU, What Is Router, ONU & Router’s main usage, and What is the difference between ONU and Router. Let’s take a closer look ! Part I. Meaning of...
0
8182
by: Hystou | last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can effortlessly switch the default language on Windows 10 without reinstalling. I'll walk you through it. First, let's disable language synchronization. With a Microsoft account, language settings sync across devices. To prevent any complications,...
0
8688
Oralloy
by: Oralloy | last post by:
Hello folks, I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>". The problem is that using the GNU compilers, it seems that the internal comparison operator "<=>" tries to promote arguments from unsigned to signed. This is as boiled down as I can make it. Here is my compilation command: g++-12 -std=c++20 -Wnarrowing bit_field.cpp Here is the code in...
0
8635
jinu1996
by: jinu1996 | last post by:
In today's digital age, having a compelling online presence is paramount for businesses aiming to thrive in a competitive landscape. At the heart of this digital strategy lies an intricately woven tapestry of website design and digital marketing. It's not merely about having a website; it's about crafting an immersive digital experience that captivates audiences and drives business growth. The Art of Business Website Design Your website is...
0
8494
tracyyun
by: tracyyun | last post by:
Dear forum friends, With the development of smart home technology, a variety of wireless communication protocols have appeared on the market, such as Zigbee, Z-Wave, Wi-Fi, Bluetooth, etc. Each protocol has its own unique characteristics and advantages, but as a user who is planning to build a smart home system, I am a bit confused by the choice of these technologies. I'm particularly interested in Zigbee because I've heard it does some...
0
4085
by: TSSRALBI | last post by:
Hello I'm a network technician in training and I need your help. I am currently learning how to create and manage the different types of VPNs and I have a question about LAN-to-LAN VPNs. The last exercise I practiced was to create a LAN-to-LAN VPN between two Pfsense firewalls, by using IPSEC protocols. I succeeded, with both firewalls in the same network. But I'm wondering if it's possible to do the same thing, with 2 Pfsense firewalls...
0
4188
by: adsilva | last post by:
A Windows Forms form does not have the event Unload, like VB6. What one acts like?
1
1800
muto222
by: muto222 | last post by:
How can i add a mobile payment intergratation into php mysql website.
2
1496
bsmnconsultancy
by: bsmnconsultancy | last post by:
In today's digital era, a well-designed website is crucial for businesses looking to succeed. Whether you're a small business owner or a large corporation in Toronto, having a strong online presence can significantly impact your brand's success. BSMN Consultancy, a leader in Website Development in Toronto offers valuable insights into creating effective websites that not only look great but also perform exceptionally well. In this comprehensive...

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.