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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 11, 20262026-06-11T06:33:35+00:00 2026-06-11T06:33:35+00:00

My problem is, PHP encodes arrays differently, based on if they are consecutive &

  • 0

My problem is, PHP encodes arrays differently, based on if they are consecutive & begin with a zero index, or not. Example:

$arr = array(array(12), array(13));
===> [[12], [13]]

$arr = array("0" => array(12), "1" => array(13));
===> [[12], [13]]

$arr = array("0" => array(12), "2" => array(13));
===> {"0": [12], "2": [13]}

Why is the third one so radically different?

The first example produces a list of lists, the third example produces an object with lists. I need to convert all of these to Java ‘s Map<Integer, List<Double>>. That is the most generic datatype I could find in Java for these PHP objects. I am using Gson from Google. However, since the examples produces different types of objects, I cannot just read this into a map. I have to first check if it has indices and then adding one by one to a custom map. Please look at the line that says “THERE HAS TO BE A BETTER WAY TO DO THIS PART”. This is my code:

import java.lang.StringBuilder;
import com.google.gson.*;
import com.google.gson.reflect.*;
import java.util.Map;
import java.util.HashMap;
import java.util.List;

public class Saving {

    public static void main(String[] args) {

        String json = "[[12], [13]]";
        json = json.trim();
        Map<Integer, List<Double>> fuelSavings = null;

        // such a cluster****
        if(json.startsWith("[[")) { // THERE HAS TO BE A BETTER WAY TO DO THIS PART
        // ANY WAY I CAN AVOID THIS ENTIRE IF CONDITION

            //implicit keys
            fuelSavings = new HashMap<Integer, List<Double>>();
            List<List<Double>> temporaryList = new Gson().fromJson(json, new TypeToken<List<List<Double>>>(){}.getType());
            int index = 0;
            for(List<Double> temporaryListMember: temporaryList) {
                fuelSavings.put(index, temporaryListMember);
                index++;
            }

        } else {

            // explicit keys
            // THIS PART IS PERFECT
            fuelSavings = new Gson().fromJson(json, new TypeToken<Map<Integer, List<Double>>>(){}.getType());

        }

        System.out.println(fuelSavings);

    }

}

Any help is appreciated, thanks!

  • 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-11T06:33:36+00:00Added an answer on June 11, 2026 at 6:33 am

    What you do in PHP are two different things. Array in PHP serves as Arrays, Maps, Lists.

    array(array(12), array(13)); and array("0" => array(12), "2" => array(13)); are not the same even in PHP.

    What you need to do in Java using Gson is to parse the json as String and check whether the main object is a Map or a List and then advise. If you have loaded a List then you know it was a array(array(12), array(13)); otherwise it was the other one.

    Can’t you do something from the PHP side to generate it always the same?

    Edit:

    If you cannot do anything from the PHP side then your check will be identical to instantiate a JsonParser parse then checking if JsonObject.isJsonArray() returns true, except that it will be eventually quicker but you won’t rely on the gson library.

    Map<Integer, List<Double>> fuelSavings = null;
    JsonElement jElement = new JsonParser().parse("[[12], [13]]");
    JsonObject jObject = jelement.getAsJsonObject();
    if (jObject.isJsonArray()) {
        fuelSavings = new HashMap<Integer, List<Double>>();
        List<List<Double>> temporaryList = new Gson().fromJson(jElement, new TypeToken<List<List<Double>>>(){}.getType());
        int index = 0;
        for(List<Double> temporaryListMember: temporaryList) {
            fuelSavings.put(index++, temporaryListMember);
        }
    } else /* this is a map */ {
        fuelSavings = new Gson().fromJson(jElement, new TypeToken<Map<Integer, List<Double>>>(){}.getType());
    }
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I am having one problem with the PHP json_encode function. It encodes numbers as
So I have two files: index.php and query.php. The problem is, I don't know
I have a small but weird problem while encoding php arrays to json. I
I have a problem with arrays in PHP. So, lets see: I have a
Problem: Trying to use 'dynamic' variables to access JSON/JavaScript arrays. # EXAMPLE # Instead
I have problem with php script. The thing is that it shows me, that
I have a problem in php code inserting values into database (I use PHPMyAdmin).
I have a problem with php header redirect. I already spent hours trying to
I'm having a problem with php's for loop. The loop iterates too fast and
I have a bizzare problem with php date function. code: $numDays = 8; $date

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.