Search code examples
phparraysexplodearray-push

How to convert string into an array


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[$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


Solution

  • 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));