Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

PHP explode and assign it to a multi-dimensional array

i want to explode a string two time and make a multi-dimensional array.

$data = "i love funny movies \n i love stackoverflow dot com \n i like rock song";
$data = explode("\n", $data);

so a print_r($data); will output:

Array
(
    [0] => i love funny movies 
    [1] =>  i love stackoverflow com 
    [2] =>  i like rock song
)

now if i do like this:

$line_data = explode(" ", $data); // explode $data variable by spaces.

a print_r($line_data); will give me this:

Array
(
    [0] => i
    [1] => love
    [2] => funny
    [3] => movies
    [4] => 
    [5] => i
    [6] => love
    [7] => stackoverflow
    [8] => dot
    [9] => com
    [10] => 
    [11] => i
    [12] => like
    [13] => rock
    [14] => song
)

but what i want to achive will look like this:

Array
(
    [0][0] => i
    [0][1] => love
    [0][2] => funny
    [0][3] => movies
    [0][4] => 
    [1][5] => i
    [1][6] => love
    [1][7] => stackoverflow
    [1][8] => dot
    [1][9] => com
    [1][10] => 
    [2][11] => i
    [2][12] => like
    [2][13] => rock
    [2][14] => song
)

here, the first index should represent the line number and second index will represent the word number.
How to explode the string to have such array?

like image 391
rakibtg Avatar asked Dec 15 '22 02:12

rakibtg


2 Answers

Do it:

$data = "i love funny movies \n i love stackoverflow dot com \n i like rock song";
$data = explode(" \n ", $data);

$out = array();
$step = 0;
$last = count($data);
$last--;

foreach($data as $key=>$item){

   foreach(explode(' ',$item) as $value){
    $out[$key][$step++] = $value;
   }

   if ($key!=$last){
    $out[$key][$step++] = ' '; // not inserting last "space"
   }

}

print '<pre>';
print_r($out);
print '</pre>';

OUTPUT (what you need):

Array
(
  [0] => Array
    (
        [0] => i
        [1] => love
        [2] => funny
        [3] => movies
        [4] =>  
    )

[1] => Array
    (
        [5] => i
        [6] => love
        [7] => stackoverflow
        [8] => dot
        [9] => com
        [10] =>  
    )

[2] => Array
    (
        [11] => i
        [12] => like
        [13] => rock
        [14] => song
    )

)
like image 198
voodoo417 Avatar answered Feb 04 '23 16:02

voodoo417


First, explode the string by newline. Now you have an array. Iterate over the values of this array and explode each value by space. The following code does exactly this:

$data = "i love funny movies \n i love stackoverflow dot com \n i like rock song";
$data = explode("\n ", $data);
$num = 0;
foreach($data as &$val){
$exp = explode(" ", $val);
$val = array_combine(range($num, $num+sizeof($exp)-1), $exp);
$num += sizeof($exp);
}
print_r($data);

Alternative solution with array_search and array_slice:

$data = "i love funny movies \n i love stackoverflow dot com \n i like rock song";
$data = explode(" ", $data);
$arr = array();
while($search = array_search("\n", array_values($data))){
$arr[] = array_merge(array_slice($data, 0, $search, true), array(""));
$data = array_slice($data, $search+1, null, true);
}
$data = array_merge($arr, array($data));
print_r($data);

The output will be:

Array
(
    [0] => Array
        (
            [0] => i
            [1] => love
            [2] => funny
            [3] => movies
            [4] => 
        )

    [1] => Array
        (
            [5] => i
            [6] => love
            [7] => stackoverflow
            [8] => dot
            [9] => com
            [10] => 
        )

    [2] => Array
        (
            [11] => i
            [12] => like
            [13] => rock
            [14] => song
        )

)
like image 42
Sharanya Dutta Avatar answered Feb 04 '23 16:02

Sharanya Dutta