Solved

Dynamically created key pair php

Posted on 2013-11-27
4
363 Views
Last Modified: 2013-11-27
I have a foreach loop and need to dynamically create the key value pairs and build the array.  
After running the foreach loop, it should produce something like

  $sampleArray = array(
      'firstfield' => (string)"Person",
     'thirdfield' => (integer)'Quantity',
     'fourthfield' => (string)'Product'
  );

Open in new window


I need to know how to use foreach loop and build the sample array like above
0
Comment
Question by:debbieau1
  • 2
  • 2
4 Comments
 
LVL 42

Expert Comment

by:Chris Stanyon
ID: 39680348
Not sure what you mean - you need something to loop through with the foreach loop (such as an array)

What exactly do you want to run the foreach loop on?
0
 
LVL 1

Author Comment

by:debbieau1
ID: 39680494
Hi Chris

I have values coming from a database table and I need to save these into another table.  

 I have a foreach loop which iterates through.  I want to merge these so it looks like the sample below.  I had problems with each (key value pair replacing, rather than combining them into one array  

       foreach($test as $k=>$sampleEntry)
      {

the key value pair was in here  
      }
      

The goal is to combine the ones from the loop into one array like below.  

$sampleArray = array(
      'firstfield' => (string)"Person",
     'thirdfield' => (integer)'Quantity',
     'fourthfield' => (string)'Product'
  );

Then i will save the $sampleArray to db.  

I hope I am being clear enough.  Please let me know if not.  Thank you very much.
0
 
LVL 42

Accepted Solution

by:
Chris Stanyon earned 500 total points
ID: 39680510
Without seeing data or code it's difficult to guess what you need, but if you want to add new values to an array in a loop then you need to include the square brackets to indicate it's a new element within the array, otherwise you'll just overwrite what you already have:

Here's an idea for you to play with:

$sampleArray = array();

foreach($test as $k=>$sampleEntry):
   $sampleArray[$k] = $sampleEntry;
endforeach;

var_dump($sampleArray);

Open in new window

0
 
LVL 1

Author Closing Comment

by:debbieau1
ID: 39680536
Thanks very much for such a quick and clear response.
0

Featured Post

How to run any project with ease

Manage projects of all sizes how you want. Great for personal to-do lists, project milestones, team priorities and launch plans.
- Combine task lists, docs, spreadsheets, and chat in one
- View and edit from mobile/offline
- Cut down on emails

Join & Write a Comment

Things That Drive Us Nuts Have you noticed the use of the reCaptcha feature at EE and other web sites?  It wants you to read and retype something that looks like this.Insanity!  It's not EE's fault - that's just the way reCaptcha works.  But it is …
Password hashing is better than message digests or encryption, and you should be using it instead of message digests or encryption.  Find out why and how in this article, which supplements the original article on PHP Client Registration, Login, Logo…
Explain concepts important to validation of email addresses with regular expressions. Applies to most languages/tools that uses regular expressions. Consider email address RFCs: Look at HTML5 form input element (with type=email) regex pattern: T…
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 …

743 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

Need Help in Real-Time?

Connect with top rated Experts

15 Experts available now in Live!

Get 1:1 Help Now