Michal
Michal

Reputation: 3368

retrieve values of a series of $_POST keys

Let's say I have a series of $_POST values, all of them structured like $_POST['shop_1'], $_POST['shop_2'], $_POST['shop_2'] (....).

I would need to join all the values of them into a comma-separated string, so at first I have to identify them. What would be the best way to do it?

The code might look something like:

foreach ( ??array_identify??("shop_*",$_POST) as $shop )
{
  $string .= $shop.",";
}

Upvotes: 0

Views: 79

Answers (3)

rdleal
rdleal

Reputation: 1032

Try something like this:

implode(',', array_keys($_POST));

Hope it helps.

Upvotes: 0

Marc B
Marc B

Reputation: 360672

Try preg_grep:

$keys = preg_grep('/^shop_\d+$/', array_keys($_POST));
foreach($keys as $key) {
    $val = $_POST[$key];
    ...
}

Upvotes: 1

John Conde
John Conde

Reputation: 219814

Use implode() to join array values into strings:

$string = implode(',', $_POST);

Upvotes: 1

Related Questions