Jump to content

read a array from txt file


tazeha

Recommended Posts

Hello

 

How can I extract array from txt file.

$data = Array(    [0] => Array        (            [0] => AMDSempron 145            [1] => 860,000            [2] => 910,000        )    [1] => Array        (            [0] => IntelCore 2 Quad Q8200 2.33GHz 4MB L2 Cache            [1] => 1,800,000            [2] => 1,800,000        )    [2] => Array        (            [0] => IntelCore i7-920 Processor (8M Cache, 2.66 GHz, 4.80 GT/s            [1] => 3,300,000            [2] => 3,300,000        )    [3] => Array        (            [0] => IntelCore⢠i5-3450 Processor -6M Cache, up to 3.50 GHz            [1] => 5,600,000            [2] => 6,800,000        )    [4] => Array        (            [0] => Intel® Core⢠i3-530 Processor  -4M Cache, 2.93 GHz            [1] => 2,350,000            [2] => 2,380,000        )}

Then inputing array to text file.

 

This

 

file.txt

Array(    [0] => Array        (            [0] => AMDSempron 145            [1] => 860,000            [2] => 910,000        )    [1] => Array        (            [0] => IntelCore 2 Quad Q8200 2.33GHz 4MB L2 Cache            [1] => 1,800,000            [2] => 1,800,000        )    [2] => Array        (            [0] => IntelCore i7-920 Processor (8M Cache, 2.66 GHz, 4.80 GT/s            [1] => 3,300,000            [2] => 3,300,000        )    [3] => Array        (            [0] => IntelCore⢠i5-3450 Processor -6M Cache, up to 3.50 GHz            [1] => 5,600,000            [2] => 6,800,000        )    [4] => Array        (            [0] => Intel® Core⢠i3-530 Processor  -4M Cache, 2.93 GHz            [1] => 2,350,000            [2] => 2,380,000        )}

Now How Can I extracting?

I writed this code.

<?phpfile_put_contents('array.txtl', print_r($data, TRUE));$arrtxt = file_get_contents('array.txt',treu);//Select Name and Price from array $par1= array_column( $arrtxt, 0);$par2 = array_column( $arrtxt, 1);//Combine Name and Price array$c = array_combine($par1, $par2);foreach($c as $name=>$price){    echo 'Name:'.' '.$name.'<br />'.'<hr>'.PHP_EOL;    echo 'price:'.'   '.$price.'<br />'.'<hr>'.PHP_EOL;}?>

but give thise error:

 

array_column() expects parameter 1 to be array, resource given in C:wampwwwcurlindex.php on line 28

Link to comment
Share on other sites

file_get_contents() returns a string, not an array.

You have to store the data in the text file in a way that it can be converted back into an array. I sometimes use the serialize() and unserialize() functions for storing and retrieving data.

 

The way your text file is currently, there are no PHP functions that can turn that string back into an array.

Link to comment
Share on other sites

Create an account or sign in to comment

You need to be a member in order to leave a comment

Create an account

Sign up for a new account in our community. It's easy!

Register a new account

Sign in

Already have an account? Sign in here.

Sign In Now
×
×
  • Create New...