Solved

Is there a way to combine each element of a multidimensional array with another array in php?

Posted on 2010-11-28
8
262 Views
Last Modified: 2012-06-27
I have a multidimensional array which is a set of exploded numbers .ie

3-4-7
2-5-1
1-2-3
They have been exploded using "-" delimiter to input into  a database using a form that I copy and paste the numbers from a spreadsheet. I can load them individually or as a set. just fine. However I need put them into the database combined with another array  of letters ie:

Column 1          Column2
A                          347
B                          251
C                          123

The letters array are the name of tables in the database and the numbers are being inserted with each number a different field. Again I can explode and load them fine, but I want to be able to take the letter array, and copy the numbers into the form and have the numbers load into the database based on the names in the letter array. Any ideas would be appreciated.

0
Comment
Question by:phillyrob817
  • 4
  • 4
8 Comments
 
LVL 109

Expert Comment

by:Ray Paseur
ID: 34226036
It looks like you have two arrays there - you can use an iterator like foreach() to walk the arrays.
0
 
LVL 109

Expert Comment

by:Ray Paseur
ID: 34226043
It would probably be helpful to see your CREATE TABLE statements and a little "before-and-after" example.

This shows how the iterators work.
foreach ($column1 as $key => $table_name)
{
    echo $column2[$key];
}

Open in new window

0
 

Author Comment

by:phillyrob817
ID: 34226329
Thanks for the reply.

The table names  in the db are A, B, C  and in each table the fields are: id, date, number_1, number_2 ,number_3.

I take numbers from a spreadsheet  and copy them into a text box  like so

1-2-3
4-5-6
7-8-9 ect  
up to 20 rows of numbers. My script will explode  those number delimiting on the "-"  and upload each number into the proper fiield, but I first must select each table, then copy/paste into form then hit submit to upload. I have to do each table separately. I am looking to have an array with the each table name, and when i copy the numbers, have each row be associated with each element of the array (table name) ie

table A  matched with first pasted number 1-2-3
table B with second 4-5-6 and so on.


0
What is SQL Server and how does it work?

The purpose of this paper is to provide you background on SQL Server. It’s your self-study guide for learning fundamentals. It includes both the history of SQL and its technical basics. Concepts and definitions will form the solid foundation of your future DBA expertise.

 
LVL 109

Accepted Solution

by:
Ray Paseur earned 250 total points
ID: 34226406
This looks like it could benefit from a lot of automation.  Instead of cutting numbers from a spreadsheet and pasting into a text box, try writing the spreadsheet output into a file.  You can probably save the spreadsheet as a ".csv" file.  Then when you read the spreadsheet in with fgetcsv() you will have an array of numbers (corresponding to the cells of the spreadsheet).  You can store the first row into table A, the second into table B, etc.
0
 

Author Comment

by:phillyrob817
ID: 34226705
I have never used  fgetcsv(), so you have sent me down a new path that may the a very easy solution to all the code I have written. Thx I will let you know  shortly!
0
 

Author Closing Comment

by:phillyrob817
ID: 34226812
This helps a lot, but leads to another question. Does not solve issue.
0
 
LVL 109

Expert Comment

by:Ray Paseur
ID: 34226951
Yeah, the reason it does not solve the issue is because you have not given EE adequate test data. Take some time to learn about "TDD" -- it will make your work much easier.
0
 

Author Comment

by:phillyrob817
ID: 34227275
ok thx  I will.
0

Featured Post

PRTG Network Monitor: Intuitive Network Monitoring

Network Monitoring is essential to ensure that computer systems and network devices are running. Use PRTG to monitor LANs, servers, websites, applications and devices, bandwidth, virtual environments, remote systems, IoT, and many more. PRTG is easy to set up & use.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Does the idea of dealing with bits scare or confuse you? Does it seem like a waste of time in an age where we all have terabytes of storage? If so, you're missing out on one of the core tools in every professional programmer's toolbox. Learn how to …
When table data gets too large to manage or queries take too long to execute the solution is often to buy bigger hardware or assign more CPUs and memory resources to the machine to solve the problem. However, the best, cheapest and most effective so…
Learn how to match and substitute tagged data using PHP regular expressions. Demonstrated on Windows 7, but also applies to other operating systems. Demonstrated technique applies to PHP (all versions) and Firefox, but very similar techniques will w…
The viewer will learn how to create a basic form using some HTML5 and PHP for later processing. Set up your basic HTML file. Open your form tag and set the method and action attributes.: (CODE) Set up your first few inputs one for the name and …

830 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question