-3

How do I convert a REQUEST string into arrays in a list like the following?

$_REQUEST["InventoryData"] == sku=qty&234444=11&ShirtBig=111&ShirtSmall=101&empty=0

Array ( [0] => sku [1] => qty ) 
Array ( [0] => 234444 [1] => 11 ) 
Array ( [0] => ShirtBig [1] => 111 ) 
Array ( [0] => ShirtSmall [1] => 101 ) 
Array ( [0] => empty [1] => 0 )

This is a modification of "MASS UPDATE STOCK LEVELS IN MAGENTO – FAST" script for updating using a client side submission of data.

Jeff
  • 67
  • 1
  • 8

2 Answers2

2
$result = array();
parse_str($_REQUEST['InventoryData'], $data);
foreach ($data as $key => $value) {
    $result[] = array($key, $value);
}
cHao
  • 84,970
  • 20
  • 145
  • 172
  • `parse_str`... what a great name for a function. :D – Ry- Dec 07 '12 at 00:00
  • 2
    Yep. I was looking for a name like `http_parse_query` to go along with the complementary function `http_build_query`. PHP has so much stuff already built in; the trouble is freaking *finding* it sometimes. :) – cHao Dec 07 '12 at 00:07
  • This works except it gives me an array of arrays. I just need a list of arrays. If I print_r(array($key, $value)); I get the correct result. – Jeff Dec 07 '12 at 01:51
0

You could use the explode function to split strings into arrays by a certain character: http://php.net/manual/en/function.explode.php

However you may need to do some string manipulation to get that string into the structure you posted.

Dan Belden
  • 1,199
  • 1
  • 9
  • 20