Solved

Dynamically created key pair php

Posted on 2013-11-27
4
367 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
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
  • 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

Technology Partners: We Want Your Opinion!

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!

Question has a verified solution.

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

This article discusses four methods for overlaying images in a container on a web page
Since pre-biblical times, humans have sought ways to keep secrets, and share the secrets selectively.  This article explores the ways PHP can be used to hide and encrypt information.
The viewer will learn how to count occurrences of each item in an array.
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 …

735 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