开发者

How to convert string into an array

开发者 https://www.devze.com 2023-02-12 05:21 出处:网络
I have an array something like this $arr1 = array( \'0\' => \'674534856|213123213|232313123\', \'1\' => \'349578449|782374879|232313123\'

I have an array something like this

$arr1 = array(
    '0' => '674534856|213123213|232313123',
    '1' => '349578449|782374879|232313123'
);

I loop through the arr1 array,

for ($x=0; $x < $count; $x++) {
    $check = explode("|", $arr1开发者_Python百科[$x]);
    array_pop($check);

    $count_check = count($check);

    for ($z=0; $z < $count_check; $z++) {
        array_push($result, $check[$z]); 
    }  
}

It's not working as expected. Any help appreciated. Thanks.

EDIT $result is result array


Just implode() everything in your input array with the same delimiter to flatten it to a single string, and then explode() by that delimiter:

$result = explode('|', implode('|', $arr1));


Try

$result = explode('|', join('|', $arr1));

// outputs
array('674534856', '213123213', '232313123', '349578449', '782374879', '232313123') 

Or

$result = array_map(function($temp) { return explode('|', $temp); }, $arr1);

// outputs
array(
    [0] => array('674534856', '213123213', '232313123'), 
    [1] => array('349578449', '782374879', '232313123') 
)
0

精彩评论

暂无评论...
验证码 换一张
取 消

关注公众号