Jump to content

Explode problem


sandy1028

Recommended Posts

Hi,

 

I am not able to explode further  what is the problem.

 

data.txt

User1 : 0 : 1 : 2|user2 : 132 : 133 : 134|User3 : 223 : 233 : 245|User4 : 334 : 39: 334|User5 : 423 : 433: 435|User6 : 52 : 53: 55|User7 : 633 : 633: 634|User8 : 623 : 7: 74|User9 : 83 : 853: 845|User10 : 93 : 923 : 945|User11 : 623 : 623: 645|User12 : 23 : 23 : 45|User13 : 23 : 23 : 45|User14 : 23 : 23 : 45|User15 : 23 : 23 : 45|User16 : 23 : 23 : 45|User17 : 23 : 23 : 45|User18 : 23 : 23 : 45|User19 : 23 : 23 : 45|User20 : 23 : 23 : 45|User21 : 23 : 23 : 45|User22 : 23 : 23 : 45|User23 : 23 : 23 : 45|User24 : 23 : 23 : 45

 

$data="data.txt";
foreach($data as $line){
$text_line=explode("|",$line);
foreach($text_line as $text){
$tex=explode(":",$text);
foreach($tex as $t){
$te=explode(" ",$t);
}
}
}


Link to comment
https://forums.phpfreaks.com/topic/66843-explode-problem/
Share on other sites

Try the attached code below. It'll load each user into their own separate array, example:

    [user1] => Array
        (
            [0] => 0
            [1] => 1
            [2] => 2
        )

    [user2] => Array
        (
            [0] => 132
            [1] => 133
            [2] => 134
        )

The code:

<?php

$data = file("data.txt");

foreach($data as $line)
{
    $text_line = explode("|",$line);

    foreach($text_line as $text)
    {
        list($user, $col1, $col2, $col3) = array_map('trim', explode(":", $text));

        $users[$user] = array($col1, $col2, $col3);

    }
}

echo '<pre>' . print_r($users, true) . '</pre>';

?>

Link to comment
https://forums.phpfreaks.com/topic/66843-explode-problem/#findComment-335164
Share on other sites

Archived

This topic is now archived and is closed to further replies.

×
×
  • Create New...

Important Information

We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue.