0

This is my json:

{  
   "all_counts_reports":{  
      "26":{  
         "name":"kumar",
         "date":"2017-04-27",
         "trips_per_day":"2",
         "cash_trips":"0",
         "credit_trips":"1",
         "compliment_trips":"1"
      },
      "28":{  
         "name":"kumar",
         "date":"2017-04-29",
         "trips_per_day":"1",
         "cash_trips":"1",
         "credit_trips":"0",
         "compliment_trips":"0"
      }
   }
}

I want to remove the second level keys (e.g "26:" and "28":) using PHP.

In other words, I want to replace the double-quoted number keys with zero-indexed numeric keys.

How can I make it look like this:

{"all_counts_reports":
    [
        {"name":"kumar",
         "date":"2017-04-27",
         "trips_per_day":"2",
         "cash_trips":"0",
         "credit_trips":"1",
         "compliment_trips":"1"
        },
        {"name":"kumar",
         "date":"2017-04-29",
         "trips_per_day":"1",
         "cash_trips":"1",
         "credit_trips":"0",
         "compliment_trips":"0"
        }
    ]
}
mickmackusa
  • 43,625
  • 12
  • 83
  • 136
  • 1
    If you literally just removed them, you'd have invalid JSON, which presumably you don't want. What result *do* you want? Separately: It's unclear what you're asking: Are you trying to use PHP code to parse and transform this JSON, or are you getting this result from existing PHP code and want to change that code so it produces a different result? – T.J. Crowder Apr 29 '17 at 08:47
  • This feels like an [XY problem](https://meta.stackexchange.com/questions/66377/what-is-the-xy-problem). What are you _really_ trying to do? – Bart Friederichs Apr 29 '17 at 08:47
  • ... and of course... What have you tried yourself? – mickmackusa Apr 29 '17 at 08:48
  • Yes i want to pass my json into android app, this values not posting to android because of that values.@T.J.Crowder – teddycherry kj Apr 29 '17 at 08:50
  • 1
    It is now time for you to update your question to provide a response to all of the comments up to this point. Without doing this, the answers will be based assumptions. – mickmackusa Apr 29 '17 at 08:52
  • sorry for my fault – teddycherry kj Apr 29 '17 at 09:54

2 Answers2

1

Here is the demo.

Code:

// declare $json
$array=json_decode($json,true);  // decode as array
// overwrite subarray with zero-indexed keys while preserving subarray values
$array['all_counts_reports']=array_values($array['all_counts_reports']);
var_export(json_encode($array));  // return to json

Input:

$json='{  
   "all_counts_reports":{  
      "26":{  
         "name":"kumar",
         "date":"2017-04-27",
         "trips_per_day":"2",
         "cash_trips":"0",
         "credit_trips":"1",
         "compliment_trips":"1"
      },
      "28":{  
         "name":"kumar",
         "date":"2017-04-29",
         "trips_per_day":"1",
         "cash_trips":"1",
         "credit_trips":"0",
         "compliment_trips":"0"
      }
   }
}';

Output:

'{"all_counts_reports":
    [
        {"name":"kumar",
         "date":"2017-04-27",
         "trips_per_day":"2",
         "cash_trips":"0",
         "credit_trips":"1",
         "compliment_trips":"1"
        },
        {"name":"kumar",
         "date":"2017-04-29",
         "trips_per_day":"1",
         "cash_trips":"1",
         "credit_trips":"0",
         "compliment_trips":"0"
        }
    ]
}'

In your javascript, use JSON.parse() to strip the wrapping single quotes:

var str='{"all_counts_reports":[{"name":"kumar","date":"2017-04-27","trips_per_day":"2","cash_trips":"0","credit_trips":"1","compliment_trips":"1"},{"name":"kumar","date":"2017-04-29","trips_per_day":"1","cash_trips":"1","credit_trips":"0","compliment_trips":"0"}]}';
var json=JSON.parse(str);
console.log(json);

And because we are running with assumptions, if you want to remove the all_counts_reports key as well, you can use this one-liner:

Code:

$new_json=json_encode(array_values(current(json_decode($json,true))));

Output:

'[{"name":"kumar","date":"2017-04-27","trips_per_day":"2","cash_trips":"0","credit_trips":"1","compliment_trips":"1"},{"name":"kumar","date":"2017-04-29","trips_per_day":"1","cash_trips":"1","credit_trips":"0","compliment_trips":"0"}]'
mickmackusa
  • 43,625
  • 12
  • 83
  • 136
  • its working but at start and end have a single quotes. and i fix that using `$result = str_replace('"', '', json_encode($array));` this – teddycherry kj Apr 29 '17 at 09:54
  • @teddycherrykj `JSON.parse()` https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/JSON/parse – mickmackusa Apr 29 '17 at 11:24
  • @teddycherrykj I have done a substantial edit on your question. If there is anything that is misrepresenting your question, please feel free to edit the question again so that it is right and true. – mickmackusa Apr 29 '17 at 12:14
0
$array = json_decode($your_json_string,true);
print_r($array);
$result = array();
foreach($array['all_counts_reports'] as $rep){
    $result['all_counts_reports'][] = array(
        "name"=>$rep['name'],
        "date"=>$rep['date'],
        "trips_per_day"=>$rep['trips_per_day'],
        "cash_trips"=>$rep['cash_trips'],
        "credit_trips"=>$rep['credit_trips'],
        "compliment_trips"=>$rep['compliment_trips'], 
    );
}

$result_json = json_encode($result);
echo $result_json;

There may be better solution, but this one is right now in my mind

Its unclear that what you looking for, if you just want to remove and dont want to maintain as original json then you can do like this example. But if you dont want your all_counts_reports treated as array [] then this example will not help.

And simply pass to android then above will work.

BetaDev
  • 4,516
  • 3
  • 21
  • 47