0

I am trying to assign a variable to each keys and values of an associate array so I can use it later on in php. The key values is dynamically generated

$keys= '';
$items= '';
foreach($_POST as $key => $item) {
$keys .= $key . '<br>';
$items .= $item . '<br>';
}

when echo $keys

item1 price1 unit1 btnsave

and when echo $item

CFGreen 0.16 each EDIT

what I want is to assign a variable to each one

$item1 = item1
$price1 = price1
$init1 = unit1

and exclude the last btnsave

user2686655
  • 71
  • 1
  • 1
  • 10
  • What's wrong with an array? `$items = array();` then declare `$items[X] = array( item, price, unit );` from here you can access either value (item, price, unit) by its array number ie. `$items[1][0]` will be the item name, `$items[1][1]` will be the price and `$items[1][2]` will be the unit. – John Smith Jan 09 '23 at 11:44
  • 1
    Have you tried `$keys = array_keys($_POST); $items = array_values($_POST);` then `echo $items[0];` (prints the first item)? – CarlosCarucce Jan 09 '23 at 12:00
  • Thanx option 2 so much simpler and working 100% – user2686655 Jan 09 '23 at 13:13

0 Answers0