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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 4, 20262026-06-04T16:25:53+00:00 2026-06-04T16:25:53+00:00

I’m generating a dummy array where keys are timestamps and values are zeroes. Start

  • 0

I’m generating a dummy array where keys are timestamps and values are zeroes. Start is today (date without time part) and going back for $days days in the past:

$days  = 10;
$limit = strtotime(date('Y-m-d'));
$start = $limit - (($days - 1) * 86400);

// Dummy array of timestamps and zeroes
$dummy = array_combine(range($start, $limit, 86400), array_fill(0, $days, 0));
var_dump($dummy);

array (size=10)
  1337551200 => int 0
  1337637600 => int 0
  1337724000 => int 0
  1337810400 => int 0
  1337896800 => int 0
  1337983200 => int 0
  1338069600 => int 0
  1338156000 => int 0
  1338242400 => int 0
  1338328800 => int 0 // Today is the last

This array is going to be merged with another one extracted from a MySQL result set (and its values should override dummy ones):

$keys = array_map(function($e) { return strtotime($e['date']); }, $values);
$vals = array_map(function($e) { return intval($e['count']); },   $values);

// Array of real values coming from database
$reals = array_combine($keys, $vals);
var_dump($reals);

array (size=1)
  1338328800 => int 2 // Today

Since array_merge operates a key reordering (when keys of integer type) i switched to + array operator, and the resulting array is reordered: new keys come first:

var_dump($reals + $dummy);

array (size=10)
  1338328800 => int 2 // Today become the first
  1337551200 => int 0
  1337637600 => int 0
  1337724000 => int 0
  1337810400 => int 0
  1337896800 => int 0
  1337983200 => int 0
  1338069600 => int 0
  1338156000 => int 0
  1338242400 => int 0

So question is + operator put those keys first and how can i solve this without using sort functions, as below:

$merged = $reals + $dummy;
ksort($merged);
var_dump($merged);

array (size=10)
  1337551200 => int 0
  1337637600 => int 0
  1337724000 => int 0
  1337810400 => int 0
  1337896800 => int 0
  1337983200 => int 0
  1338069600 => int 0
  1338156000 => int 0
  1338242400 => int 0
  1338328800 => int 2
  • 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-04T16:25:54+00:00Added an answer on June 4, 2026 at 4:25 pm

    That’s because + (applied to the arrays) appends the data from the right array that is not exists in the left array.

    You’ve used var_dump($reals + $dummy); which means: take $reals which consists of 1 item (today as the first and the only element) and append everything from $dummy that has another keys.

    The possible solutions are:

    1. sort the data (you mentioned that)
    2. merge manually, with foreach
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

link Im having trouble converting the html entites into html characters, (&# 8217;) i
I have a string like this: La Torre Eiffel paragonata all’Everest What PHP function
Specifically, suppose I start with the string string =hello \'i am \' me And
I'm parsing an RSS feed that has an ’ in it. SimpleXML turns this
That's pretty much it. I'm using Nokogiri to scrape a web page what has
I have just tried to save a simple *.rtf file with some websites and
I want to count how many characters a certain string has in PHP, but
I would like to count the length of a string with PHP. The string
For some reason, after submitting a string like this Jack’s Spindle from a text
I am trying to understand how to use SyndicationItem to display feed which is

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.