0

I'm doing a quick PHP project that takes information from another file and reads it and gathers the information. The websites stores information like this:

Filename: .ipn

 Array
(
    [mc_gross] => 10.00
    [ipn_track_id] => 731ea17d
)
Array
(
    [mc_gross] => 10.00
    [ipn_track_id] => 523fd7886
)
Array
(
    [mc_gross] => 10.00
    [ipn_track_id] => 523fd7886
)

How would I create a PHP script that takes the .ipn file and sums up the overall [mc_gross], which in this case would be 30.00?

4
  • 2
    is that the exact contents of the .ipn file? Commented Feb 2, 2014 at 22:51
  • You should save the array as JSON. json_encode($array); and json_decode($file);. Commented Feb 2, 2014 at 22:52
  • What generates the .ipn? That's a very unconventional format and not very mark-up friendly. You could parse it, but you'd be better off working with XML, JSON, or YML Commented Feb 2, 2014 at 22:52
  • You can use a regular expression for this - just match on the literal [mc_gross] => followed by any number of [0-9.]. Give that a go, and let me know how you get on! Commented Feb 2, 2014 at 22:52

3 Answers 3

2

This should work -

<?php
        $input = file_get_contents("filename.ext");
        $regexp = "/\[mc_gross\][ ]*=>[ ]*([0-9]+(?:\.[0-9]+)?)/";
        $sum = 0;
        if(preg_match_all($regexp, $input, $matches, PREG_SET_ORDER)){
            foreach($matches as $match) {
                $sum += $match[1];
            }
            unset($match);
        }
        echo "Sum ".$sum;

/*
    var_dumping the matches - 
    array
      0 => 
        array
          0 => string '[mc_gross] => 10.00' (length=19)
          1 => string '10.00' (length=5)
      1 => 
        array
          0 => string '[mc_gross] => 10.00' (length=19)
          1 => string '10.00' (length=5)
      2 => 
        array
          0 => string '[mc_gross] => 10.00' (length=19)
          1 => string '10.00' (length=5)
*/

?>
Sign up to request clarification or add additional context in comments.

7 Comments

What would I type inside the regexp if I wanted to print out an array of string that would be named [mc_name] everytime.
mc_name instead of mc_gross?? just replace mc_gross to mc_name Like this "/[mc_name][ ]*=>[ ]*([0-9]+(?:\.[0-9]+)?)/" .
Additionally, if you have [mc_gross] and [mc_name] occuring in different arrays and want to find sum of both the entries, you can use this "/[(?:(?:mc_gross)|(?:mc_name))][ ]*=>[ ]*([0-9]+(?:\.[0-9]+)?)/"
The problem is that your code seems to only take numbers, not strings.
Oh ok you did not specify that. I took the format to be concrete. Use this for names - /[mc_name][ ]*=>[ ]*([a-zA-Z]+)/
|
0

First, save the array to the files using serialize.

$string = serialize($array);
file_put_contents('myfile.ipn',serialize($string));

Then, read the file using unserialize and file_get_contents.

$content = file_get_contents('myfile.ipn');
$array = unserialize($content);

Hope this helps!

1 Comment

What if OP has no influence on how the file that he wants to read is generated?
0

Do it:

$lines = array_chunk(file('items.ipn'),5);
$sum = 0;
foreach($lines as $line){
  $sum += floatval(end(explode('=>',$line[2])));
}
print $sum;
// 30

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.