Array parse PHP

I've got an array that looks like this

Array
(
    [2015-09-29] => Array
        (
            [sale] => 133.74
            [net] => 101.00
            [nettax] => 12.63
            [fm] => 108.07
            [fmtax] => 13.51
        )

    [2015-09-30] => Array
        (
            [sale] => 133.74
            [net] => 101.00
            [nettax] => 12.63
            [fm] => 108.07
            [fmtax] => 13.51
        )

    [2015-10-01] => Array
        (
            [sale] => 202.59
            [net] => 153.00
            [nettax] => 19.13
            [fm] => 163.71
            [fmtax] => 20.46
        )

)

Open in new window


What I'd like to do is output

"2 days at 133.74
1 day at 202.59"

I could build another array which keys by the price and just do counts - but I'm hoping there's a more straightforward way rather than muddying stuff up by creating a secondary array.  :)

Any ideas?
LVL 2
erzoolanderAsked:
Who is Participating?

[Product update] Infrastructure Analysis Tool is now available with Business Accounts.Learn More

x
I wear a lot of hats...

"The solutions and answers provided on Experts Exchange have been extremely helpful to me over the last few years. I wear a lot of hats - Developer, Database Administrator, Help Desk, etc., so I know a lot of things but not a lot about one thing. Experts Exchange gives me answers from people who do know a lot about one thing, in a easy to use platform." -Todd S.

Ray PaseurCommented:
It's helpful to know about this function.  When you post data here at E-E, if you use this it will save everyone time as we try to build and test solutions for you.
http://php.net/manual/en/function.var-export.php
erzoolanderAuthor Commented:
Does this help?  I've already got it for my test PHP.

$sale['2015-09-29']['sale'] = "133.74";
$sale['2015-09-29']['net'] = "101.00";
$sale['2015-09-29']['nettax'] = "12.63";
$sale['2015-09-29']['fm'] = "108.07";
$sale['2015-09-29']['fmtax'] = "13.51";
$sale['2015-09-30']['sale'] = "133.74";
$sale['2015-09-30']['net'] = "101.00";
$sale['2015-09-30']['nettax'] = "12.63";
$sale['2015-09-30']['fm'] = "108.07";
$sale['2015-09-30']['fmtax'] = "13.51";
$sale['2015-10-01']['sale'] = "202.59";
$sale['2015-10-01']['net'] = "153.00";
$sale['2015-10-01']['nettax'] = "19.13";
$sale['2015-10-01']['fm'] = "163.71";
$sale['2015-10-01']['fmtax'] = "20.46";

Open in new window

Ray PaseurCommented:
Try this:
http://iconoun.com/demo/temp_errzoolander.php
<?php // demo/temp_errzoolander.php
/**
 * http://www.experts-exchange.com/questions/28699030/Array-parse-PHP.html
 */
error_reporting(E_ALL);
echo '<pre>';


$arr = Array
(
    '2015-09-29' => Array
        (
            'sale' => '133.74',
            'net' => '101.00',
            'nettax' => '12.63',
            'fm' => '108.07',
            'fmtax' => '13.51'
        ),

    '2015-09-30' => Array
        (
            'sale' => '133.74',
            'net' => '101.00',
            'nettax' => '12.63',
            'fm' => '108.07',
            'fmtax' => '13.51'
        ),

    '2015-10-01' => Array
        (
            'sale' => '202.59',
            'net' => '153.00',
            'nettax' => '19.13',
            'fm' => '163.71',
            'fmtax' => '20.46'
        )
)
;
var_dump($arr);

// DETERMINE ALL OF THE SALE PRICES
$sps = [];
foreach ($arr as $day)
{
    @$sps[$day['sale']]++;
}

// SHOW THE QORK PRODUCT
var_dump($sps);

// FORMAT THE WORK PRODUCT
foreach ($sps as $val => $ndays)
{
    $plural = NULL;
    if ($ndays != 1) $plural = 's';
    echo PHP_EOL . "$ndays day$plural at $val";
}

Open in new window

Experts Exchange Solution brought to you by

Your issues matter to us.

Facing a tech roadblock? Get the help and guidance you need from experienced professionals who care. Ask your question anytime, anywhere, with no hassle.

Start your 7-day free trial
Why Diversity in Tech Matters

Kesha Williams, certified professional and software developer, explores the imbalance of diversity in the world of technology -- especially when it comes to hiring women. She showcases ways she's making a difference through the Colors of STEM program.

Ray PaseurCommented:
Thanks for posting that - It would have helped, but I think we're OK anyway this time.  Test data is the most important thing a programmer can have!

All the best, ~Ray
hieloCommented:
>> 2 days at 133.74
what if instead of:
$sale['2015-09-30']['sale'] = "133.74";

it was:
$sale['2015-10-01']['sale'] = "133.74";
Meaning, the prices are for non-consecutive days? Does it count as "2 days"?
Ray PaseurCommented:
@hielo: Yep, one of many questions we might have about the data structure!  For me, it all comes back to the quality of the test data, since that is what will be used to prove whether the solution is correct or not.
erzoolanderAuthor Commented:
Yep - it would count as two days in such a scenario.  The function is only meant to tell how many days were sold at $x rate.  

The order the rates fell isn't something they're concerned with.  So - if it was a 10 day sequence...with 2 at the start having the same rate as 2 at the end...they only want to know that 4 days were at that amount.  :)
Ray PaseurCommented:
If all you're looking for is the total, it's OK.  I think as a consumer, I might want to know the daily prices, too, but that would seem to be a different part of the application.  Also, as a technologist, I think I would want to use object-oriented design and programming for this.  If for no other reason than that the PHP syntax is easier with OOP than it is with arrays.
It's more than this solution.Get answers and train to solve all your tech problems - anytime, anywhere.Try it for free Edge Out The Competitionfor your dream job with proven skills and certifications.Get started today Stand Outas the employee with proven skills.Start learning today for free Move Your Career Forwardwith certification training in the latest technologies.Start your trial today
PHP

From novice to tech pro — start learning today.