Reading values from a file which contains associative array PHP









up vote
0
down vote

favorite
1












I need to read the content of a file called orders.log with PHP and use the variables. The log file is stored like this:



Array
(
[time] => 2099-99-99 00:00:00
[gateway] => Paypal
[gatewayOK] => Yes
[gatewayTransactionId] => XXXXXXX
[POST] => Array
(
[mc_gross] => 9.99
[protection_eligibility] => Eligible
[address_status] => confirmed
[payer_id] => XXXXX
[address_street] => XXXXX
[payment_date] => 00:00:00 Nov 11, 2018 PDT
[payment_status] => Completed
[charset] => windows-1252

)

)


I have tried reading it like this:



<?php
$orders=file_get_contents("orders.log");
echo $orders['time'];
echo $myarray[0]['gateway'];
echo $myarray[1]['mc_gross'];
?>


But the result does not work like intended. It throws "A" and "r" . Any help would be appreciated.










share|improve this question





















  • Is the content of the file literally as you show? Can you change this and save it as something useful like JSON instead?
    – Nigel Ren
    Nov 11 at 17:12










  • yes sadly I need to read the file as it is. I can not parse it to JSON or any other format.
    – Bijay Regmi
    Nov 11 at 17:13










  • Is every record in the same format - as in exactly the same fields on the same line?
    – Nigel Ren
    Nov 11 at 17:15










  • yes it is. Just another array set that declares another order.
    – Bijay Regmi
    Nov 11 at 17:16














up vote
0
down vote

favorite
1












I need to read the content of a file called orders.log with PHP and use the variables. The log file is stored like this:



Array
(
[time] => 2099-99-99 00:00:00
[gateway] => Paypal
[gatewayOK] => Yes
[gatewayTransactionId] => XXXXXXX
[POST] => Array
(
[mc_gross] => 9.99
[protection_eligibility] => Eligible
[address_status] => confirmed
[payer_id] => XXXXX
[address_street] => XXXXX
[payment_date] => 00:00:00 Nov 11, 2018 PDT
[payment_status] => Completed
[charset] => windows-1252

)

)


I have tried reading it like this:



<?php
$orders=file_get_contents("orders.log");
echo $orders['time'];
echo $myarray[0]['gateway'];
echo $myarray[1]['mc_gross'];
?>


But the result does not work like intended. It throws "A" and "r" . Any help would be appreciated.










share|improve this question





















  • Is the content of the file literally as you show? Can you change this and save it as something useful like JSON instead?
    – Nigel Ren
    Nov 11 at 17:12










  • yes sadly I need to read the file as it is. I can not parse it to JSON or any other format.
    – Bijay Regmi
    Nov 11 at 17:13










  • Is every record in the same format - as in exactly the same fields on the same line?
    – Nigel Ren
    Nov 11 at 17:15










  • yes it is. Just another array set that declares another order.
    – Bijay Regmi
    Nov 11 at 17:16












up vote
0
down vote

favorite
1









up vote
0
down vote

favorite
1






1





I need to read the content of a file called orders.log with PHP and use the variables. The log file is stored like this:



Array
(
[time] => 2099-99-99 00:00:00
[gateway] => Paypal
[gatewayOK] => Yes
[gatewayTransactionId] => XXXXXXX
[POST] => Array
(
[mc_gross] => 9.99
[protection_eligibility] => Eligible
[address_status] => confirmed
[payer_id] => XXXXX
[address_street] => XXXXX
[payment_date] => 00:00:00 Nov 11, 2018 PDT
[payment_status] => Completed
[charset] => windows-1252

)

)


I have tried reading it like this:



<?php
$orders=file_get_contents("orders.log");
echo $orders['time'];
echo $myarray[0]['gateway'];
echo $myarray[1]['mc_gross'];
?>


But the result does not work like intended. It throws "A" and "r" . Any help would be appreciated.










share|improve this question













I need to read the content of a file called orders.log with PHP and use the variables. The log file is stored like this:



Array
(
[time] => 2099-99-99 00:00:00
[gateway] => Paypal
[gatewayOK] => Yes
[gatewayTransactionId] => XXXXXXX
[POST] => Array
(
[mc_gross] => 9.99
[protection_eligibility] => Eligible
[address_status] => confirmed
[payer_id] => XXXXX
[address_street] => XXXXX
[payment_date] => 00:00:00 Nov 11, 2018 PDT
[payment_status] => Completed
[charset] => windows-1252

)

)


I have tried reading it like this:



<?php
$orders=file_get_contents("orders.log");
echo $orders['time'];
echo $myarray[0]['gateway'];
echo $myarray[1]['mc_gross'];
?>


But the result does not work like intended. It throws "A" and "r" . Any help would be appreciated.







php arrays






share|improve this question













share|improve this question











share|improve this question




share|improve this question










asked Nov 11 at 17:10









Bijay Regmi

658




658











  • Is the content of the file literally as you show? Can you change this and save it as something useful like JSON instead?
    – Nigel Ren
    Nov 11 at 17:12










  • yes sadly I need to read the file as it is. I can not parse it to JSON or any other format.
    – Bijay Regmi
    Nov 11 at 17:13










  • Is every record in the same format - as in exactly the same fields on the same line?
    – Nigel Ren
    Nov 11 at 17:15










  • yes it is. Just another array set that declares another order.
    – Bijay Regmi
    Nov 11 at 17:16
















  • Is the content of the file literally as you show? Can you change this and save it as something useful like JSON instead?
    – Nigel Ren
    Nov 11 at 17:12










  • yes sadly I need to read the file as it is. I can not parse it to JSON or any other format.
    – Bijay Regmi
    Nov 11 at 17:13










  • Is every record in the same format - as in exactly the same fields on the same line?
    – Nigel Ren
    Nov 11 at 17:15










  • yes it is. Just another array set that declares another order.
    – Bijay Regmi
    Nov 11 at 17:16















Is the content of the file literally as you show? Can you change this and save it as something useful like JSON instead?
– Nigel Ren
Nov 11 at 17:12




Is the content of the file literally as you show? Can you change this and save it as something useful like JSON instead?
– Nigel Ren
Nov 11 at 17:12












yes sadly I need to read the file as it is. I can not parse it to JSON or any other format.
– Bijay Regmi
Nov 11 at 17:13




yes sadly I need to read the file as it is. I can not parse it to JSON or any other format.
– Bijay Regmi
Nov 11 at 17:13












Is every record in the same format - as in exactly the same fields on the same line?
– Nigel Ren
Nov 11 at 17:15




Is every record in the same format - as in exactly the same fields on the same line?
– Nigel Ren
Nov 11 at 17:15












yes it is. Just another array set that declares another order.
– Bijay Regmi
Nov 11 at 17:16




yes it is. Just another array set that declares another order.
– Bijay Regmi
Nov 11 at 17:16












1 Answer
1






active

oldest

votes

















up vote
1
down vote



accepted










This assumes that each entry is 20 lines long, it reads in the log file and the splits it into 20 segments using array_chunk().



It then processes each segment, first splitting the lines by the => using explode() and adding the values to an associative array with the left hand side as the key. You can then use the key to access each value.



$input = file("log.txt", FILE_IGNORE_NEW_LINES);

$orders = array_chunk($input, 20);
foreach ( $orders as $order )
$split = ;
foreach ( $order as $line )
$info = explode("=>", $line);
if ( count($info) == 2)
$split[trim($info[0]," t")] = trim ($info[1]);



echo "gateway-".$split['gateway'].PHP_EOL;
echo "mc_gross-".$split['mc_gross'].PHP_EOL;



If you wanted a list of all orders...



$input = file("log.txt", FILE_IGNORE_NEW_LINES);

$orders = array_chunk($input, 20);
$orderList = ;
foreach ( $orders as $order )
$split = ;
foreach ( $order as $line )
$info = explode("=>", $line);
if ( count($info) == 2)
$split[trim($info[0]," t")] = trim ($info[1]);


$orderList = $split;



echo "gateway-".$orderList[0]['gateway'].PHP_EOL;
echo "mc_gross-".$orderList[0]['mc_gross'].PHP_EOL;


A third way which doesn't rely on the data being all the same format, this reads on a line by line basis and tries to work out the end of an element itself (just a line containing ))...



$fp = fopen("log.txt", "r");
$orderList = ;
$order = ;
while ( $line = fgets($fp))
// Remove extra data after content
$line = rtrim($line);
// If end of order (a line just starting with a ')')
if ( $line == ')' )
// Convert order into associative array
$split = ;
foreach ( $order as $line )
$info = explode("=>", $line);
if ( count($info) == 2)
$split[trim($info[0]," t")] = trim ($info[1]);


// Add data to order list
$orderList = $split;
$order = ;

else
// Add line to existing data
$order = $line;



print_r($orderList);
fclose($fp);





share|improve this answer






















  • There is a way if you can't rely on the details always being 20 lines long - let me know if you have this problem.
    – Nigel Ren
    Nov 11 at 17:26










  • Well the file also contains a couple of blank datasets like this Array ( [time] => 2018-11-10 08:11:41 [gateway] => Stripe [gatewayOK] => Yes [gatewayTransactionId] => [POST] => Array ( ) [GET] => Array ( ) )
    – Bijay Regmi
    Nov 11 at 17:38










  • As you will end up with an array of segments, you could probably process them after to see which ones you need. The last code example works on variable sized elements.
    – Nigel Ren
    Nov 11 at 17:40










  • Yep I adjusted variables to my need and seems to work perfectly. Thank you.
    – Bijay Regmi
    Nov 11 at 17:41










Your Answer






StackExchange.ifUsing("editor", function ()
StackExchange.using("externalEditor", function ()
StackExchange.using("snippets", function ()
StackExchange.snippets.init();
);
);
, "code-snippets");

StackExchange.ready(function()
var channelOptions =
tags: "".split(" "),
id: "1"
;
initTagRenderer("".split(" "), "".split(" "), channelOptions);

StackExchange.using("externalEditor", function()
// Have to fire editor after snippets, if snippets enabled
if (StackExchange.settings.snippets.snippetsEnabled)
StackExchange.using("snippets", function()
createEditor();
);

else
createEditor();

);

function createEditor()
StackExchange.prepareEditor(
heartbeatType: 'answer',
convertImagesToLinks: true,
noModals: true,
showLowRepImageUploadWarning: true,
reputationToPostImages: 10,
bindNavPrevention: true,
postfix: "",
imageUploader:
brandingHtml: "Powered by u003ca class="icon-imgur-white" href="https://imgur.com/"u003eu003c/au003e",
contentPolicyHtml: "User contributions licensed under u003ca href="https://creativecommons.org/licenses/by-sa/3.0/"u003ecc by-sa 3.0 with attribution requiredu003c/au003e u003ca href="https://stackoverflow.com/legal/content-policy"u003e(content policy)u003c/au003e",
allowUrls: true
,
onDemand: true,
discardSelector: ".discard-answer"
,immediatelyShowMarkdownHelp:true
);



);













draft saved

draft discarded


















StackExchange.ready(
function ()
StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2fstackoverflow.com%2fquestions%2f53251160%2freading-values-from-a-file-which-contains-associative-array-php%23new-answer', 'question_page');

);

Post as a guest















Required, but never shown

























1 Answer
1






active

oldest

votes








1 Answer
1






active

oldest

votes









active

oldest

votes






active

oldest

votes








up vote
1
down vote



accepted










This assumes that each entry is 20 lines long, it reads in the log file and the splits it into 20 segments using array_chunk().



It then processes each segment, first splitting the lines by the => using explode() and adding the values to an associative array with the left hand side as the key. You can then use the key to access each value.



$input = file("log.txt", FILE_IGNORE_NEW_LINES);

$orders = array_chunk($input, 20);
foreach ( $orders as $order )
$split = ;
foreach ( $order as $line )
$info = explode("=>", $line);
if ( count($info) == 2)
$split[trim($info[0]," t")] = trim ($info[1]);



echo "gateway-".$split['gateway'].PHP_EOL;
echo "mc_gross-".$split['mc_gross'].PHP_EOL;



If you wanted a list of all orders...



$input = file("log.txt", FILE_IGNORE_NEW_LINES);

$orders = array_chunk($input, 20);
$orderList = ;
foreach ( $orders as $order )
$split = ;
foreach ( $order as $line )
$info = explode("=>", $line);
if ( count($info) == 2)
$split[trim($info[0]," t")] = trim ($info[1]);


$orderList = $split;



echo "gateway-".$orderList[0]['gateway'].PHP_EOL;
echo "mc_gross-".$orderList[0]['mc_gross'].PHP_EOL;


A third way which doesn't rely on the data being all the same format, this reads on a line by line basis and tries to work out the end of an element itself (just a line containing ))...



$fp = fopen("log.txt", "r");
$orderList = ;
$order = ;
while ( $line = fgets($fp))
// Remove extra data after content
$line = rtrim($line);
// If end of order (a line just starting with a ')')
if ( $line == ')' )
// Convert order into associative array
$split = ;
foreach ( $order as $line )
$info = explode("=>", $line);
if ( count($info) == 2)
$split[trim($info[0]," t")] = trim ($info[1]);


// Add data to order list
$orderList = $split;
$order = ;

else
// Add line to existing data
$order = $line;



print_r($orderList);
fclose($fp);





share|improve this answer






















  • There is a way if you can't rely on the details always being 20 lines long - let me know if you have this problem.
    – Nigel Ren
    Nov 11 at 17:26










  • Well the file also contains a couple of blank datasets like this Array ( [time] => 2018-11-10 08:11:41 [gateway] => Stripe [gatewayOK] => Yes [gatewayTransactionId] => [POST] => Array ( ) [GET] => Array ( ) )
    – Bijay Regmi
    Nov 11 at 17:38










  • As you will end up with an array of segments, you could probably process them after to see which ones you need. The last code example works on variable sized elements.
    – Nigel Ren
    Nov 11 at 17:40










  • Yep I adjusted variables to my need and seems to work perfectly. Thank you.
    – Bijay Regmi
    Nov 11 at 17:41














up vote
1
down vote



accepted










This assumes that each entry is 20 lines long, it reads in the log file and the splits it into 20 segments using array_chunk().



It then processes each segment, first splitting the lines by the => using explode() and adding the values to an associative array with the left hand side as the key. You can then use the key to access each value.



$input = file("log.txt", FILE_IGNORE_NEW_LINES);

$orders = array_chunk($input, 20);
foreach ( $orders as $order )
$split = ;
foreach ( $order as $line )
$info = explode("=>", $line);
if ( count($info) == 2)
$split[trim($info[0]," t")] = trim ($info[1]);



echo "gateway-".$split['gateway'].PHP_EOL;
echo "mc_gross-".$split['mc_gross'].PHP_EOL;



If you wanted a list of all orders...



$input = file("log.txt", FILE_IGNORE_NEW_LINES);

$orders = array_chunk($input, 20);
$orderList = ;
foreach ( $orders as $order )
$split = ;
foreach ( $order as $line )
$info = explode("=>", $line);
if ( count($info) == 2)
$split[trim($info[0]," t")] = trim ($info[1]);


$orderList = $split;



echo "gateway-".$orderList[0]['gateway'].PHP_EOL;
echo "mc_gross-".$orderList[0]['mc_gross'].PHP_EOL;


A third way which doesn't rely on the data being all the same format, this reads on a line by line basis and tries to work out the end of an element itself (just a line containing ))...



$fp = fopen("log.txt", "r");
$orderList = ;
$order = ;
while ( $line = fgets($fp))
// Remove extra data after content
$line = rtrim($line);
// If end of order (a line just starting with a ')')
if ( $line == ')' )
// Convert order into associative array
$split = ;
foreach ( $order as $line )
$info = explode("=>", $line);
if ( count($info) == 2)
$split[trim($info[0]," t")] = trim ($info[1]);


// Add data to order list
$orderList = $split;
$order = ;

else
// Add line to existing data
$order = $line;



print_r($orderList);
fclose($fp);





share|improve this answer






















  • There is a way if you can't rely on the details always being 20 lines long - let me know if you have this problem.
    – Nigel Ren
    Nov 11 at 17:26










  • Well the file also contains a couple of blank datasets like this Array ( [time] => 2018-11-10 08:11:41 [gateway] => Stripe [gatewayOK] => Yes [gatewayTransactionId] => [POST] => Array ( ) [GET] => Array ( ) )
    – Bijay Regmi
    Nov 11 at 17:38










  • As you will end up with an array of segments, you could probably process them after to see which ones you need. The last code example works on variable sized elements.
    – Nigel Ren
    Nov 11 at 17:40










  • Yep I adjusted variables to my need and seems to work perfectly. Thank you.
    – Bijay Regmi
    Nov 11 at 17:41












up vote
1
down vote



accepted







up vote
1
down vote



accepted






This assumes that each entry is 20 lines long, it reads in the log file and the splits it into 20 segments using array_chunk().



It then processes each segment, first splitting the lines by the => using explode() and adding the values to an associative array with the left hand side as the key. You can then use the key to access each value.



$input = file("log.txt", FILE_IGNORE_NEW_LINES);

$orders = array_chunk($input, 20);
foreach ( $orders as $order )
$split = ;
foreach ( $order as $line )
$info = explode("=>", $line);
if ( count($info) == 2)
$split[trim($info[0]," t")] = trim ($info[1]);



echo "gateway-".$split['gateway'].PHP_EOL;
echo "mc_gross-".$split['mc_gross'].PHP_EOL;



If you wanted a list of all orders...



$input = file("log.txt", FILE_IGNORE_NEW_LINES);

$orders = array_chunk($input, 20);
$orderList = ;
foreach ( $orders as $order )
$split = ;
foreach ( $order as $line )
$info = explode("=>", $line);
if ( count($info) == 2)
$split[trim($info[0]," t")] = trim ($info[1]);


$orderList = $split;



echo "gateway-".$orderList[0]['gateway'].PHP_EOL;
echo "mc_gross-".$orderList[0]['mc_gross'].PHP_EOL;


A third way which doesn't rely on the data being all the same format, this reads on a line by line basis and tries to work out the end of an element itself (just a line containing ))...



$fp = fopen("log.txt", "r");
$orderList = ;
$order = ;
while ( $line = fgets($fp))
// Remove extra data after content
$line = rtrim($line);
// If end of order (a line just starting with a ')')
if ( $line == ')' )
// Convert order into associative array
$split = ;
foreach ( $order as $line )
$info = explode("=>", $line);
if ( count($info) == 2)
$split[trim($info[0]," t")] = trim ($info[1]);


// Add data to order list
$orderList = $split;
$order = ;

else
// Add line to existing data
$order = $line;



print_r($orderList);
fclose($fp);





share|improve this answer














This assumes that each entry is 20 lines long, it reads in the log file and the splits it into 20 segments using array_chunk().



It then processes each segment, first splitting the lines by the => using explode() and adding the values to an associative array with the left hand side as the key. You can then use the key to access each value.



$input = file("log.txt", FILE_IGNORE_NEW_LINES);

$orders = array_chunk($input, 20);
foreach ( $orders as $order )
$split = ;
foreach ( $order as $line )
$info = explode("=>", $line);
if ( count($info) == 2)
$split[trim($info[0]," t")] = trim ($info[1]);



echo "gateway-".$split['gateway'].PHP_EOL;
echo "mc_gross-".$split['mc_gross'].PHP_EOL;



If you wanted a list of all orders...



$input = file("log.txt", FILE_IGNORE_NEW_LINES);

$orders = array_chunk($input, 20);
$orderList = ;
foreach ( $orders as $order )
$split = ;
foreach ( $order as $line )
$info = explode("=>", $line);
if ( count($info) == 2)
$split[trim($info[0]," t")] = trim ($info[1]);


$orderList = $split;



echo "gateway-".$orderList[0]['gateway'].PHP_EOL;
echo "mc_gross-".$orderList[0]['mc_gross'].PHP_EOL;


A third way which doesn't rely on the data being all the same format, this reads on a line by line basis and tries to work out the end of an element itself (just a line containing ))...



$fp = fopen("log.txt", "r");
$orderList = ;
$order = ;
while ( $line = fgets($fp))
// Remove extra data after content
$line = rtrim($line);
// If end of order (a line just starting with a ')')
if ( $line == ')' )
// Convert order into associative array
$split = ;
foreach ( $order as $line )
$info = explode("=>", $line);
if ( count($info) == 2)
$split[trim($info[0]," t")] = trim ($info[1]);


// Add data to order list
$orderList = $split;
$order = ;

else
// Add line to existing data
$order = $line;



print_r($orderList);
fclose($fp);






share|improve this answer














share|improve this answer



share|improve this answer








edited Nov 11 at 17:40

























answered Nov 11 at 17:26









Nigel Ren

23.7k61832




23.7k61832











  • There is a way if you can't rely on the details always being 20 lines long - let me know if you have this problem.
    – Nigel Ren
    Nov 11 at 17:26










  • Well the file also contains a couple of blank datasets like this Array ( [time] => 2018-11-10 08:11:41 [gateway] => Stripe [gatewayOK] => Yes [gatewayTransactionId] => [POST] => Array ( ) [GET] => Array ( ) )
    – Bijay Regmi
    Nov 11 at 17:38










  • As you will end up with an array of segments, you could probably process them after to see which ones you need. The last code example works on variable sized elements.
    – Nigel Ren
    Nov 11 at 17:40










  • Yep I adjusted variables to my need and seems to work perfectly. Thank you.
    – Bijay Regmi
    Nov 11 at 17:41
















  • There is a way if you can't rely on the details always being 20 lines long - let me know if you have this problem.
    – Nigel Ren
    Nov 11 at 17:26










  • Well the file also contains a couple of blank datasets like this Array ( [time] => 2018-11-10 08:11:41 [gateway] => Stripe [gatewayOK] => Yes [gatewayTransactionId] => [POST] => Array ( ) [GET] => Array ( ) )
    – Bijay Regmi
    Nov 11 at 17:38










  • As you will end up with an array of segments, you could probably process them after to see which ones you need. The last code example works on variable sized elements.
    – Nigel Ren
    Nov 11 at 17:40










  • Yep I adjusted variables to my need and seems to work perfectly. Thank you.
    – Bijay Regmi
    Nov 11 at 17:41















There is a way if you can't rely on the details always being 20 lines long - let me know if you have this problem.
– Nigel Ren
Nov 11 at 17:26




There is a way if you can't rely on the details always being 20 lines long - let me know if you have this problem.
– Nigel Ren
Nov 11 at 17:26












Well the file also contains a couple of blank datasets like this Array ( [time] => 2018-11-10 08:11:41 [gateway] => Stripe [gatewayOK] => Yes [gatewayTransactionId] => [POST] => Array ( ) [GET] => Array ( ) )
– Bijay Regmi
Nov 11 at 17:38




Well the file also contains a couple of blank datasets like this Array ( [time] => 2018-11-10 08:11:41 [gateway] => Stripe [gatewayOK] => Yes [gatewayTransactionId] => [POST] => Array ( ) [GET] => Array ( ) )
– Bijay Regmi
Nov 11 at 17:38












As you will end up with an array of segments, you could probably process them after to see which ones you need. The last code example works on variable sized elements.
– Nigel Ren
Nov 11 at 17:40




As you will end up with an array of segments, you could probably process them after to see which ones you need. The last code example works on variable sized elements.
– Nigel Ren
Nov 11 at 17:40












Yep I adjusted variables to my need and seems to work perfectly. Thank you.
– Bijay Regmi
Nov 11 at 17:41




Yep I adjusted variables to my need and seems to work perfectly. Thank you.
– Bijay Regmi
Nov 11 at 17:41

















draft saved

draft discarded
















































Thanks for contributing an answer to Stack Overflow!


  • Please be sure to answer the question. Provide details and share your research!

But avoid


  • Asking for help, clarification, or responding to other answers.

  • Making statements based on opinion; back them up with references or personal experience.

To learn more, see our tips on writing great answers.





Some of your past answers have not been well-received, and you're in danger of being blocked from answering.


Please pay close attention to the following guidance:


  • Please be sure to answer the question. Provide details and share your research!

But avoid


  • Asking for help, clarification, or responding to other answers.

  • Making statements based on opinion; back them up with references or personal experience.

To learn more, see our tips on writing great answers.




draft saved


draft discarded














StackExchange.ready(
function ()
StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2fstackoverflow.com%2fquestions%2f53251160%2freading-values-from-a-file-which-contains-associative-array-php%23new-answer', 'question_page');

);

Post as a guest















Required, but never shown





















































Required, but never shown














Required, but never shown












Required, but never shown







Required, but never shown

































Required, but never shown














Required, but never shown












Required, but never shown







Required, but never shown







這個網誌中的熱門文章

Barbados

How to read a connectionString WITH PROVIDER in .NET Core?

Node.js Script on GitHub Pages or Amazon S3