Reading values from a file which contains associative array PHP
up vote
0
down vote
favorite
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
add a comment |
up vote
0
down vote
favorite
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
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
add a comment |
up vote
0
down vote
favorite
up vote
0
down vote
favorite
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
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
php arrays
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
add a comment |
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
add a comment |
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);
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
add a comment |
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);
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
add a comment |
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);
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
add a comment |
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);
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);
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
add a comment |
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
add a comment |
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.
Sign up or log in
StackExchange.ready(function ()
StackExchange.helpers.onClickDraftSave('#login-link');
);
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
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
Sign up or log in
StackExchange.ready(function ()
StackExchange.helpers.onClickDraftSave('#login-link');
);
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
Sign up or log in
StackExchange.ready(function ()
StackExchange.helpers.onClickDraftSave('#login-link');
);
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
Sign up or log in
StackExchange.ready(function ()
StackExchange.helpers.onClickDraftSave('#login-link');
);
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
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
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