I have an export from a legacy system which i want to import into an RDBMS solution.
The export file has data as the example below
%a = (
'1' => 'test',
'3' => 'test2',
'44' => 'this is another test()
);
%b = (
'1' => 'super man'
);
%c = (
'username' => 'testing'
);
I wish to get these files into associative php array so that i can iterate into the database table, check if the values exist and save it if it does not exist.
I am stuck with the parsing of the file part.
so far i have been able to reach here
$string = file_get_contents($path);
$params = explode("%", $string); // to separate the main chunks
foreach ($params as $keyvalue) {
if ($keyvalue!='') { //as the file starts with % to truncate the first blank result
$trimmed=substr(trim($keyvalue, ' '), 3, strlen($keyvalue) - 4); // remove this part from the result 'a='
$finalArray = explode(";", $trimmed); // remove the semi column
$array = $finalArray[0];
print_r($array );
echo '<br/>';
echo '<br/>';
echo '<br/>';
echo '<br/>';
}
with the above i do get this output
( "1" => "test", "3" => "test2" ,'44' => 'this is another test())
( '1' => 'super man' )
('username' => 'testing' )
displayed on different lines, i tried json_decode (...,true), unserialise ect to convert this output into array so that i can loop with no success.
Any help will be most welcomed.
try this
$array[] = $finalArray[0];
Because you creating new array everytime. You need to append to the existing array so as to get the desired output.
And my edited code is below
$params = explode("%", $string);
$i = 0;
foreach ($params as $keyvalue) {
if ($keyvalue!='') {
$trimmed=substr(trim($keyvalue, ' '), 3, strlen($keyvalue) - 4);
$finalArray = explode(";", $trimmed);
$array = $finalArray[0];
$newArray = explode(',',substr($array, 1, -1));
foreach($newArray as $arravalue) {
$newArray1 = explode('=>',substr($arravalue, 1, -1));
$finalResultArray[$i][trim(str_replace("'","",$newArray1[0]))] = '"'.trim(str_replace("'","",$newArray1[1])).'"';
}
$i++;
}
}
It gives an output array $finalResultArray with the below format.
Array
(
[0] => Array
(
[1] => "test"
[3] => "test2"
[44] => "this is another test("
)
[1] => Array
(
[1] => "super man"
)
[2] => Array
(
[username] => "testing"
)
)
I made a little tweek with string replace to do the trick, by converting it to a json format replacing the ( by { the => by : and ) by }
it seem to work fine but if there is a neater solution I would like to welcome it.
$string = file_get_contents($path);
$params = explode("%", $string);
foreach ($params as $keyvalue) {
if ($keyvalue!='') {
$trimmed=substr(trim($keyvalue, ' '), 3, strlen($keyvalue) - 4);
$finalArray = explode(";", $trimmed);
$array = $finalArray[0];
print_r($array); // display 1
$string2 = str_replace('(', '{', $array);
$string3 = str_replace(')', '}', $string2);
$string4 = str_replace('=>', ':', $string3);
$ar = json_decode($string4);
echo ('<pre>');
//echo $first; // value
print_r( $ar);
echo('</pre>'); //display 2
die('wa');
echo '<br/>';
echo '<br/>';
echo '<br/>';
echo '<br/>';
}
}
//display 1
( "1" => "test1", "2" => "test2", "3" => "test4")
display 2
stdClass Object
(
[1] => test1
[2] => test2
[3] => test4
)
Related
I am using PHP 7.3.5 and I have the following set of array values:
$valueArr = ['-4.2%', '51.0', '90K', '0.5%', '0.74|2.6', '-1.2B', '779B', '215K', '92.2%', '42.8B', '1.49T', '1690B', '-10.8B', '0.38|3.9', '102.4', '1.00%', '0.07|1.3'];
Basically I want for each of these values the number and the "type", so if it is a percentage then I would like to get -4.2 and percentage.
I tried to create a minimum example (however the below code is no real good example ;( ), but I am stuck at the data structure level as some array keys have two inputs, such as '0.74|2.6':
<?php
$valueArr = ['-4.2%', '51.0', '90K', '0.5%', '0.74|2.6', '-1.2B', '779B', '215K', '92.2%', '42.8B', '1.49T', '1690B', '-10.8B', '0.38|3.9', '102.4', '1.00%', '0.07|1.3'];
$resArr = array();
$structureArr = array(
'value1' => "",
'number1' => "",
'value2' => "",
'number2' => ""
);
foreach ($valueArr as $key => $v) {
if (1 === preg_match('/%/', $valueArr[$key])) {
preg_match('!\d+\.*\d*!', $valueArr[$key], $structureArr['number1']);
$structureArr['value1'] = 'percentage';
}
/*
if (1 === preg_match('|', $valueArr[$key])) {
$str = explode("|", $valueArr[$key]);
$value1 = 'number';
$number1 = $str[0];
$value2 = 'number';
$number2 = $str[1];
}
if (1 === preg_match('', $valueArr[$key])) {
}
*/
array_push($resArr, $structureArr);
}
print_r($resArr);
/*
Wanted Result
Array
(
[0] => Array
(
[0] => -4.2
[1] => 'percentage'
)
[1] => Array
(
[0] => 51.0
[1] => 'number'
)
[2] => Array
(
[0] => 90000
[1] => number
)
[3] => Array
(
[0] => 0.5
[1] => percentage
)
[4] => Array
(
[0] => 0.74
[1] => number
[2] => 2.6
[3] => number
)
...
*/
I would highly appreciate your input on how to structure this array input.
Appreciate your replies!
If you join the array on a space and replace pipes | with a space, then you have a list of numbers and their symbol (if any) separated by a space. Then just match your numbers and whatever symbol comes after it. Then you just match the number index with the symbol index. I used an array to map the symbol to the word and number if none:
$string = str_replace('|', ' ', implode(' ', $valueArr));
preg_match_all('/([\d.-]+)([^\s]*)/', $string, $matches);
$types = ['%'=>'percent','K'=>'thousand','M'=>'million','B'=>'billion','T'=>'trillion'];
foreach($matches[1] as $k => $v) {
$t = $types[$matches[2][$k]] ?? 'number';
$result[] = [$v, $t];
}
This yields an array like this, with each number that was joined by a pipe with it's own element:
Array
(
[0] => Array
(
[0] => -4.2
[1] => percent
)
[1] => Array
(
[0] => 51.0
[1] => number
)
[2] => Array
(
[0] => 90
[1] => thousand
)
///etc...
If you need a floating point number then just change:
$result[] = [(float)$v, $t];
This expands on my comment. Not sure if it's the most optimal solution or not.
Rough outline...
Create array mapping suffix to multiplier. Loop through source array. explode on |. Loop through result. If last character is %, strip it, value=value and type=percentage, else, strip last char, use it as array index (if it is an available index), value=value*multiplier and type=number.
$resArr = array();
$multipliers = array("K" => 1000, "M" => 1000000, "B" => 1000000000, "T" => 1000000000000);
$valueArr = ['-4.2%', '51.0', '90K', '0.5%', '0.74|2.6', '-1.2B', '779B', '215K', '92.2%', '42.8B', '1.49T', '1690B', '-10.8B', '0.38|3.9', '102.4', '1.00%', '0.07|1.3'];
foreach($valueArr as $index => $value)
{
$parts = explode("|", $value);
$resArr[$index] = array();
foreach($parts as $part)
{
$lastChar = substr($part, -1);
if($lastChar == "%")
{
$resArr[$index][] = substr($part, 0, -1);
$resArr[$index][] = "percentage";
}
else if(in_array($lastChar, array_keys($multipliers)))
{
$multiple = $multipliers[$lastChar];
$resArr[$index][] = (substr($part, 0, -1))*$multiple;
$resArr[$index][] = "number";
}
else
{
$resArr[$index][] = $part;
$resArr[$index][] = "number";
}
}
}
var_dump($resArr);
DEMO
I have a text file which I must read, and use the data from.
3
sam 99912222
tom 11122222
harry 12299933
sam
edward
harry
How can I create an array of these strings in the following form?
array(
"name" => "number"
...
)
I tried this:
$handle = fopen("file.txt", "r");
fscanf($handle, "%d %d", $name, $number);
What then? No matter what I try, it only works for the first line.
sam 99912222
Added codes to have both types of output - ignoring and including the lines that don't have name-value pairs. Check them out below
This code goes through each line and gets only the ones that have both name and value (something[space]something)):
//$lines = ... each line of the file in an array
$vals = array();
foreach($lines as $v){
$tmp = explode(' ', $v);
if(count($tmp) > 1){
$vals[trim($tmp[0])] = trim($tmp[1]); // trim to prevent garbage
}
}
print_r($vals);
It will output this:
Array
(
[sam] => 99912222
[tom] => 11122222
[harry] => 12299933
)
See the code in action here.
If you need the values even if they didn't come in pairs, do it like this:
//$lines = ... each line of the file
$vals = array();
foreach($lines as $v){
$tmp = explode(' ', $v);
$name = '';
$number = '';
$tmp[0] = trim($tmp[0]);
if(count($tmp) > 1){
$name = $tmp[0];
$number = trim($tmp[1]);
}else{
if(is_numeric($tmp[0])){
$number = $tmp[0];
}else{
$name = $tmp[0];
}
}
$vals[] = array(
'name' => $name,
'number' => $number
);
}
print_r($vals);
And the output:
Array
(
[0] => Array
(
[name] =>
[number] => 3
)
[1] => Array
(
[name] => sam
[number] => 99912222
)
[2] => Array
(
[name] => tom
[number] => 11122222
)
[3] => Array
(
[name] => harry
[number] => 12299933
)
[4] => Array
(
[name] => sam
[number] =>
)
[5] => Array
(
[name] => edward
[number] =>
)
[6] => Array
(
[name] => harry
[number] =>
)
See the code in action here.
Data in file are inconsistent, best of is to use regex to identify what data you've got from each line.
$lines = file('file.txt'); // this will open file and split them into lines
$items = array();
foreach($lines as $line){
$name = null;
$number = null;
$nameFound = preg_match("|([A-Za-z]+)|", $line, $matches);
if($nameFound){
$name = $matches[0];
}
$numberFound = preg_match("|([0-9]+)|", $line, $matches);
if($numberFound){
$number = $matches[0];
}
$items[] = array('name' => $name, 'number' => $number);
}
Then in items you should find parsed data from file.
To make it just extract full format data just change lines with regex into one line like this:
$lines = file('file.txt'); // this will open file and split them into lines
$items = array();
foreach($lines as $line){
$userFound = preg_match("/([A-Za-z]+) ([0-9]+)/", $line, $matches);
if($userFound){
$items[$matches[1]] = $matches[2];
}
}
With the Algorithm below, you can simply parse each individual line of the Text-File Contents into an array with the 1st Word or Digit(s) on each line as the Key and the 2nd Word as the Value. When the 2nd word or group of words do not exist, a NULL is assigned to that Key. For re-usability, this algorithm has been encapsulated into a Function. Here you go:
<?php
function parseTxtFile($txtFile){
$arrTxtContent = [];
if(!file_exists($txtFile)){ return null;}
$strFWriteTxtData = file_get_contents($txtFile);
if(empty($strFWriteTxtData)){return null;}
$arrFWriteInfo = explode("\n", $strFWriteTxtData);
foreach($arrFWriteInfo as $iKey=>$lineData){
$arrWriteData = explode(", ", $lineData);
foreach($arrWriteData as $intKey=>$strKeyInfo){
preg_match("#(^[a-z0-9_A-Z]*)(\s)(.*$)#i", $strKeyInfo, $matches);
preg_match("#(^[a-z0-9_A-Z]*)(\s*)?$#i", $strKeyInfo, $matches2);
if($matches) {
list(, $key, $null, $val) = $matches;
if (!array_key_exists($key, $arrTxtContent)) {
$arrTxtContent[$key] = $val;
}else{
$iKey = $intKey + 1;
$key = $key . "_{$iKey}";
$arrTxtContent[$key] = $val;
}
}else if($matches2) {
list(, $key, $null) = $matches2;
if (!array_key_exists($key, $arrTxtContent)) {
$arrTxtContent[$key] = null;
}else{
$key = preg_match("#_\d+#", $key, $match)? $key . $match[0] : "{$key}_1";
$arrTxtContent[$key] = null;
}
}
}
}
return $arrTxtContent;
}
var_dump(parseTxtFile(__DIR__ . "/data.txt"));
Just call the function parseTxtFile($txtFile) passing it the path to your text File and it will return an Array that looks something like below:
array (size=7)
3 => null
'sam' => string '99912222' (length=8)
'tom' => string '11122222' (length=8)
'harry' => string '12299933' (length=8)
'sam_1' => null
'edward' => null
'harry_1' => null
Hope this could help a bit....
Cheers & Good-Luck ;-)
input array
$input = array (
"group_name_1" => "audi",
"group_locations_1" => "tokyo,barcelona,paris",
"group_quantities_at_locations_1" => "1,2,7",
"group_name_2" => "ford",
"group_locations_2" => "london,prag",
"group_quantities_at_locations_2" => "3,6"
);
needed output form
$target_output = array (
"audi" => array ( "tokyo" => 1, "barcelona" => 2, "paris" => 7 ),
"ford" => array ( "london" => 3, "prag" => 6 )
);
notes 1:
number of groups are dynamic (user input). For example, additional to
"audi" and "ford"; there could be also "toyota", "mercedes".
Each groups has 3 subinfo: 1-name , 2-locations and 3-quantities for
locations.
Sequences in input are always same. 1st name, 2nd locations, 3rd
quantities.
Each group has proper order number always in input. (such as
"group_name_1 or group_locations_4)
notes 2: I've read array functions again. And tried various codes but I even couldn't get close.
Can you please help me.
assuming that group_name_x, group_locations_x and group_quantities_at_locations_x keys alwas exists in your $input array
$input = array(
"group_name_1" => "audi",
"group_locations_1" => "tokyo,barcelona,paris",
"group_quantities_at_locations_1" => "1,2,7",
"group_name_2" => "ford",
"group_locations_2" => "london,prag",
"group_quantities_at_locations_2" => "3,6"
);
$new_array = array();
foreach ($input as $key => $val) {
if (strpos($key, 'group_name') !== false) {
$new_array[$val] = array();
$group_no = $key[strlen($key) - 1];
$location_array = explode(',', $input["group_locations_{$group_no}"]);
$group_quantities_array = explode(',', $input["group_quantities_at_locations_{$group_no}"]);
$new_array[$val] = array_combine($location_array, $group_quantities_array);
}
}
print_r($new_array);
output:
Array
(
[audi] => Array
(
[tokyo] => 1
[barcelona] => 2
[paris] => 7
)
[ford] => Array
(
[london] => 3
[prag] => 6
)
)
<?php
$inputs = array (
"group_name_1" => "audi",
"group_locations_1" => "tokyo,barcelona,paris",
"group_quantities_at_locations_1" => "1,2,7",
"group_name_2" => "ford",
"group_locations_2" => "london,prag",
"group_quantities_at_locations_2" => "3,6"
);
$result = array();
foreach ($inputs as $key => $value) {
if (!preg_match('/group_name_([0-9]*)/', $key, $matches)) {
continue;
}
$locations = explode(',', $inputs['group_locations_' . $matches[1]]);
$quantities = explode(',', $inputs['group_quantities_at_locations_' . $matches[1]]);
$result[$value] = array_combine($locations, $quantities);
}
echo '<pre>';
var_dump($result);
echo '</pre>';
You can simply use array_walk like as
$result = [];
$chunked = array_chunk($input,3);
array_walk($chunked,function($v) use (&$result){
$result[$v[0]] = array_combine(explode(",",$v[1]),explode(",",$v[2]));
});
print_R($result);
Demo
Little complex to explain , so here is simple concrete exemple :
array 1 :
Array
(
[4] => bim
[5] => pow
[6] => foo
)
array 2 :
Array
(
[n] => Array
(
[0] => 1
)
[m] => Array
(
[0] => 1
[1] => 2
)
[l] => Array
(
[0] => 1
[1] => 4
[2] => 64
)
And i need to output an array 3 ,
array expected :
Array
(
[bim] => n-1
[pow] => Array
(
[0] => m-1
[1] => m-2
)
[foo] => Array
(
[0] => l-1
[1] => l-4
[2] => l-64
)
Final echoing OUTPUT expected:
bim n-1 , pow m-1 m-2 ,foo l-1 l-4 l-64 ,
I tried this but seems pity:
foreach($array2 as $k1 =>$v1){
foreach($array2[$k1] as $k => $v){
$k[] = $k1.'_'.$v);
}
foreach($array1 as $res =>$val){
$val = $array2;
}
Thanks for helps,
Jess
CHALLENGE ACCEPTED
<?php
$a = array(
4 => 'bim',
5 => 'pow',
6 => 'foo',
);
$b = array(
'n' => array(1),
'm' => array(1, 2),
'l' => array(1, 4, 64),
);
$len = count($a);
$result = array();
$aVals = array_values($a);
$bKeys = array_keys($b);
$bVals = array_values($b);
for ($i = 0; $i < $len; $i++) {
$combined = array();
$key = $aVals[$i];
$prefix = $bKeys[$i];
$items = $bVals[$i];
foreach ($items as $item) {
$combined[] = sprintf('%s-%d', $prefix, $item);
};
if (count($combined) === 1) {
$combined = $combined[0];
}
$result[$key] = $combined;
}
var_dump($result);
?>
Your code may be very easy. For example, assuming arrays:
$one = Array
(
4 => 'bim',
5 => 'pow',
6 => 'foo'
);
$two = Array
(
'n' => Array
(
0 => 1
),
'm' => Array
(
0 => 1,
1 => 2
),
'l' => Array
(
0 => 1,
1 => 4,
2 => 64
)
);
You may get your result with:
$result = [];
while((list($oneKey, $oneValue) = each($one)) &&
(list($twoKey, $twoValue) = each($two)))
{
$result[$oneValue] = array_map(function($item) use ($twoKey)
{
return $twoKey.'-'.$item;
}, $twoValue);
};
-check this demo Note, that code above will not make single-element array as single element. If that is needed, just add:
$result = array_map(function($item)
{
return count($item)>1?$item:array_shift($item);
}, $result);
Version of this solution for PHP4>=4.3, PHP5>=5.0 you can find here
Update: if you need only string, then use this (cross-version):
$result = array();
while((list($oneKey, $oneValue) = each($one)) &&
(list($twoKey, $twoValue) = each($two)))
{
$temp = array();
foreach($twoValue as $item)
{
$temp[] = $twoKey.'-'.$item;
}
$result[] = $oneValue.' '.join(' ', $temp);
};
$result = join(' ', $result);
As a solution to your problem please try executing following code snippet
<?php
$a=array(4=>'bim',5=>'pow',6=>'foo');
$b=array('n'=>array(1),'m'=>array(1,2),'l'=>array(1,4,64));
$keys=array_values($a);
$values=array();
foreach($b as $key=>$value)
{
if(is_array($value) && !empty($value))
{
foreach($value as $k=>$val)
{
if($key=='n')
{
$values[$key]=$key.'-'.$val;
}
else
{
$values[$key][]=$key.'-'.$val;
}
}
}
}
$result=array_combine($keys,$values);
echo '<pre>';
print_r($result);
?>
The logic behind should be clear by reading the code comments.
Here's a demo # PHPFiddle.
//omitted array declarations
$output = array();
//variables to shorten things in the loop
$val1 = array_values($array1);
$keys2 = array_keys($array2);
$vals2 = array_values($array2);
//iterating over each element of the first array
for($i = 0; $i < count($array1); $i++) {
//if the second array has multiple values at the same index
//as the first array things will be handled differently
if(count($vals2[$i]) > 1) {
$tempArr = array();
//iterating over each element of the second array
//at the specified index
foreach($vals2[$i] as $val) {
//we push each element into the temporary array
//(in the form of "keyOfArray2-value"
array_push($tempArr, $keys2[$i] . "-" . $val);
}
//finally assign it to our output array
$output[$val1[$i]] = $tempArr;
} else {
//when there is only one sub-element in array2
//we can assign the output directly, as you don't want an array in this case
$output[$val1[$i]] = $keys2[$i] . "-" . $vals2[$i][0];
}
}
var_dump($output);
Output:
Array (
["bim"]=> "n-1"
["pow"]=> Array (
[0]=> "m-1"
[1]=> "m-2"
)
["foo"]=> Array (
[0]=> "l-1"
[1]=> "l-4"
[2]=> "l-64"
)
)
Concerning your final output you may do something like
$final = "";
//$output can be obtained by any method of the other answers,
//not just with the method i posted above
foreach($output as $key=>$value) {
$final .= $key . " ";
if(count($value) > 1) {
$final .= implode($value, " ") .", ";
} else {
$final .= $value . ", ";
}
}
$final = rtrim($final, ", ");
This will echo bim n-1, pow m-1 m-2, foo l-1 l-4 l-64.
I have a .txt file that looks like the one below:
Test = 10849831 = August 6, 2013:
56cake = 0 = August 6, 2013:
Wwe = 812986192 = August 6, 2013:
I want to explode the file above to make an array that looks like the one below:
Array
(
[Test] => Array
(
[0] => 10849831
[1] => August 6, 2013
)
[56cake] => Array
(
[0] => 0
[1] => August 6, 2013
)
[Wwe] => Array
(
[0] => 812986192
[1] => August 6, 2013
)
)
How can I accomplish this? I've already tried using something like explode(":", $data) but I don't know how to use it to do what I want above. I'm fairly new with PHP.
Just a little bit of array iteration does the trick with: explode, array_map, trim, isset and foreach.
PHP Example
$txt = <<<DOC
Test = 10849831 = August 6, 2013:
56cake = 0 = August 6, 2013:
Wwe = 812986192 = August 6, 2013:
DOC;
$Output = array();
$Lines = explode(":", $txt);
foreach($Lines as $line) {
$Rows = array_map('trim', explode(" = ", $line));
if(!isset($Rows[0], $Rows[1], $Rows[2])) continue;
$Output[$Rows[0]] = array($Rows[1], $Rows[2]);
}
print_r($Output);
PHP Code Output
Array
(
[Test] => Array
(
[0] => 10849831
[1] => August 6, 2013
)
[56cake] => Array
(
[0] => 0
[1] => August 6, 2013
)
[Wwe] => Array
(
[0] => 812986192
[1] => August 6, 2013
)
)
The Code Explained
Break up the lines with explode(":", $txt)
Loop through each line and break up each section by = again using explode
Use array_map to cycle each value and remove whitespace
Check to ensure we have 3 values with isset, if not, then skip the iteration
Push our collected values into the output array
Something like this?
<?php
// Sample text data
$text = 'Test = 10849831 = August 6, 2013:
Test = 10849831 = August 6, 2013:
56cake = 0 = August 6, 2013:
Wwe = 812986192 = August 6, 2013:';
$text = explode( PHP_EOL, $text ); // split by lines using linebreaks
// $text = explode( ":", $text ); // you can also split by lines with ':'
$arr = array(); // Our array which holding the output
// print_r( $text );
foreach( $text as $line ) { // loop through lines
$line = array_map( "trim", explode( "=", $line ) ); // split by '=' sign and do 'trim'
// print_r( $line );
if ( count( $line ) === 3 ) {
$key = strtolower( $line[0] ); // make the key lowercase (case insensitive)
$val1 = $line[1];
// process the date and remove day from date
$val2 = trim( $line[2], ":" ); // remove ':' from the end of date
$val2 = array_map( "trim", explode( " ", $val2 ) ); // split by space sign and do a 'trim'
$val2 = $val2[0]." ".$val2[2]; // join year and month parts
if ( isset( $arr[$key] ) ) { // check if the key already exists
// key exists, push new values
array_push( $arr[$key], $val1, $val2 );
continue; // key exists so continue the loop
}
// key doesn't exists in array so add the values
$arr[$key] = array( $val1, $val2 );
}
}
// there is an altwernate way to remove duplicate values using 'array_unique'
// uncomment below code if you dont want duplicate values
/*if ( !empty( $arr ) ) {
$arr = array_map( "array_unique", $arr ); // remove duplicate values from array using 'array_unique'
}*/
print_r( $arr );
?>
Here is the list of functions which i used in my code and link to those functions documentation
explode
array_map
trim
count
strtolower
isset
array_push
array_unique
Try to re-use this code:
<?php
echo '<pre>';
$result = array();
$string = 'Test = 10849831 = August 6, 2013';
$temp1 = explode("=", $string, 2);
print_r($temp1);
/*
Array
(
[0] => Test
[1] => 10849831 = August 6, 2013
)
*/
$key = $temp1[0];
$result[$key] = explode("=", $temp1[1]);
print_r($result);
/*
Array
(
[Test ] => Array
(
[0] => 10849831
[1] => August 6, 2013
)
)
*/
?>
$chunks = explode(':', $text);
$out = array();
foreach ($chunks as $key => $value){
$parts = explode('=', $value);
if (count($parts) == 3){
$out[trim($parts[0])] = array(trim($parts[1]), trim($parts[2]));
}
}
print_r($out);
You have use explode like this:
<?php
//
//Test = 10849831 = August 6, 2013:
//56cake = 0 = August 6, 2013:
//Wwe = 812986192 = August 6, 2013:
function read_data_file( $file ) {
$file_open = fopen( $file , 'r' );
$file_data = fread( $file_open , filesize( $file ) );
fclose( $file_open );
return $file_data;
}
$result_read = read_data_file('/home/josecarlos/Desktop/test.txt');
$result_explode = explode("\n", $result_read );
$final_result = array();
foreach ($result_explode as $key => $cursor){
$explode = explode("=", $cursor );
if (isset($explode[0]) && $explode[0] != ''){
$final_result[trim($explode[0])] =array (trim($explode[1]),trim($explode[2])) ;
}
}
var_dump($final_result);
The result is:
array(3) {
'Test' =>
array(2) {
[0] =>
string(8) "10849831"
[1] =>
string(15) "August 6, 2013:"
}
'56cake' =>
array(2) {
[0] =>
string(1) "0"
[1] =>
string(15) "August 6, 2013:"
}
'Wwe' =>
array(2) {
[0] =>
string(9) "812986192"
[1] =>
string(15) "August 6, 2013:"
}
}