Solved

Dynamically created key pair php

Posted on 2013-11-27
4
364 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 43

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 43

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

Is Your Active Directory as Secure as You Think?

More than 75% of all records are compromised because of the loss or theft of a privileged credential. Experts have been exploring Active Directory infrastructure to identify key threats and establish best practices for keeping data safe. Attend this month’s webinar to learn more.

Question has a verified solution.

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

Both Easy and Powerful How easy is PHP? http://lmgtfy.com?q=how+easy+is+php (http://lmgtfy.com?q=how+easy+is+php)  Very easy.  It has been described as "a programming language even my grandmother can use." How powerful is PHP?  http://en.wikiped…
This article discusses how to create an extensible mechanism for linked drop downs.
The viewer will learn how to look for a specific file type in a local or remote server directory using PHP.
This tutorial will teach you the core code needed to finalize the addition of a watermark to your image. The viewer will use a small PHP class to learn and create a watermark.

867 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

22 Experts available now in Live!

Get 1:1 Help Now