Avatar of debbieau1
debbieau1
Flag for United States of America asked on

Dynamically created key pair php

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
PHP

Avatar of undefined
Last Comment
debbieau1

8/22/2022 - Mon
Chris Stanyon

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?
debbieau1

ASKER
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.
ASKER CERTIFIED SOLUTION
Chris Stanyon

THIS SOLUTION ONLY AVAILABLE TO MEMBERS.
View this solution by signing up for a free trial.
Members can start a 7-Day free trial and enjoy unlimited access to the platform.
See Pricing Options
Start Free Trial
GET A PERSONALIZED SOLUTION
Ask your own question & get feedback from real experts
Find out why thousands trust the EE community with their toughest problems.
debbieau1

ASKER
Thanks very much for such a quick and clear response.
Experts Exchange has (a) saved my job multiple times, (b) saved me hours, days, and even weeks of work, and often (c) makes me look like a superhero! This place is MAGIC!
Walt Forbes