Solved

PHP Array Grouping of Values

Posted on 2016-07-18
4
54 Views
Last Modified: 2016-07-18
I have an array like this...

$array = array();
$array['2017-03-15']['rate']=125;
$array['2017-03-16']['rate']=125;
$array['2017-03-17']['rate']=130;
$array['2017-03-18']['rate']=130;
$array['2017-03-19']['rate']=125;
$array['2017-03-20']['rate']=125;
$array['2017-03-21']['rate']=115;

I need the output to be like.

"2 days @ 125
2 days @ 130
2 day at $125
1 Day at $115"

in the order of the days.

So, if there's X consecutive days at a specific amount, it just needs to group those together as a single line reflecting that - in date order.

How would I go about pulling that off?
0
Comment
Question by:erzoolander
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
  • 2
  • 2
4 Comments
 
LVL 35

Accepted Solution

by:
gr8gonzo earned 500 total points
ID: 41717574
You just need to keep track of the last and current values and if they match, then increment a counter and then you log whenever the value changes:

$current_rate = null;
$current_rate_count = 0;
$rate_log = array();

foreach($array as $day => $day_details)
{
  $day_rate = $day_details["rate"];

  if($day_rate == $current_rate)
  {
    $current_rate_count++;
  }
  else
  {
    if($current_rate !== null)
    {
      $rate_log[] = array($current_rate_count,$current_rate);
    }

    // Update the current rate to the new one
    $current_rate = $day_rate;
    $current_rate_count = 1;
  }
}

That should give you an array called $rate_log with the data you're asking for:

$rate_log[0] = array(2, 125);
$rate_log[1] = array(2, 130);
$rate_log[2] = array(2, 125);
$rate_log[3] = array(1, 115);

From there, you can just loop through it and format/display the results:

foreach($rate_log as $log_details)
{
  echo $log_details[0] . " day at \$" . $log_details[1] . "<br />\n";
}
0
 
LVL 2

Author Comment

by:erzoolander
ID: 41717603
the approach makes sense to me - but the output seems to be off.

Here's my code -

$array = array();
$array['2017-03-15']['rate']=125;
$array['2017-03-16']['rate']=125;
$array['2017-03-17']['rate']=130;
$array['2017-03-18']['rate']=130;
$array['2017-03-19']['rate']=130;
$array['2017-03-20']['rate']=135;
?>
<pre>
<?php print_r($array);?>
</pre>
<?php


$current_rate = null;
$current_rate_count = 0;
$rate_log = array();

foreach($array as $day => $day_details)
{
  $day_rate = $day_details["rate"];

  if($day_rate == $current_rate)
  {
    $current_rate_count++;
  }
  else
  {
    if($current_rate !== null)
    {
      $rate_log[] = array($current_rate_count,$current_rate);
    }

    // Update the current rate to the new one
    $current_rate = $day_rate;
    $current_rate_count = 1;
  }
}

foreach($rate_log as $log_details)
{
  echo $log_details[0] . " day at \$" . $log_details[1] . "<br />\n";
} ]

Open in new window


which outputs:

2 day at $125
3 day at $130

-- for some reason it's omitting the 135 value...or anything that comes after 130.
0
 
LVL 35

Expert Comment

by:gr8gonzo
ID: 41717654
Sorry, I was missing a line. After the foreach loop through $array, you might still have some data at the end that needs to be added to the $rate_log array, so just check and add it after the loop ends:

if($current_rate_count > 0) { $rate_log[] = array($current_rate_count,$current_rate); }
0
 
LVL 2

Author Comment

by:erzoolander
ID: 41717670
Thanks!  I found that after also :)

Thank you!
0

Featured Post

Independent Software Vendors: We Want Your Opinion

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Suggested Solutions

Title # Comments Views Activity
Simple function not working 7 43
Returning errors to display in a dialog box - Ajax, Json, Php 16 49
php error 27 55
Send email using HTML and PHP in separate file 5 38
I imagine that there are some, like me, who require a way of getting currency exchange rates for implementation in web project from time to time, so I thought I would share a solution that I have developed for this purpose. It turns out that Yaho…
Things That Drive Us Nuts Have you noticed the use of the reCaptcha feature at EE and other web sites?  It wants you to read and retype something that looks like this.Insanity!  It's not EE's fault - that's just the way reCaptcha works.  But it is …
The viewer will learn how to create and use a small PHP class to apply a watermark to an image. This video shows the viewer the setup for the PHP watermark as well as important coding language. Continue to Part 2 to learn the core code used in creat…
This tutorial will teach you the core code needed to finalize the addition of a watermark to your image. The viewer will use a small PHP class to learn and create a watermark.

763 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question