Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • SEARCH
  • Home
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 9234559
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 18, 20262026-06-18T06:49:08+00:00 2026-06-18T06:49:08+00:00

I read a txt file in php. The problem is I could except the

  • 0

I read a txt file in php. The problem is I could except the space from it. I used explode() function but It doesn’t work. I tried all of the ways to read but nothing right.

$a = file_get_contents($file_url, FILE_USE_INCLUDE_PATH);
$a = explode(" ",$a);//When I call this, all of value are added to $a[0] as an array
$a = explode("\n",$a[0]); // I called this to explode "enter" but it doesn't work too

And now, after call the codes above, $a[0] is “85”, $a[1] is “94 16”. When I call $a[1][0], it’s “9” instead “94” as I want.

The data at txt file is a triangle as the problem in IOI 1994:

85
94 16
15 63 72
55 78 89 105
77 24 56 93 17
...

This is the file which I want to use http://www.mediafire.com/?kjdjflvf0665q03

And this is my code to solve the problem triangle with this data from txt file

$t = array();
$count = 0;
$l = strlen($a[0]);
$d = 0;//get the quantity of rows in txt file
while($l>0){
    $d++;
    $l = $l - $d;
}
$f = array();
$prevX = $prevY = $val = 0;
//add all of values to array $t[] as a type: $t[row][col]
for($i =0 ; $i<$d; $i++){
    for($j=0; $j<=$i; $j++){
        $t[$i][$j] = $a[0][$count++];
    }
}
//start the beginning points: $f[row+1][col] = ($t[row+1][col]+MAX(f[row][col],f[row][col-1]),prevX, prevY, $t[row+1][col]) 
$f[0][0] = array($t[0][0],$prevX,$prevY, $t[0][0]);
$f[1][0] = array(($t[1][0]+$f[0][0][0]),0,0,$t[1][0]);
$f[1][1] = array(($t[1][1]+$f[0][0][0]),0,0,$t[1][1]);
$i=2;
while($i<$d){
    for ( $j=0; $j <= $i; $j++ ){
        if(($j-1)<0){//check to except the value which not undefined, but may be not works exactly
            $f[$i][$j] = array(($f[$i-1][$j][0]+$t[$i][$j]),$i-1,$j,$t[$i][$j]);
        }
        else {
            if($j>($i-1)){//check to except the value which not undefined, but may be not works exactly
                $f[$i][$j] = array(($f[$i-1][$j-1][0]+$t[$i][$j]),$i-1,$j-1,$t[$i][$j]);
            }
            else{ 
                if($f[$i-1][$j][0]<$f[$i-1][$j-1][0]){
                    $f[$i][$j] = array(($f[$i-1][$j-1][0]+$t[$i][$j]),$i-1,$j-1,$t[$i][$j]);
                }
                else if($f[$i-1][$j][0]>$f[$i-1][$j-1][0]){
                    $f[$i][$j] = array(($f[$i-1][$j][0]+$t[$i][$j]),$i-1,$j,$t[$i][$j]);
                }
            }
        }
    }
    $i++;
}
//print_r($f);
$result=0;
$x = $y = $finalR = 0;
for($j=0;$j<$d;$j++){
    if($f[$d-1][$j][0]>$result){
        $result = $f[$d-1][$j][0];
        $x = $d-1;
        $y = $j;
        $finalR = $f[$d-1][$j][3];
    }
}
while($x>0){
    echo '(',$x,',',$y,')',$f[$x][$y][3],'<----';
    $x = $f[$x][$y][1];
    $y = $f[$x][$y][2];
}
echo '(0,0)', $f[0][0][3];
  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-06-18T06:49:09+00:00Added an answer on June 18, 2026 at 6:49 am

    It’s impossible to have $a[1] as “94 16” and have $a[1][0] as 94 because $a[1] is string and the $a[1] in $a[1][0] is an array key.

    It’s easy enough to make two arrays, one for the row and one for the reference table

    <?
    $file = file('file.txt');
    foreach($file as $i => $line)
    {
        foreach(explode("\t",$line) as $num)
        {
            $num = trim($num);
            if($num!='')
            {
                $a[$i][] = $num;
            }
        }
    }
    echo '<pre>';
    pre($a);
    
    //full rows examples
    pre(get_ref('0',$a));
    pre(get_ref('1',$a));
    pre(get_ref('2',$a));
    
    //individual cells examples
    pre(get_ref('0,0',$a));
    pre(get_ref('1,0',$a));
    pre(get_ref('1,1',$a));
    pre(get_ref('2,0',$a));
    pre(get_ref('2,1',$a));
    pre(get_ref('3,2',$a));
    
    function get_ref($ref,$a)
    {
        $ref = explode(',',$ref);
        foreach($ref as $r)
        {
            $a = $a[$r];
        }
        if(is_array($a))
        {
            return(implode("\t",$a));
        }else{
            return($a);
        }
    
    }
    
    
    function pre($d)
    {
        echo '<pre>';
        print_r($d);
        echo '</pre>';
    }
    ?>
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I'm trying to load data from a .txt file with PHP, but I fail
I need a php script to read a .txt file. The content of the
I'm trying to read a txt file from a ftp server and I'm getting
Is possible to read from a txt file in loop like this? files names:
I want to read data about MMORPG characters from a .txt file and then
How can I read a .txt file from my server, and preserve it's linebreaks?
I am using PHP to read in a .txt file, and parsing the data
I have the following code: <?php $FILE=giant-data-barf.txt; $fp = fopen($FILE,'r'); //read everything into data
I'm testing a script to automate advertisement read from a txt file) i want
Hi this is my problem i want to read from a file till i

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.