0

I want show all post variables if name of variables has start add-

for example: this is a full string with post variables:

Array ( [PartNumber] => sfd [Description] => dsf [Issue] => dfs [Customer] => J.C.B. SERVICE [widget7-table_length] => 5 [add_332/F2684] => [add_333/D1641] => sdf [add_333/D1202] => [add_332/F3144] => sdf [add_332/F3147] => sfd [makeassy] => )

I want to display in array only

[add_332/F2684] => [add_333/D1641] => sdf [add_333/D1202] => [add_332/F3144] => sdf [add_332/F3147] => sfd

I'm trying

print_r($_POST['add_%']);

But, how you see this post, it's not working. Do you have any ideas ?

Rahul
  • 18,271
  • 7
  • 41
  • 60
Dashiphone
  • 59
  • 1
  • 9
  • 1
    Does this answer your question? [PHP - get all keys from a array that start with a certain string](https://stackoverflow.com/questions/4979238/php-get-all-keys-from-a-array-that-start-with-a-certain-string) – Devsi Odedra Nov 05 '19 at 10:22

3 Answers3

1

Use array_filter to extract specific array keys:

$output = array_filter($_POST, function($e) {
    return strpos($e, 'add_') === 0;
}, ARRAY_FILTER_USE_KEY);
kaczmen
  • 528
  • 4
  • 12
0

You coudl try using preg_grep()

$res= preg_grep ('/^add_ (\w+)/i', $_POST);

var_dump($res);
treyBake
  • 6,440
  • 6
  • 26
  • 57
ScaisEdge
  • 131,976
  • 10
  • 91
  • 107
0

Another Solution with Foreach

<?php
    $x = array(
         'PartNumber' => 'sfd',
         'Description' => 'dsf',
         'add_332/F2684' =>'asd', 
         'add_333/D1641' =>'sdf' ,
         'add_333/D1202' => 'asd',
         'TESTEST' => 'ASDASD' );

    $tmp = array();
    foreach($x as $key => $value)
    {
        if(substr( $key, 0, 3 ) === "add")
        {
            array_push($tmp,array($key =>$value));
        }
    }
    die(print_r($tmp));
?>

Output:

Array
(
    [0] => Array
        (
            [add_332/F2684] => asd
        )

[1] => Array
    (
        [add_333/D1641] => sdf
    )

[2] => Array
    (
        [add_333/D1202] => asd
    )

)
1
MThiele
  • 387
  • 1
  • 9