Solved

Dynamically created key pair php

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

Free Tool: IP Lookup

Get more info about an IP address or domain name, such as organization, abuse contacts and geolocation.

One of a set of tools we are providing to everyone as a way of saying thank you for being a part of the community.

Question has a verified solution.

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

Suggested Solutions

Title # Comments Views Activity
What does $selected = $month == $beginning_month ? 'selected' : ''; mean? 5 40
Number of hours between date in DB and now 8 21
Dynamic Dropdowns 15 32
Ahax pagination 9 32
Popularity Can Be Measured Sometimes we deal with questions of popularity, and we need a way to collect opinions from our clients.  This article shows a simple teaching example of how we might elect a favorite color by letting our clients vote for …
Introduction This article is intended for those who are new to PHP error handling (https://www.experts-exchange.com/articles/11769/And-by-the-way-I-am-New-to-PHP.html).  It addresses one of the most common problems that plague beginning PHP develop…
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 look for a specific file type in a local or remote server directory using PHP.

839 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