How to convert string into an array

103 Views Asked by At

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

2

There are 2 best solutions below

0
On BEST ANSWER

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));
0
On

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') 
)