Reputation: 739
I have array in php and convert that in json.Now I have this code in json format
{
"2": "12:40 to 13:0",
"3": "13:0 to 13:20",
"4": "13:20 to 13:40",
"5": "13:40 to 14:0",
"6": "14:0 to 14:20",
"7": "14:20 to 14:40",
"8": "14:40 to 15:0",
"9": "15:0 to 15:20",
"10": "15:20 to 15:40",
"11": "15:40 to 16:0",
"12": "16:0 to 16:20",
"13": "16:20 to 16:40",
"14": "16:40 to 17:0",
"15": "17:0 to 17:20",
"16": "17:20 to 17:40",
"17": "17:40 to 18:0"
} in json format but i want to remove indexes 2,3,4,5 form code and want data in
{
"12:40 to 13:0",
"13:0 to 13:20",
"13:20 to 13:40",
"13:40 to 14:0",
"14:0 to 14:20",
"14:20 to 14:40",
"14:40 to 15:0",
"15:0 to 15:20",
"15:20 to 15:40",
"15:40 to 16:0",
"16:0 to 16:20",
"16:20 to 16:40",
"16:40 to 17:0",
"17:0 to 17:20",
"17:20 to 17:40",
"17:40 to 18:0"
}
any help :)
Upvotes: 3
Views: 2328
Reputation: 31749
Try with -
$json = '{
"2": "12:40 to 13:0",
"3": "13:0 to 13:20",
"4": "13:20 to 13:40",
"5": "13:40 to 14:0",
"6": "14:0 to 14:20",
"7": "14:20 to 14:40",
"8": "14:40 to 15:0",
"9": "15:0 to 15:20",
"10": "15:20 to 15:40",
"11": "15:40 to 16:0",
"12": "16:0 to 16:20",
"13": "16:20 to 16:40",
"14": "16:40 to 17:0",
"15": "17:0 to 17:20",
"16": "17:20 to 17:40",
"17": "17:40 to 18:0"
}';
$data = (array)json_decode($json); //your json data
$data_values = array_values($data);//if you want array
$newStr = "{".implode(',', $data_values)."}"; // if you want string
$newJson = json_encode($data_values); //if you want json
Upvotes: 0
Reputation: 12127
try this
<?php
$json = '{
"2": "12:40 to 13:0",
"3": "13:0 to 13:20",
"4": "13:20 to 13:40",
"5": "13:40 to 14:0",
"6": "14:0 to 14:20",
"7": "14:20 to 14:40",
"8": "14:40 to 15:0",
"9": "15:0 to 15:20",
"10": "15:20 to 15:40",
"11": "15:40 to 16:0",
"12": "16:0 to 16:20",
"13": "16:20 to 16:40",
"14": "16:40 to 17:0",
"15": "17:0 to 17:20",
"16": "17:20 to 17:40",
"17": "17:40 to 18:0"
}';
echo json_encode(array_values(json_decode($json, true)));
?>
{"12:40 to 13:0","13:0 to 13:20",.....}
this is not valid json format when key does not in JOSN.
correct format is ["12:40 to 13:0","13:0 to 13:20","13:20 to 13:40".....]
Upvotes: 0
Reputation: 683
I hope you want to remove the index from the JSON, here is the way:
$json = '{
"2": "12:40 to 13:0",
"3": "13:0 to 13:20",
"4": "13:20 to 13:40",
"5": "13:40 to 14:0",
"6": "14:0 to 14:20",
"7": "14:20 to 14:40",
"8": "14:40 to 15:0",
"9": "15:0 to 15:20",
"10": "15:20 to 15:40",
"11": "15:40 to 16:0",
"12": "16:0 to 16:20",
"13": "16:20 to 16:40",
"14": "16:40 to 17:0",
"15": "17:0 to 17:20",
"16": "17:20 to 17:40",
"17": "17:40 to 18:0"
}';
$json = json_encode(array_values(json_decode($json,true)));
Upvotes: 0
Reputation: 23978
Yes, you can remove keys and keep just values using PHP's array_values
Just do json_decode
and then use array_values
.
By default, array indexes are numeric.
If you generate the array e.g. $arr = array('PHP', 'JAVA')
, you will get
This is numeric array
$arr = (0 => 'PHP', '1' => 'JAVA')
Now, if we create a new associative array.
$arr = ('scripting' => 'PHP', 'programming' => 'JAVA')
So, array_values converts the array into numeric array from associative array.
Upvotes: 2