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 9092661
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 16, 20262026-06-16T22:53:34+00:00 2026-06-16T22:53:34+00:00

I have two arrays. Now i want to compare some elements in from both

  • 0

I have two arrays. Now i want to compare some elements in from both arrays based on that i need to populate one array element to the new array.

 Array1: $days this is an array of elements between two dates.

$days = Array
       (
         [0] => 2012-06-23
         [1] => 2012-06-24
         [2] => 2012-06-25
         [3] => 2012-06-26
         [4] => 2012-06-27
         [5] => 2012-06-28
         [6] => 2012-06-29
         [7] => 2012-06-30
         [8] => 2012-07-01
         [9] => 2012-07-02
         [10] => 2012-07-03
         [11] => 2012-07-04
         [12] => 2012-07-05
         [13] => 2012-07-06
         [14] => 2012-07-07
         [15] => 2012-07-08
         [16] => 2012-07-09
         [17] => 2012-07-10
         [18] => 2012-07-11
         [19] => 2012-07-12
         [20] => 2012-07-13
         [21] => 2012-07-14
         [22] => 2012-07-15
         [23] => 2012-07-16
        )

 Array2: $summary is the array which i am getting from the database..

 $summary = Array
 (
    [0] => Array
    (
        [date] => 2012-06-23 
        [no_of_posts] => 1
    )

    [1] => Array
    (
        [date] => 2012-06-24 
        [no_of_posts] => 2
    )

    [2] => Array
    (
        [date] => 2012-06-25 
        [no_of_posts] => 1
    )

    [3] => Array
    (
        [date] => 2012-06-26 
        [no_of_posts] => 1
    )

    [4] => Array
    (
        [date] => 2012-06-27
        [no_of_posts] => 1
    )

Now from these both arrays i need an array which tells me on this date this many times posts are there if there no posts against one date then it should be zero for that. I tried like this…..

    $result = array();
   foreach($summary as $key=>$s) {
       foreach($days as $d) {
          if($s['date'] == $d) {
               $result[$d] = $s['no_of_posts'];
          } else {
               $result[$d] = 0;
          }
      }
   }

I know there is mistake i can’t able to find………..any ideas.

Output array need to look like below…..

    $result = Array
            Array
       (
         [2012-06-23] = 1
         [2012-06-24] = 2
         [2012-06-25] = 1
         [2012-06-26] = 1
         [2012-06-27] = 1
         [2012-06-28] = 0
         [2012-06-29] = 0
         [2012-06-30] = 0
         [2012-07-01] = 0
         [2012-07-02] = 0
         [2012-07-03] = 0
         [2012-07-04] = 0
         [2012-07-05] = 0
         [2012-07-06] = 0
         [2012-07-07] = 0
         [2012-07-08] = 0
         [2012-07-09] = 0
         [2012-07-10] = 0
         [2012-07-11] = 0
         [2012-07-12] = 0 
         [2012-07-13] = 0
         [2012-07-14] = 0
         [2012-07-15] = 0
         [2012-07-16] = 0
        )
  • 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-06-16T22:53:36+00:00Added an answer on June 16, 2026 at 10:53 pm

    The quickest way is to loop through your database results and create a values array indexed by date:

    $summaryTotals = array();
    foreach($summary as $dbSummary) {
        $summaryTotals[$dbSummary['date']] = $dbSummary['no_of_posts'];
    }
    

    Then iterate through your dates array inputting the values where a match occurs in the summary array, like so:

    $dayValues = array();
    foreach($days as $day) {
        if(isset($summaryTotals[$day])) {
            $dayValues[$day] = $summaryTotals[$day];
        } else {
            $dayValues[$day] = 0;
        }
    }
    

    The dayValues array should now be what you’re after. I believe.

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

If I have two arrays both with 10000 items, now I want to merge
I have two arrays. I'd like to copy ranges of data from one of
I have two arrays (records returned from a database query) that I'm merging. I
i have two numbers 1,2 in one array.Now i want to generate all the
I have two arrays which contain objects of assets, now I want to subtract
I have two arrays a={a,b,c,d} b={1,2,3,4} now I want to create a 3rd array
I have two arrays values and keys both of the same length. I want
I have two arrays, @names and @employees , that are filled with strings that
I have two arrays and I post the arrays in one input; <input name='sistem[]'
I have two arrays, the first one contents numbers and the second one use

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.