search value (with regex) in array and push search value to multidimensional array in PHP

Example Data

Array
(
    [0] => [DATA_1_A]
    [1] => [DATA_1_B] [DATA_2_B]
    [2] => [DATA_1_C] [DATA_2_C] [DATA_3_C]
)

And push value to multidimensional array.

Array
(
    [0] => Array
        (
            [0] => DATA_1_A
        )
    [1] => Array
        (
            [0] => DATA_1_B
            [1] => DATA_2_B
        )
    [2] => Array
        (
            [0] => DATA_1_C
            [1] => DATA_2_C
            [2] => DATA_3_C
        )
)

$new = array_map(function($i) {
if(preg_match_all('/\[([^\]]+)\]/', $i, $m)) return $m[1];
return $i; }, $arr);

http://stackoverflow.com/a/37666969

Published by

sornram9254

Ayutthaya Technical College/Voc.Cert.🛠️ | KMUTNB/B.S.Tech.Ed.⚙️ | Information Security Engineer 👨🏻‍💻| Penetration Tester👨🏻‍💻 | COYG🔴 Milan🔴⚫️ | Taylor Swift👩‍🎤 | ติ่งซีรีส์ญี่ปุ่น 🇯🇵

Leave a Reply

Your email address will not be published. Required fields are marked *

This site uses Akismet to reduce spam. Learn how your comment data is processed.