Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • SEARCH
  • Home
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 6565087
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 25, 20262026-05-25T14:02:56+00:00 2026-05-25T14:02:56+00:00

I am in the middle of doing a quick prototype of some statistical graphs

  • 0

I am in the middle of doing a quick prototype of some statistical graphs using FLOT and php and I am literally hacking something together to show something.

However I am faced with a problem which is doing my nut…

so I have some data which I have grabbed from the database and then in order to make it work easily for my Flot hack I have converted it to look something like this:

[ [0,1], [2,1] , [4,1] , [4,1] , [5,1] ,[9 , 1] , [9, 1] , [10,1] , [12,1], [13,1] ]

now the end plan is to have several sets of data and then produce a stacked chart. so what i need to do is sort out where there are duplicate like in the above where there is:

[…..[4,1],[4,1] …..]

needs to look like this:

[…..[4,1],[4,2,1]…..]

below is my attempt at trying to sort it ( it is the latest iteration of attempt i have tried forwards, backwards changing inner values and outer values)…

$count = count($array);
$sorted = false;
while (!$sorted)
{
    $doneSomething = 0;
    for($i = $count - 1; $i > 0; $i--)
    {
        $tempArr = $array[$i];
        foreach($array as $key => $a)
        {
            if($key == $i)
            {
                echo "breaking";
                continue;   
                }
            $result = array_diff($tempArr,$a);
            if(count($result) == 0 )
            {
                $array[$i][1]++;
                if(count($array[$i]) == 3)
                    $array[$i][2]++;
                else
                    $array[$i][] = 1;
                $doneSomething++;
                }
            if($doneSomething > 0)
                break;
            }
        }
    if($doneSomething == 0)
        $sorted=true;
    }

the result is this:

 Array
   (
    [0] => Array
        (
            [0] => 0
            [1] => 1
        )

    [1] => Array
        (
            [0] => 2
            [1] => 4
            [2] => 3
        )

    [2] => Array
        (
            [0] => 4
            [1] => 2
            [2] => 1
        )

    [3] => Array
        (
            [0] => 4
            [1] => 5
            [2] => 4
        )

    [4] => Array
        (
            [0] => 5
            [1] => 1
        )

    [5] => Array
        (
            [0] => 9
            [1] => 2
            [2] => 1
        )

    [6] => Array
        (
            [0] => 9
            [1] => 3
            [2] => 2
        )

    [7] => Array
        (
            [0] => 10
            [1] => 1
        )

    [8] => Array
        (
            [0] => 12
            [1] => 1
        )

    [9] => Array
        (
            [0] => 13
            [1] => 1
        )

   )

As you can see not really my intended result:

[ [0,1], [2,1] , [4,1] , [4,2,1] , [5,1] ,[9,1] , [9,2,1] , [10,1] , [12,1], [13,1] ]

If anyone can help me resolve this issue it would be really appreciated.

thanks

Vade

Edit: i should note that if there is only 2 duplicates it isnt that bad i can work that out but its when there is 3 or more ie:

[….[4,1],[4,1],[4,1],[4,1]…]

which needs to look like:

[….[4,1],[4,2,1],[4,3,2],[4,4,3]….]

  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-05-25T14:02:57+00:00Added an answer on May 25, 2026 at 2:02 pm

    Try this out. This seems to be working fine here:

    <?php
        $array = array( array(0,1), array(2,1) , array(4,1) , 
                        array(4,1) , array(5,1) ,array(9 , 1) , 
                        array(9, 1) , array(10,1) , array(12,1), array(13,1) );
    
        $last_elem = null;
        foreach($array as &$elem){
            if($last_elem){
                if($elem[0] == $last_elem[0]){
                    $elem[2] = (isset($last_elem[2]) ? $last_elem[2] + 1 : 1);
                    $elem[1] = $last_elem[1] + 1;
                }
            }
            $last_elem = &$elem;
        }
    
        var_dump($array);
    ?>
    

    This is working perfectly here, I have tested it with this array:
    [ [0,1], [2,1], [4,1], [4,1], [4,1], [4,1], [5,1], [9,1], [9,1] ,[9,1] ,[10,1] ,[12,1], [13,1] ];

    The output is:

    array
      0 => 
        array
          0 => int 0
          1 => int 1
      1 => 
        array
          0 => int 2
          1 => int 1
      2 => 
        array
          0 => int 4
          1 => int 1
      3 => 
        array
          0 => int 4
          1 => int 2
          2 => int 1
      4 => 
        array
          0 => int 4
          1 => int 3
          2 => int 2
      5 => 
        array
          0 => int 4
          1 => int 4
          2 => int 3
      6 => 
        array
          0 => int 5
          1 => int 1
      7 => 
        array
          0 => int 9
          1 => int 1
      8 => 
        array
          0 => int 9
          1 => int 2
          2 => int 1
      9 => 
        array
          0 => int 9
          1 => int 3
          2 => int 2
      10 => 
        array
          0 => int 10
          1 => int 1
      11 => 
        array
          0 => int 12
          1 => int 1
      12 => &
        array
          0 => int 13
          1 => int 1
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I have a webapp that I am in the middle of doing some load/performance
I am doing some queries from a database to make graphs, and a graph
I'm in the middle of writing code in .Net to draw something in my
I have inherited a middle tier system with some multi-Threading issues. Two different threads,
I'm in the middle of building a pretty big site using asp.net (forms if
I was in the middle of doing a recursive svn add/commit, and a folder
I'm in the middle of using Visual Basic (Visual Studio 2010) to create dynamically
Am I doing something wrong? If I don't have the with options added location
We're in the middle of debugging some memory issues. We were watch 5 instances
I'm in the middle of porting some c++ code to java, and I keep

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.