How to remove first array name from json - php

I'm Getting values like this in JSON format -
{
"comments": [{
"name": "ABC",
"desc": "Hello...",
"values": [{
"status": "fine",
"label": ""
}]
}, {
"name": "XYZ",
"desc": "Good Morning..",
"values": [{
"status": "fine",
"label": "happy"
}]
}]
}
But i don't want first array name means i need result like this -
[{
"name": "ABC",
"desc": "Hello...",
"values": [{
"status": "fine",
"label": ""
}]
}, {
"name": "XYZ",
"desc": "Good Morning..",
"values": [{
"status": "fine",
"label": "happy"
}]
}]
Need a help...

Do like this...
<?php
$json='{
"comments": [{
"name": "ABC",
"desc": "Hello...",
"values": [{
"status": "fine",
"label": ""
}]
}, {
"name": "XYZ",
"desc": "Good Morning..",
"values": [{
"status": "fine",
"label": "happy"
}]
}]
}';
$arr=json_decode($json,1);
echo json_encode($arr['comments']);
OUTPUT :
[{"name":"ABC","desc":"Hello...","values":[{"status":"fine","label":""}]},{"name":"XYZ","desc":"Good Morning..","values":[{"status":"fine","label":"happy"}]}]

Try this:
$data = '[{
"name": "ABC",
"desc": "Hello...",
"values": [{
"status": "fine",
"label": ""
}]
}, {
"name": "XYZ",
"desc": "Good Morning..",
"values": [{
"status": "fine",
"label": "happy"
}]
}]';
$return = json_decode($data, true);
foreach ($return as $key => $value){
unset($return[$key]['name']);
}
echo '<pre>';
print_r($return);
echo '</pre>';

Related

Array filter JSON data with price parameter

I have this JSON data and I am using this code to match the title. It works, but now I want to go one step further and list all products which are less than some price, or more than some price, or equal to some price
$json = '[{
"id": 2042049298481,
"title": "Test Product 53",
"variants": [{
"id": 15733087797297,
"title": "Default Title",
"price": "100.00",
"sku": "no"
}]
}, {
"id": 2196682342449,
"title": "its test yar",
"variants": [{
"id": 16385289879601,
"title": "Default Title",
"price": "144.00",
"sku": "no"
}]
}, {
"id": 4175970041905,
"title": "uhjkjhkhk",
"variants": [{
"id": 30302326554673,
"title": "Default Title",
"price": "1000.00",
"sku": "10"
}]
}, {
"id": 4183828791345,
"title": "kaminsss",
"variants": [{
"id": 30346449518641,
"title": "Default Title",
"price": "111.00",
"sku": "no"
}]
}, {
"id": 4247884890161,
"title": "hellooo",
"variants": [{
"id": 30579860832305,
"title": "Default Title",
"price": "1111.00",
"sku": "no"
}]
}, {
"id": 4248143822897,
"title": "10oct latest collection",
"variants": [{
"id": 31157780938801,
"title": "50",
"price": "111.00",
"sku": "no-1"
}, {
"id": 31157802270769,
"title": "60",
"price": "101.00",
"sku": ""
}, {
"id": 31157804761137,
"title": "70",
"price": "111.00",
"sku": ""
}]
}, {
"id": 4284600746033,
"title": "18oct2019",
"variants": [{
"id": 31595410030641,
"title": "120 / black",
"price": "100.00",
"sku": "10"
}]
}, {
"id": 4285596860465,
"title": "test2222",
"variants": [{
"id": 30877916921905,
"title": "Default Title",
"price": "111.00",
"sku": "no"
}]
}]';
$array = json_decode($json,1);
$filter_name ='title';
$filter_value ='Test';
$expected88 = array_filter($array, function($el) use ($filter_value, $filter_name) {
return ($el[$filter_name] >= $filter_value);
});
echo json_encode($expected88,true);
This works flawlessly for title but we want to filter based on variant price. There may be several prices for one variant and if $filter_value is greater than, equal to, or less than, anyone can occur.
For example user may search for product whose minimum price is 50, so in that case, if any variant is matching criteria then it will list those products, or user may search that all variants' prices should be less than 50.
We tried like this
$expected = array_filter($array, function ($var) use ($filter_value) {
return ($var[$filter_name] >= $filter_value);
});
but it did not work.
We tried foreach and tested again but it always gave empty result
Here is possible solution
$filter_name ='variants';
$filter_value = 200;
$filter_field = "price";
$expected88 = array_filter($array, function($el) use ($filter_value, $filter_name, $filter_field) {
return ((int)$el[$filter_name][0][$filter_field] >= (int)$filter_value);
});

Sorting only a part of object/array in PHP

I've been struggling around this for a while now and can't seem to get the right algorithm.
What I want is to sort this:
$variable = '{
"0": {
"id": "0",
"code": "main_colour",
"label": "Main Colour",
"options": [{
"id": "825",
"label": "White",
"products": ["1", "2", "3"]
}, {
"id": "840",
"label": "Cream",
"products": ["3", "4", "5"]
}],
"position": "0"
},
"2": {
"id": "0",
"code": "size",
"label": "Size",
"options": [{
"id": "825",
"label": "S",
"products": ["1", "2", "3"]
}, {
"id": "840",
"label": "M",
"products": ["3", "4", "5"]
}],
"position": "0"
}
}';
And the output would be
$variable = '{
"0": {
"id": "0",
"code": "main_colour",
"label": "Main Colour",
"options": [{
"id": "840",
"label": "Cream",
"products": ["3", "4", "5"]
},{
"id": "825",
"label": "White",
"products": ["1", "2", "3"]
}],
"position": "0"
},
"2": {
"id": "0",
"code": "size",
"label": "Size",
"options": [{
"id": "840",
"label": "M",
"products": ["3", "4", "5"]
}, {
"id": "825",
"label": "S",
"products": ["1", "2", "3"]
}],
"position": "0"
}
}';
So basically it should be sorted using the label inside the "options" without affecting the other fields. I've been trying usort and tried different algorithms but to no avail.
Current code that sorts only the outside "label" and not the inner label:
function optionsSort($a, $b) {
return strcmp($a['options'][0]['label'], $b['options'][0]['options']['label']);
}
usort($variable, "optionsSort"));
Here's the code:
// your array
$variable = '{
"0": {
"id": "0",
"code": "main_colour",
"label": "Main Colour",
"options": [{
"id": "825",
"label": "White",
"products": ["1", "2", "3"]
}, {
"id": "840",
"label": "Cream",
"products": ["3", "4", "5"]
}],
"position": "0"
},
"2": {
"id": "0",
"code": "size",
"label": "Size",
"options": [{
"id": "825",
"label": "S",
"products": ["1", "2", "3"]
}, {
"id": "840",
"label": "M",
"products": ["3", "4", "5"]
}],
"position": "0"
}
}';
$a = json_decode($variable);
foreach ($a as $item) {
// sorting function
usort($item->options, function($a, $b) { return strcmp($a->label, $b->label); });
}

REGEX: needed in php [duplicate]

This question already has an answer here:
How to extract individual elements from a JSON string?
(1 answer)
Closed 5 years ago.
{
"status": "success",
"data": {
"title": "test2",
"json_query": {
"condition": "AND",
"rules": [{
"id": "event",
"field": "event",
"type": "string",
"operator": "equal",
"value": "signup"
}, {
"condition": "AND",
"rules": [{
"id": "event",
"field": "event",
"type": "string",
"operator": "equal",
"value": "signup"
}, {
"condition": "AND",
"rules": [{
"id": "kwd",
"field": "kwd",
"type": "string",
"operator": "equal",
"value": "epub"
}]
}]
}, {
"id": "kwd",
"field": "kwd",
"type": "string",
"operator": "equal",
"value": "arts"
}, {
"condition": "AND",
"rules": [{
"id": "kwd",
"field": "kwd",
"type": "string",
"operator": "equal",
"value": "automotive"
}, {
"condition": "AND",
"rules": [{
"id": "kwd",
"field": "kwd",
"type": "string",
"operator": "equal",
"value": "books"
}, {
"condition": "AND",
"rules": [{
"id": "kwd",
"field": "kwd",
"type": "string",
"operator": "equal",
"value": "business"
}, {
"condition": "AND",
"rules": [{
"id": "kwd",
"field": "kwd",
"type": "string",
"operator": "equal",
"value": "books"
}]
}]
}]
}]
}]
}
}
}
This is my string and i need the regex which will get all id with its value
eg: "id" : "event", "id" : "event", "id" : "kwd", "id" : "kwd"
means every occurrence, so i can replace or add a unique value to id
NOTE:- I dont want to make an array, dont want to do with loop,
finding possibility to replace with preg_match or any other regex method.
I got my answer myself:
preg_match_all('/"id":"(.*)",/U', $json, $matches, PREG_PATTERN_ORDER);
print_r($matches); die;

issues when looping throw a json on php

this is driving me nuts,
I'm trying to loop throw a specific json:
I tried using
json_encode -> throws Warning: curl_close() expects parameter 1 to be resource, null given
json_decode -> shows the same text
$json->{"SKU"} and $json->SKU shows the same text
I tried to validate the json with different websites and all say the json is correct
I want to take only this values SKU, price, specialprice (with everything inside) and quantity
[{
"SKU": "118075",
"BrandName": "DIVIDED",
"CategoryName": "Correas",
"Categories": [{
"Id": "423",
"CategoryName": "Moda",
"LevelDepth": "2",
"Parent": null
}, {
"Id": "960",
"CategoryName": "Accesorios de Mujer",
"LevelDepth": "3",
"Parent": "Moda"
}],
"Name": "Divided correa Carrie",
"Description": "a",
"DescriptionShort": "v",
"MetaDescription": "Correa Divided Carrie en presentaci\u00f3n de tres colores.",
"MetaTitle": "Divided Correa Carrie",
"IsPublished": "1",
"PackageWeight": "1.000000",
"Price": 39.9,
"FinalPrice": 16.3,
"SpecialPrice": {
"StartDate": "2014-11-21 06:00:00",
"EndDate": "2017-12-31 23:59:00",
"Reduction": "20.000000",
"ReductionType": "amount"
},
"Quantity": "39",
"DateAdd": "2015-09-24 11:26:08",
"DateUpd": "2016-10-12 12:51:13",
"Images": [{
"Id": "77",
"Cover": "1",
"Legend": "Divided correa Carrie",
"url": "https:\/\/www.google.com.pe\/img\/p\/7\/7\/77.jpg"
}, {
"Id": "78",
"Cover": "0",
"Legend": "Divided correa Carrie",
"url": "https:\/\/www.google.com.pe\/img\/p\/7\/8\/78.jpg"
}, {
"Id": "79",
"Cover": "0",
"Legend": "Divided correa Carrie",
"url": "https:\/\/www.google.com.pe\/img\/p\/7\/9\/79.jpg"
}],
"FlatVariations": [{
"SKU": "001503280",
"Price": 39.9,
"FinalPrice": 16.3,
"Default": "1",
"IdImage": "77",
"Attributes": [{
"Name": "Color",
"Value": "Naranja"
}]
}, {
"SKU": "001503281",
"Price": 39.9,
"FinalPrice": 39.9,
"Default": "0",
"IdImage": "78",
"Attributes": [{
"Name": "Color",
"Value": "Blanco"
}]
}, {
"SKU": "001503282",
"Price": 39.9,
"FinalPrice": 39.9,
"Default": "0",
"IdImage": "79",
"Attributes": [{
"Name": "Color",
"Value": "Negro"
}]
}]
}]
Thanks a lot for any hint (I already tried almost every answer on stack)
I'm pretty sure you can do it in this way:
<?php
//Enter your code here, enjoy!
$text = <<<EOT
[{
"SKU": "118075",
"BrandName": "DIVIDED",
"CategoryName": "Correas",
"Categories": [{
"Id": "423",
"CategoryName": "Moda",
"LevelDepth": "2",
"Parent": null
}, {
"Id": "960",
"CategoryName": "Accesorios de Mujer",
"LevelDepth": "3",
"Parent": "Moda"
}],
"Name": "Divided correa Carrie",
"Description": "a",
"DescriptionShort": "v",
"MetaDescription": "Correa Divided Carrie en presentaci\u00f3n de tres colores.",
"MetaTitle": "Divided Correa Carrie",
"IsPublished": "1",
"PackageWeight": "1.000000",
"Price": 39.9,
"FinalPrice": 16.3,
"SpecialPrice": {
"StartDate": "2014-11-21 06:00:00",
"EndDate": "2017-12-31 23:59:00",
"Reduction": "20.000000",
"ReductionType": "amount"
},
"Quantity": "39",
"DateAdd": "2015-09-24 11:26:08",
"DateUpd": "2016-10-12 12:51:13",
"Images": [{
"Id": "77",
"Cover": "1",
"Legend": "Divided correa Carrie",
"url": "https:\/\/www.google.com.pe\/img\/p\/7\/7\/77.jpg"
}, {
"Id": "78",
"Cover": "0",
"Legend": "Divided correa Carrie",
"url": "https:\/\/www.google.com.pe\/img\/p\/7\/8\/78.jpg"
}, {
"Id": "79",
"Cover": "0",
"Legend": "Divided correa Carrie",
"url": "https:\/\/www.google.com.pe\/img\/p\/7\/9\/79.jpg"
}],
"FlatVariations": [{
"SKU": "001503280",
"Price": 39.9,
"FinalPrice": 16.3,
"Default": "1",
"IdImage": "77",
"Attributes": [{
"Name": "Color",
"Value": "Naranja"
}]
}, {
"SKU": "001503281",
"Price": 39.9,
"FinalPrice": 39.9,
"Default": "0",
"IdImage": "78",
"Attributes": [{
"Name": "Color",
"Value": "Blanco"
}]
}, {
"SKU": "001503282",
"Price": 39.9,
"FinalPrice": 39.9,
"Default": "0",
"IdImage": "79",
"Attributes": [{
"Name": "Color",
"Value": "Negro"
}]
}]
}]
EOT;
$decoded = json_decode($text);
# Index 0 because is an array
var_dump($decoded[0]->SKU);
You can execute the code directly here.
Hope this helps.

PHP Troubles with converting from JSON to stdClass, making changes, and then converting back to JSON

So, in the very beginning, before the send_sms.php is loaded, I have this Json stored in a database:
{
"chats": {
"chat": [{
"id": "1",
"name": "Ethan Wilberforce",
"messages": {
"message": [{
"id": "1",
"name": "Ethan Wilberforce",
"text": "Hello how are you doing",
"time": "4:41"
}, {
"id": "2",
"name": "Qasim Iqbal",
"text": "Not bad. How about you?",
"time": "4:42"
}, {
"id": "3",
"name": "Ethan Wilberforce",
"text": "I'm not too bad myself.",
"time": "4:43"
}]
}
}, {
"id": "2",
"name": "Geoff Vahaaho",
"messages": {
"message": [{
"id": "1",
"name": "Geoff Vahaaho",
"text": "Hello how are you doing",
"time": "4:41"
}, {
"id": "2",
"name": "Qasim Iqbal",
"text": "Not bad. How about you?",
"time": "4:42"
}, {
"id": "3",
"name": "Geoff Vahaaho",
"text": "I'm not too bad myself.",
"time": "4:43"
}, {
"id": "4",
"name": "Qasim Iqbal",
"text": "Nice.",
"time": "4:43"
}]
}
}]
}
}
The Json is completely valid, no errors. It is storing two chats, with messages in them.
Now, here is the PHP code that alters the Json:
$data = $user->data;
$parsed_data = json_decode($data);
...
for($i = 0, $size = sizeof($parsed_data->chats->chat); $i < $size; ++$i) {
if($parsed_data->chats->chat[$i]->name == $to) {
$found = true;
$parsed_data->chats->chat[$i]->messages->message[sizeof($parsed_data->chats->chat[$i]->messages->message)] = new stdClass;
$parsed_data->chats->chat[$i]->messages->message[sizeof($parsed_data->chats->chat[$i]->messages->message)]->id = sizeof($parsed_data->chats->chat[$i]->messages->message);
$parsed_data->chats->chat[$i]->messages->message[sizeof($parsed_data->chats->chat[$i]->messages->message)]->name = $user->name;
$parsed_data->chats->chat[$i]->messages->message[sizeof($parsed_data->chats->chat[$i]->messages->message)]->text = $message;
$parsed_data->chats->chat[$i]->messages->message[sizeof($parsed_data->chats->chat[$i]->messages->message)]->time = $time;
echo "done. ";
break;
}
}
What I intend this to do is, to add another stdClass object in the "message" array of the chat. So I basically do just that, hoping it will work.
Now, it works, kind of, but here is the new Json after we json_encode it:
{
"chats": {
"chat": [{
"id": "1",
"name": "Ethan Wilberforce",
"messages": {
"message": [{
"id": "1",
"name": "Ethan Wilberforce",
"text": "Hello how are you doing",
"time": "4:41"
}, {
"id": "2",
"name": "Qasim Iqbal",
"text": "Not bad. How about you?",
"time": "4:42"
}, {
"id": "3",
"name": "Ethan Wilberforce",
"text": "I'm not too bad myself.",
"time": "4:43"
}, {}, {
"id": 4
}, {
"name": "Qasim Iqbal"
}, {
"text": "Hello i am testing"
}, {
"time": 1326066200
}]
}
}, {
"id": "2",
"name": "Geoff Vahaaho",
"messages": {
"message": [{
"id": "1",
"name": "Geoff Vahaaho",
"text": "Hello how are you doing",
"time": "4:41"
}, {
"id": "2",
"name": "Qasim Iqbal",
"text": "Not bad. How about you?",
"time": "4:42"
}, {
"id": "3",
"name": "Geoff Vahaaho",
"text": "I'm not too bad myself.",
"time": "4:43"
}, {
"id": "4",
"name": "Qasim Iqbal",
"text": "Nice.",
"time": "4:43"
}]
}
}]
}
}
You will notice it was indeed added in the "Ethan Wilberforce" chat, but each string in the stdClass was converted to its own array item in the "message" array. How could I fix this problem? Many thanks.
Your problem is this:
$parsed_data->chats->chat[$i]->messages->message[sizeof($parsed_data->chats->chat[$i]->messages->message)] = new stdClass;
$parsed_data->chats->chat[$i]->messages->message[sizeof($parsed_data->chats->chat[$i]->messages->message)]->id = sizeof($parsed_data->chats->chat[$i]->messages->message);
It basically amounts to:
$array[$last] = new stdClass();
$array[$last+1] = "id";
$array[$last+2] = "name";
You keep appending new arrays/objects, because you use sizeof(...) which always becomes one larger than the previous line. It's not the last index, but the size. Which is $lastindex+1.
What you should be doing anyway, is not using an object, but just appending an array, and with all its attributes at once:
$parsed_data->chats->chat[$i]->messages->message[] = array(
"id" => ...,
"name" => ...,
"time" => ...,
);
When you encode that associative array back into JSON it will also become a normal {...} JSON object group.

Categories