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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 4, 20262026-06-04T13:37:26+00:00 2026-06-04T13:37:26+00:00

Currently I’m attempting to simulate hours of the day with my keys for inserting

  • 0

Currently I’m attempting to simulate hours of the day with my keys for inserting into Mongo and I have something along these lines (simplified) setup:

for($i=0;$i<23;$i++){
    $returnElement["hours"]["$i"] = array();
    foreach($this->list_of_daily_usage_items as $item){
      /* @var $item DailyUsageItem */
      $returnElement["hours"]["$i"][$item->getLabel()] = $item->getDefaultValue();
    }
  }

With this I’m creating something similar to:

 [hours] => Array
    (
        [0] => Array
            (
                [labelOne] => 0
                [labelTwo] => 0
            )
        ....
        [23] => Array
            (
                [labelOne] => 0
                [labelTwo] => 0
            )

    )

However then when i push to JSON It’s converting that to:

"hours": [{
    "labelOne": 0,
    "labelTwo": 0,
}, {
    "labelOne": 0,
    "labelTwo": 0,
}]

Which normally I would expect; however when I’m attempting to make these numeric keys it’s not really what I’m hoping to see..

Is there a way to achieve this short of prepending something like _ to the number to force it to a string?

Thank you

  • 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-04T13:37:27+00:00Added an answer on June 4, 2026 at 1:37 pm

    You could add a temporary element to force a PHP Array to be converted a JSON Object

     array("_" => "removeme",
        0 => array("hello" => 0,
               "hell0" => 2),
        1 => array("hello" => 0,
               "hell0" => 2),
        2 => array("hello" => 0,
               "hell0" => 2));
    

    this will generate

    {
        "_": "removeme",
        "0": {
            "hello": 0,
            "hell0": 2
        },
        "1": {
            "hello": 0,
            "hell0": 2
        },
        "2": {
            "hello": 0,
            "hell0": 2
        }
    }
    

    If we combine this with a neato regexp, we just hassled us past PHPs somewhat evil type system:

    $finejson = preg_replace("/\"_\":\".*\",/", "", $json);
    

    which gives us this:

    {
        "0": {
            "hello": 0,
            "hell0": 2
        },
        "1": {
            "hello": 0,
            "hell0": 2
        },
        "2": {
            "hello": 0,
            "hell0": 2
        }
    }
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

Currently, I'm working on a project where I have a server - client relationship
Currently I have 2 ways of displaying images in a cell, which way will
Currently working with converting SQLException error messages into messages that are more useful for
Currently I have this code: <?php echo '<meta name=robots content=noindex>'; $arr = json_decode(file_get_contents(http://media1.clubpenguin.com/play/en/web_service/game_configs/ paper_items.json),true);
Currently i have a node.js and socket.io application in development on my local machine
Currently, I have: <html> <div class=yes1><span><img src=img></span></div> </html> <script> var x = ='yes1' var
currently we have a column with only integer values declared as NUMBER. At the
Currently i'm having trouble using two functions in my -(void)viewDidLoad , both of these
Currently have the following expression: @[TMS_Dest] + \\ + @[TMS_Rename] + (DT_WSTR,4)DatePart(yyyy, GetDate()) +
Currently I have a date in my clients data table and it is stored

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.