There is a form that cannot be changed.
It passes variables to POST.
Example

array(20) { ["posted"]=> string(4) "true" ["platel"]=> string(4) "test" ["address"]=> string(24) "Tomsk Vershinina 39a 527" ["mobil"]=> string(1) "1" ["email"]=> string(17) "votanko@gmail.com" ["goods1"]=> string(8) "product1" ["kod1"]=> string(6) "634000" ["qantity1"]=> string(2) "10" ["price1"]=> string(4) "1000" ["comments1"]=> string(3) "Red" ["goods2"]=> string(8) "product2" ["kod2"]=> string(6) "634000" ["qantity2"]=> string(2) "20" ["price2"]=> string(4) "2000" ["comments2"]=> string(4) "Blue" ["dostavka"]=> string(1) "1" ["transport_kompaniya"]=> string(0) "" ["moskaw_address"]=> string(0) "" ["oplata"]=> string(1) "1" ["Submit"]=> string(9) "Отправить" } 

How can I find the number of all elements of the category $ _POST ['goods'].
That is, in this example, there are two $ _POST ['goods1'] $ _POST ['goods2']

    1 answer 1

    Based on similar questions here and here :

    1. With the help of foreach we go through the $_POST array and with the help of preg_match check whether the key is suitable for the condition:

       $count = 0; foreach (array_keys($_POST) as $k) { if (preg_match('/^goods(\d+)$/', $k, $matches)) { $count++; } } 
    2. We get all the keys using array_keys and find those that match the pattern using preg_grep:

       $keysAll = array_keys($_POST); $keysPattern = preg_grep('/^goods(\d+)$/', $keysAll); $count = count($keysPattern); 
    • Thank. It helped. - votanko
    • the bulkhead can not be done? $ _POST ['goods1'] $ _POST ['goods0'] $ _POST ['goods2'] $ _POST ['goods3'] in PCP there is no such thing to substitute numbers - votanko
    • @votanko, why not? in the cycle "goods {$ id}" for example, and $ id changes. or what is meant? - Evgenii Izhboldin
    • $ _POST ['goods {$ id}'] - that is, like this? - votanko
    • @votanko, $ _POST ["goods {$ id}"], in double it is necessary, if with substitution of variables. - Evgenii Izhboldin