Iterate the loop in angular js (ng-repeat) - php

This is my json object. How do I loop through test and testing values inside data_options->"key"->options and show them in checkbox values?
{
"data": {
"0": {
"id": 2,
"survey_id": 14,
"question": "What's here?",
"question_type": 2,
"created_at": "2016-07-15 18:29:05",
"updated_at": "2016-07-15 18:29:05",
"deleted_at": null,
"type": "checkbox",
"ques_options_id": 2,
"key": "pqr",
"value": "PQR",
"survey_user_id": 39
},
"1": {
"id": 9,
"survey_id": 14,
"question": "Whats demo? ",
"question_type": 4,
"created_at": "2016-07-15 18:29:05",
"updated_at": "2016-07-15 18:29:05",
"deleted_at": null,
"type": "dropdown",
"ques_options_id": 9,
"key": "checking",
"value": "Checking",
"survey_user_id": 39
},
"2": {
"id": 1,
"survey_id": 14,
"question": "What's in your mind?",
"question_type": 1,
"created_at": "2016-07-15 18:29:05",
"updated_at": "2016-07-15 18:29:05",
"deleted_at": null,
"type": "textarea",
"ques_options_id": 1,
"key": "abc",
"value": "ABC",
"survey_user_id": 39
},
"3": {
"id": 8,
"survey_id": 14,
"question": "Whats abc here?",
"question_type": 3,
"created_at": "2016-07-15 18:29:05",
"updated_at": "2016-07-15 18:29:05",
"deleted_at": null,
"type": "radio",
"ques_options_id": 8,
"key": "abcde",
"value": "ABCDEF",
"survey_user_id": 39
},
"4": {
"id": 10,
"survey_id": 14,
"question": "Testing here?",
"question_type": 2,
"created_at": "2016-07-15 18:29:05",
"updated_at": "2016-07-15 18:29:05",
"deleted_at": null,
"type": "checkbox",
"ques_options_id": 10,
"key": "test",
"value": "Test",
"survey_user_id": 39
},
"5": {
"id": 10,
"survey_id": 14,
"question": "Testing here?",
"question_type": 2,
"created_at": "2016-07-15 18:29:05",
"updated_at": "2016-07-15 18:29:05",
"deleted_at": null,
"type": "checkbox",
"ques_options_id": 10,
"key": "Testing",
"value": "Testing",
"survey_user_id": 39
},
"data_option": {
"1": {
"options": {
"0": {
"abc": "ABC"
}
},
"question": "What's in your mind?",
"type": "textarea"
},
"2": {
"options": {
"0": {
"pqr": "PQR"
}
},
"question": "What's here?",
"type": "checkbox"
},
"8": {
"options": {
"0": {
"abcde": "ABCDEF"
}
},
"question": "Whats abc here?",
"type": "radio"
},
"9": {
"options": {
"0": {
"checking": "Checking"
}
},
"question": "Whats demo? ",
"type": "dropdown"
},
"10": {
"options": {
"0": {
"test": "Test"
},
"1": {
"Testing": "Testing"
}
},
"question": "Testing here?",
"type": "checkbox"
}
}
}
}

First convert object to array and then you can use ng-repeat on array as below:
var myObj = {
0: {
"id": 1,
"survey_id": 12
},
1: {
"id": 2,
"survey_id": 14
},
};
var array = $.map(myObj, function(value, index) {
return [value];
});

You can do this in view,
<div ng-app="myApp" ng-controller="ListCtrl">
<div ng-repeat="patient in data.data.data_option ">
<div ng-repeat="detail in patient.options ">
<input type="checkbox">
<span class="done-{{todo.done}}">{{detail}}</span>
</div>
</div>
Working Application

Related

Doctrine Issue - Entities not joining correctly

So I'm having a strange problem with Doctrine that I don't understand and need help with. Essentially, I am running quite a complex query due to the nature of my database and its various joins and for some reason the response when I grab try to grab all records from my database is returning my entities incorrectly.
See my doctrine query below:
$subQuery = $this->_em->createQueryBuilder()
->from(Product::class, 'ex')
->select('ppsi.id')
->join('ex.productSpecificationItems', 'ppsi')
->where('ex.id = p.id');
$subQuery = $subQuery->getDQL();
$subQuery2 = $this->_em->createQueryBuilder()
->from(Product::class, 'ex2')
->select('ppoiv.id')
->join('ex2.productOptionItemValues', 'ppoiv')
->where('ex2.id = p.id');
$subQuery2 = $subQuery2->getDQL();
$subQuery3 = $this->_em->createQueryBuilder()
->from(Product::class, 'ex3')
->select('poie.id')
->join('ex3.productOptionItems', 'poie')
->where('ex3.id = p.id');
$subQuery3 = $subQuery3->getDQL();
$qb = $this->createQueryBuilder('p')
->select('p, pc, psc, i, po, poi, poiv, ps, psi')
->join('p.productCategory', 'pc')
->leftJoin('p.productSubCategory', 'psc')
->leftJoin('p.images', 'i')
->leftJoin('p.productOptions', 'po')
->leftJoin('p.productSpecifications', 'ps')
->leftJoin('ps.productSpecificationItems', 'psi', Join::WITH, 'psi.id IN (' . $subQuery. ')')
->leftJoin('po.productOptionItems', 'poi', Join::WITH, 'poi.id IN (' . $subQuery3. ')')
->leftJoin('poi.productOptionItemValue', 'poiv', Join::WITH, 'poiv.id IN (' . $subQuery2. ')')
->addOrderBy('p.id', 'ASC');
The response of this causes some entities to have the productOptionItemValues either listed under the wrong productOptionItem, just causes blank records to return, or doesn't return them at all. See an example below (as a JSON for clarity):
{
"id": 373,
"name": "test",
"code": "test",
"url": "373-test",
"price": "1.00",
"stock": 1,
"description": "<p>fghfgh mnsdbfksdhfjk sd</p>\n",
"live": false,
"createdAt": "2022-03-17T14:45:35+00:00",
"updatedAt": "2022-03-17T15:12:44+00:00",
"featured": false,
"aggregateReviewScore": null,
"productCategory": {
"id": 2,
"name": "Hex Set Screws",
"url": "hex-set-screws"
},
"productSubCategory": {
"id": 12,
"name": "Nylon",
"url": "nylon"
},
"images": [],
"productOptions": [
{
"id": 1,
"name": "Colour",
"productOptionItems": {
"0": {
"id": 2,
"name": "Black",
"productOptionItemValue": [
{
"id": 1196,
"stock": 1,
"price": "1.00",
"useOwn": false
},
{
"id": 1201,
"stock": 1,
"price": "1.00",
"useOwn": false
},
{
"id": 1198,
"stock": 1,
"price": "1.00",
"useOwn": false
}
]
},
"1": {
"id": 8,
"name": "Yellow",
"productOptionItemValue": [
{
"id": 1197,
"stock": 1,
"price": "1.00",
"useOwn": false
}
]
},
"2": {
"id": 14,
"name": "Light Grey"
},
"3": null,
"4": {
"id": 15,
"name": "Mid Grey"
},
"7": null,
"8": {
"id": 20,
"name": "Light Blue"
},
"11": null,
"12": {
"id": 22,
"name": "Purple"
},
"13": null
}
},
{
"id": 2,
"name": "Pack Size",
"productOptionItems": [
{
"id": 3,
"name": "10",
"productOptionItemValue": [
{
"id": 1180,
"stock": 100,
"price": "10.00",
"useOwn": true
}
]
},
{
"id": 4,
"name": "100",
"productOptionItemValue": [
{
"id": 1181,
"stock": 10,
"price": "1000.00",
"useOwn": true
}
]
}
]
}
],
"productSpecifications": []
}
If I run the same query on a SINGLE product (so the same Doctrine Query as a above just with a p.id = :id WHERE added) it returns absolutely fine. See the response for that below (and the CORRECT response I am looking for):
{
"id": 373,
"name": "test",
"code": "test",
"url": "373-test",
"price": 1,
"stock": 1,
"description": "<p>fghfgh mnsdbfksdhfjk sd</p>\n",
"live": false,
"createdAt": "2022-03-17T14:45:35+00:00",
"updatedAt": "2022-03-17T15:12:44+00:00",
"featured": false,
"aggregateReviewScore": null,
"productCategory": {
"id": 2,
"name": "Hex Set Screws",
"url": "hex-set-screws"
},
"productSubCategory": {
"id": 12,
"name": "Nylon",
"url": "nylon"
},
"images": [],
"productOptions": [
{
"id": 1,
"name": "Colour",
"productOptionItems": [
{
"id": 2,
"name": "Black",
"productOptionItemValue": {
"id": 1196,
"stock": 1,
"price": 1,
"useOwn": false
}
},
{
"id": 7,
"name": "Red",
"productOptionItemValue": {
"id": 1201,
"stock": 1,
"price": 1,
"useOwn": false
}
},
{
"id": 8,
"name": "Yellow",
"productOptionItemValue": {
"id": 1197,
"stock": 1,
"price": 1,
"useOwn": false
}
},
{
"id": 14,
"name": "Light Grey",
"productOptionItemValue": {
"id": 1202,
"stock": 1,
"price": 1,
"useOwn": false
}
},
{
"id": 15,
"name": "Mid Grey",
"productOptionItemValue": {
"id": 1205,
"stock": 1,
"price": 1,
"useOwn": false
}
},
{
"id": 20,
"name": "Light Blue",
"productOptionItemValue": {
"id": 1203,
"stock": 1,
"price": 1,
"useOwn": false
}
},
{
"id": 22,
"name": "Purple",
"productOptionItemValue": {
"id": 1204,
"stock": 1,
"price": 1,
"useOwn": false
}
},
{
"id": 23,
"name": "Natural",
"productOptionItemValue": {
"id": 1198,
"stock": 1,
"price": 1,
"useOwn": false
}
}
]
},
{
"id": 2,
"name": "Pack Size",
"productOptionItems": [
{
"id": 3,
"name": "10",
"productOptionItemValue": {
"id": 1180,
"stock": 100,
"price": 10,
"useOwn": true
}
},
{
"id": 4,
"name": "100",
"productOptionItemValue": {
"id": 1181,
"stock": 10,
"price": 1000,
"useOwn": true
}
}
]
}
],
"productSpecifications": []
}
Any help or ideas would be greatly appreciated!

How to return unique values in laravel

Here I have this sample data which returns based on category products and I need to limit repeated values.
Raw JSON
[{
"brand": {
"id": "fe877b45-8620-453a-8805-63f0cbd80752",
"name": "No Brand",
"slug": "no-brand",
"description": "null"
},
"options": [{
"id": "324af955-1aa9-42ea-be6e-bb4e5623a97a",
"parent_id": "null",
"name": "Need Insurance?"
},
{
"id": "73298c18-4ccc-4138-afa5-71d3d00dff9b",
"parent_id": "null",
"name": "Color",
"slug": "color"
}
],
"rating": [],
"tags": [{
"id": "8a31ee4c-3302-4357-9686-bd4308bbf39f",
"name": "options",
"slug": "options",
"photo": "null"
}],
"variations": [{
"id": "3bf5aeb9-9da2-4fb1-a3d2-f89eb75839c3",
"parent_id": "null",
"name": "Ram",
"slug": "ram",
"photo": "null"
},
{
"id": "e5c70766-a558-4539-b41f-77f72c819a7c",
"parent_id": "null",
"name": "cpu",
"slug": "cpu",
"photo": "null"
},
{
"id": "e63ac831-f595-4889-83d2-a5be65734758",
"parent_id": "null",
"name": "Monitor",
"slug": "monitor"
}
]
},
{
"brand": {
"id": "fe877b45-8620-453a-8805-63f0cbd80752",
"name": "No Brand",
"slug": "no-brand",
"description": null
},
"options": [{
"id": "522da418-eb3f-43e9-9392-63c941842a52",
"parent_id": null,
"name": "Color",
"slug": "color-3"
}],
"rating": [],
"tags": [],
"variations": [{
"id": "8e9a26c5-2ee4-4d86-9244-a10596d67fea",
"parent_id": null,
"name": "cpu",
"slug": "cpu-3",
"photo": null
}]
}
]
Sample data
filters: [{,…}, {,…}]
0: {,…}
brand: {id: "fe877b45-8620-453a-8805-63f0cbd80752", name: "No Brand", slug: "no-brand", description: null,…}
options: [{id: "324af955-1aa9-42ea-be6e-bb4e5623a97a", parent_id: null, name: "Need Insurance?",…},…]
0: {id: "324af955-1aa9-42ea-be6e-bb4e5623a97a", parent_id: null, name: "Need Insurance?",…}
1: {id: "73298c18-4ccc-4138-afa5-71d3d00dff9b", parent_id: null, name: "Color", slug: "color",…}
rating: []
tags: [{id: "8a31ee4c-3302-4357-9686-bd4308bbf39f", name: "options", slug: "options", photo: null,…},…]
variations: [,…]
0: {id: "3bf5aeb9-9da2-4fb1-a3d2-f89eb75839c3", parent_id: null, name: "Ram", slug: "ram", photo: null,…}
1: {id: "e5c70766-a558-4539-b41f-77f72c819a7c", parent_id: null, name: "cpu", slug: "cpu", photo: null,…}
2: {id: "e63ac831-f595-4889-83d2-a5be65734758", parent_id: null, name: "Monitor", slug: "monitor",…}
1: {,…}
brand: {id: "fe877b45-8620-453a-8805-63f0cbd80752", name: "No Brand", slug: "no-brand", description: null,…}
options: [{id: "522da418-eb3f-43e9-9392-63c941842a52", parent_id: null, name: "Color", slug: "color-3",…}]
0: {id: "522da418-eb3f-43e9-9392-63c941842a52", parent_id: null, name: "Color", slug: "color-3",…}
rating: []
tags: [,…]
variations: [,…]
0: {id: "8e9a26c5-2ee4-4d86-9244-a10596d67fea", parent_id: null, name: "cpu", slug: "cpu-3", photo: null,…}
Explanation
As you can see each of my products has same fields of data and some of them are the same, for instance in options both products have Color or in variations both have cpu or brand of both is the same, in final results I need to have only 1 Color and 1 cpu and 1 brand as they are the same.
code
This is how code above returns
$data = [];
foreach($products as $i => $product) {
$data[$i]['brand'] = $product->brand;
$data[$i]['rating'] = $product->rating;
$data[$i]['variations'] = $product->variations;
$data[$i]['options'] = $product->options;
$data[$i]['tags'] = $product->tags;
}
Note: the final result is a merge of all products data into single array, but unique values. That's what I'm looking for.
Any idea?
Update
sample of final result would be something like this
finalResult: [{,…}, {,…}]
0: {,…}
brand: {id: "fe877b45-8620-453a-8805-63f0cbd80752", name: "No Brand", slug: "no-brand", description: null,…}
0: {id: "fe877b45-8620-453a-8805-63f0cbd80752", name: "No Brand", slug: "no-brand", description: null,…},…] // it was same in both products
options: [{id: "324af955-1aa9-42ea-be6e-bb4e5623a97a", parent_id: null, name: "Need Insurance?",…},…]
0: {id: "324af955-1aa9-42ea-be6e-bb4e5623a97a", parent_id: null, name: "Need Insurance?",…}
1: {id: "73298c18-4ccc-4138-afa5-71d3d00dff9b", parent_id: null, name: "Color", slug: "color",…} // it was same in both products
rating: []
tags: [{id: "8a31ee4c-3302-4357-9686-bd4308bbf39f", name: "options", slug: "options", photo: null,…},…]
0: {id: "8a31ee4c-3302-4357-9686-bd4308bbf39f", name: "options", slug: "options", photo: null,…}
1: {id: "94ef99b6-ed2a-4eea-9248-e4775159eb58", name: "product", slug: "product", photo: null,…}
2: {id: "378802b3-d13a-48c4-afa2-f9fed94d69ee", name: "werg", slug: "werg", photo: null, active: "yes",…} // added from another product
3: {id: "f1380f50-af59-4f6a-8eca-d40689c1c1c1", name: "werwg", slug: "werwg", photo: null, active: "yes",…} // added from another product
variations: [,…]
0: {id: "3bf5aeb9-9da2-4fb1-a3d2-f89eb75839c3", parent_id: null, name: "Ram", slug: "ram", photo: null,…}
1: {id: "e5c70766-a558-4539-b41f-77f72c819a7c", parent_id: null, name: "cpu", slug: "cpu", photo: null,…} // it was same in both products
2: {id: "e63ac831-f595-4889-83d2-a5be65734758", parent_id: null, name: "Monitor", slug: "monitor",…}
Update 2
Getting children of data
Options and Variations both have children (i.e Cpu => [Core i7, Core i3]) or color => ['red', 'black'] so I need to collect this children's and put them under their parents data.
Option model
public function options()
{
return $this->hasMany(Option::class);
}
public function children() {
return $this->hasMany(Option::class,'parent_id','id') ;
}
public function parent()
{
return $this->belongsTo(Option::class,'parent_id');
}
public function isParent()
{
return !$this->parent_id ? true : false; // if parent_id is null => is a Parent Option
}
Variant model
public function variants()
{
return $this->hasMany(Variant::class);
}
public function children() {
return $this->hasMany(Variant::class,'parent_id','id') ;
}
public function parent()
{
return $this->belongsTo(Variant::class,'parent_id');
}
public function isParent()
{
return !$this->parent_id ? true : false; // if parent_id is null => is a Parent Variant
}
Note: as you probably understand relationship between parent and children are defined by parent_id column.
Sample data (includes all arrays and their childs)
{
"brands": [
{
"id": "fe877b45-8620-453a-8805-63f0cbd80752",
"name": "no brand",
"slug": "no-brand",
"description": null,
"photo": null,
"created_at": "2020-07-15 11:35:18",
"updated_at": "2020-07-15 11:35:18"
}
],
"options": [
{
"id": "324af955-1aa9-42ea-be6e-bb4e5623a97a",
"parent_id": null,
"name": "need insurance?",
"slug": "need-insurance",
"photo": null,
"type": "radio",
"active": "yes",
"created_at": "2020-07-17 11:28:09",
"updated_at": "2020-07-17 11:28:09",
"pivot": {
"product_id": "293c0369-04a7-4330-bb98-ede0bcf10f8d",
"option_id": "324af955-1aa9-42ea-be6e-bb4e5623a97a"
},
"children": [
{
"id": "44afca9e-abf1-4a7a-9c46-d96d8127c2af",
"parent_id": "324af955-1aa9-42ea-be6e-bb4e5623a97a",
"name": "No",
"slug": "no",
"photo": null,
"type": "radio",
"active": "yes",
"created_at": "2020-07-17 11:28:09",
"updated_at": "2020-07-17 11:28:09"
}
]
},
{
"id": "73298c18-4ccc-4138-afa5-71d3d00dff9b",
"parent_id": null,
"name": "color",
"slug": "color",
"photo": null,
"type": "dropdown",
"active": "yes",
"created_at": "2020-07-17 11:27:41",
"updated_at": "2020-07-17 11:27:41",
"pivot": {
"product_id": "293c0369-04a7-4330-bb98-ede0bcf10f8d",
"option_id": "73298c18-4ccc-4138-afa5-71d3d00dff9b"
},
"children": [
{
"id": "29b62f35-52a2-4a8b-ac8f-7e70e065488a",
"parent_id": "73298c18-4ccc-4138-afa5-71d3d00dff9b",
"name": "Black",
"slug": "black",
"photo": null,
"type": "dropdown",
"active": "yes",
"created_at": "2020-07-17 11:27:41",
"updated_at": "2020-07-17 11:27:41"
},
{
"id": "4aa2d899-f1cc-4000-95e6-997d28dc51fc",
"parent_id": "73298c18-4ccc-4138-afa5-71d3d00dff9b",
"name": "Red",
"slug": "red",
"photo": null,
"type": "dropdown",
"active": "yes",
"created_at": "2020-07-17 11:27:41",
"updated_at": "2020-07-17 11:27:41"
},
{
"id": "5f9de5bc-e966-48f3-b78c-de709dba86b5",
"parent_id": "73298c18-4ccc-4138-afa5-71d3d00dff9b",
"name": "Gray",
"slug": "gray",
"photo": null,
"type": "dropdown",
"active": "yes",
"created_at": "2020-07-17 11:27:41",
"updated_at": "2020-07-17 11:27:41"
},
{
"id": "f248d6ab-1b88-4ea4-8d6a-62fe271bfa8a",
"parent_id": "73298c18-4ccc-4138-afa5-71d3d00dff9b",
"name": "White",
"slug": "white",
"photo": null,
"type": "dropdown",
"active": "yes",
"created_at": "2020-07-17 11:27:41",
"updated_at": "2020-07-17 11:27:41"
}
]
},
{
"id": "522da418-eb3f-43e9-9392-63c941842a52",
"parent_id": null,
"name": "color",
"slug": "color-3",
"photo": null,
"type": "radio",
"active": "yes",
"created_at": "2020-07-17 12:20:46",
"updated_at": "2020-07-17 12:20:46",
"pivot": {
"product_id": "a8bb27c8-e968-4317-b4d2-8e5cd6049ff8",
"option_id": "522da418-eb3f-43e9-9392-63c941842a52"
},
"children": [
{
"id": "84135f25-690b-407b-8c98-e7526429a594",
"parent_id": "522da418-eb3f-43e9-9392-63c941842a52",
"name": "Red",
"slug": "red-3",
"photo": null,
"type": "radio",
"active": "yes",
"created_at": "2020-07-17 12:20:46",
"updated_at": "2020-07-17 12:20:46"
},
{
"id": "9d1f0d9c-272a-4e96-ac0a-aeac869bfc30",
"parent_id": "522da418-eb3f-43e9-9392-63c941842a52",
"name": "Yellow",
"slug": "yellow-2",
"photo": null,
"type": "radio",
"active": "yes",
"created_at": "2020-07-17 12:20:46",
"updated_at": "2020-07-17 12:20:46"
}
]
}
],
"ratings": [
"4.5",
"4.0"
],
"tags": [
{
"id": "8a31ee4c-3302-4357-9686-bd4308bbf39f",
"name": "options",
"slug": "options",
"photo": null,
"active": "yes",
"created_at": "2020-07-17 11:29:47",
"updated_at": "2020-07-17 11:29:47",
"pivot": {
"product_id": "293c0369-04a7-4330-bb98-ede0bcf10f8d",
"tag_id": "8a31ee4c-3302-4357-9686-bd4308bbf39f"
}
},
{
"id": "94ef99b6-ed2a-4eea-9248-e4775159eb58",
"name": "product",
"slug": "product",
"photo": null,
"active": "yes",
"created_at": "2020-07-17 11:29:47",
"updated_at": "2020-07-17 11:29:47",
"pivot": {
"product_id": "293c0369-04a7-4330-bb98-ede0bcf10f8d",
"tag_id": "94ef99b6-ed2a-4eea-9248-e4775159eb58"
}
},
{
"id": "378802b3-d13a-48c4-afa2-f9fed94d69ee",
"name": "werg",
"slug": "werg",
"photo": null,
"active": "yes",
"created_at": "2020-07-15 11:53:13",
"updated_at": "2020-07-15 11:53:13",
"pivot": {
"product_id": "a8bb27c8-e968-4317-b4d2-8e5cd6049ff8",
"tag_id": "378802b3-d13a-48c4-afa2-f9fed94d69ee"
}
},
{
"id": "f1380f50-af59-4f6a-8eca-d40689c1c1c1",
"name": "werwg",
"slug": "werwg",
"photo": null,
"active": "yes",
"created_at": "2020-07-15 11:53:13",
"updated_at": "2020-07-15 11:53:13",
"pivot": {
"product_id": "a8bb27c8-e968-4317-b4d2-8e5cd6049ff8",
"tag_id": "f1380f50-af59-4f6a-8eca-d40689c1c1c1"
}
}
],
"variations": [
{
"id": "3bf5aeb9-9da2-4fb1-a3d2-f89eb75839c3",
"parent_id": null,
"name": "ram",
"slug": "ram",
"photo": null,
"type": "input",
"active": "yes",
"created_at": "2020-07-17 11:27:05",
"updated_at": "2020-07-17 11:27:05",
"pivot": {
"product_id": "293c0369-04a7-4330-bb98-ede0bcf10f8d",
"variant_id": "3bf5aeb9-9da2-4fb1-a3d2-f89eb75839c3"
},
"children": [
{
"id": "5687d6a8-12df-41b2-bf2f-b822faae8af0",
"parent_id": "3bf5aeb9-9da2-4fb1-a3d2-f89eb75839c3",
"name": "4 Gig",
"slug": "4 Gig",
"photo": null,
"type": "input",
"active": "yes",
"created_at": "2020-07-17 11:27:05",
"updated_at": "2020-07-17 11:27:05"
}
]
},
{
"id": "e5c70766-a558-4539-b41f-77f72c819a7c",
"parent_id": null,
"name": "cpu",
"slug": "cpu",
"photo": null,
"type": "input",
"active": "yes",
"created_at": "2020-07-17 11:26:58",
"updated_at": "2020-07-17 11:26:58",
"pivot": {
"product_id": "293c0369-04a7-4330-bb98-ede0bcf10f8d",
"variant_id": "e5c70766-a558-4539-b41f-77f72c819a7c"
},
"children": [
{
"id": "83003a24-cc69-4305-8d3a-e99da91d3354",
"parent_id": "e5c70766-a558-4539-b41f-77f72c819a7c",
"name": "Core i7",
"slug": "Core i7",
"photo": null,
"type": "input",
"active": "yes",
"created_at": "2020-07-17 11:26:58",
"updated_at": "2020-07-17 11:26:58"
}
]
},
{
"id": "e63ac831-f595-4889-83d2-a5be65734758",
"parent_id": null,
"name": "monitor",
"slug": "monitor",
"photo": null,
"type": "input",
"active": "yes",
"created_at": "2020-07-17 11:27:21",
"updated_at": "2020-07-17 11:27:21",
"pivot": {
"product_id": "293c0369-04a7-4330-bb98-ede0bcf10f8d",
"variant_id": "e63ac831-f595-4889-83d2-a5be65734758"
},
"children": [
{
"id": "816e1fab-24eb-49e9-9b3a-d4b4cce16cdf",
"parent_id": "e63ac831-f595-4889-83d2-a5be65734758",
"name": "14\"",
"slug": "14\"",
"photo": null,
"type": "input",
"active": "yes",
"created_at": "2020-07-17 11:27:21",
"updated_at": "2020-07-17 11:27:21"
}
]
},
{
"id": "8e9a26c5-2ee4-4d86-9244-a10596d67fea",
"parent_id": null,
"name": "cpu",
"slug": "cpu-3",
"photo": null,
"type": "input",
"active": "yes",
"created_at": "2020-07-17 12:20:56",
"updated_at": "2020-07-17 12:20:56",
"pivot": {
"product_id": "a8bb27c8-e968-4317-b4d2-8e5cd6049ff8",
"variant_id": "8e9a26c5-2ee4-4d86-9244-a10596d67fea"
},
"children": [
{
"id": "50857808-106e-4ae0-8c02-a54761e6dac7",
"parent_id": "8e9a26c5-2ee4-4d86-9244-a10596d67fea",
"name": "Core i3",
"slug": "Core i3-2",
"photo": null,
"type": "input",
"active": "yes",
"created_at": "2020-07-17 12:20:56",
"updated_at": "2020-07-17 12:20:56"
}
]
}
]
}
You can achieve what you want in one iteration over the data using reduce like so:
$variations = [];
$result = array_reduce($filters, function ($result, $filter) use ($variations) {
$filter['brand']['name'] = strtolower($filter['brand']['name']);
if ($result['brands']->where('name', $filter['brand']['name'])->isEmpty()) {
$result['brands']->push($filter['brand']);
}
foreach ($filter['options'] as $option) {
$option['name'] = strtolower($option['name']);
if ($result['options']->where('name', $option['name'])->isEmpty()) {
$result['options']->push($option);
}
}
if (isset($filter['rating']['id'])) {
if ($result['ratings']->where('id', $filter['rating']['id'])->isEmpty()) {
$result['ratings']->push($filter['rating']);
}
}
foreach ($filter['tags'] as $tag) {
$tag['name'] = strtolower($tag['name']);
if ($result['tags']->where('name', $tag['name'])->isEmpty()) {
$result['tags']->push($tag);
}
}
foreach ($filter['variations'] as $variation) {
$variation['name'] = strtolower($variation['name']);
$variationName = $variation['name'];
$children = collect($variation['children'])->pluck('name');
if ($result['variations']->where('name', $variation['name'])->isEmpty()) {
$result['variations']->push($variation);
$variations[$variationName] = $children;
} else {
$different = $variations[$variationName]->diff($children);
if ($different->isNotEmpty()) {
$result['variations']->push($variation);
foreach ($different as $childName) {
$variations[$variationName]->push($childName);
}
}
}
}
return $result;
}, collect([
'brands' => collect(),
'options' => collect(),
'ratings' => collect(),
'tags' => collect(),
'variations' => collect()
]));
If you need the result as an array, you can use the collection's toArray method:
$result->toArray();
I have added a sample code with a row json data based on your input
$jsonData = '[{
"brand": {"id": "fe877b45-8620-453a-8805-63f0cbd80752", "name": "No Brand", "slug": "no-brand", "description": "null"},
"options": [{"id": "324af955-1aa9-42ea-be6e-bb4e5623a97a", "parent_id": "null", "name": "Need Insurance?"},
{"id": "73298c18-4ccc-4138-afa5-71d3d00dff9b", "parent_id": "null", "name": "Color", "slug": "color"}],
"rating": [],
"tags": [{"id": "8a31ee4c-3302-4357-9686-bd4308bbf39f", "name": "options", "slug": "options", "photo": "null"}],
"variations": [{"id": "3bf5aeb9-9da2-4fb1-a3d2-f89eb75839c3", "parent_id": "null", "name": "Ram", "slug": "ram", "photo": "null"},
{"id": "e5c70766-a558-4539-b41f-77f72c819a7c", "parent_id": "null", "name": "cpu", "slug": "cpu", "photo": "null"},
{"id": "e63ac831-f595-4889-83d2-a5be65734758", "parent_id": "null", "name": "Monitor", "slug": "monitor"}
]
},
{
"brand": {"id": "fe877b45-8620-453a-8805-63f0cbd80752", "name": "No Brand", "slug": "no-brand", "description": null},
"options": [{"id": "522da418-eb3f-43e9-9392-63c941842a52", "parent_id": null, "name": "Color", "slug": "color-3"}],
"rating": [],
"tags": [],
"variations": [{"id": "8e9a26c5-2ee4-4d86-9244-a10596d67fea", "parent_id": null, "name": "Cpu", "slug": "cpu-3", "photo": null}]
}
]
';
$jsonDataArr =json_decode($jsonData);
$data = array();
foreach($jsonDataArr as $key => $items){
foreach($items as $innerKey => $eachItem){
if(!isset($data[$innerKey])){
$data[$innerKey] = array();
if(is_array($eachItem)){
foreach($eachItem as $each)
if(!empty($each))
$data[$innerKey][] = (array) $each;
}else{
if(!empty($eachItem))
$data[$innerKey][] = (array) $eachItem;
}
}else{
if(is_array($eachItem)){
foreach($eachItem as $each)
if(!empty($each))
$data[$innerKey][] = (array) $each;
}else{
if(!empty($eachItem))
$data[$innerKey][] = (array) $eachItem;
}
}
}
}
foreach($data as $key => $val){
foreach($val as $l => $item){
$index = trim(strtolower($item['name']));
$data[$key][$index] = $item;
unset($data[$key][$l]);
}
}
foreach($data as $key => $val){
$data[$key]=array_values($val);
}
print_r($data);
Demo

filter collection to remove items that have an empty array

I have a collection that looks like this,
[
{
"id": 1,
"title": "Cooking",
"deleted_at": null,
"created_at": null,
"updated_at": null,
"listings": [
{
"id": 6,
"title": "Listing Number 1",
"slug": "listing-number-1",
"description": "Description",
"booking_details": "Booking Details",
"cost": "25.00",
"active": 0,
"user_id": 2,
"deleted_at": null,
"created_at": "2020-06-03T22:35:56.000000Z",
"updated_at": "2020-06-03T22:35:56.000000Z",
"pivot": {
"category_id": 1,
"listing_id": 6
},
"assets": [],
"primary_image": []
},
{
"id": 7,
"title": "Listing Number 1",
"slug": "listing-number-1",
"description": "Description",
"booking_details": "Booking Details",
"cost": "25.00",
"active": 0,
"user_id": 2,
"deleted_at": null,
"created_at": "2020-06-03T22:36:10.000000Z",
"updated_at": "2020-06-03T22:36:10.000000Z",
"pivot": {
"category_id": 1,
"listing_id": 7
},
"assets": [],
"primary_image": []
},
{
"id": 8,
"title": "Listing Number 1",
"slug": "listing-number-1",
"description": "Description",
"booking_details": "Booking Details",
"cost": "25.00",
"active": 0,
"user_id": 2,
"deleted_at": null,
"created_at": "2020-06-03T22:36:26.000000Z",
"updated_at": "2020-06-03T22:36:26.000000Z",
"pivot": {
"category_id": 1,
"listing_id": 8
},
"assets": [],
"primary_image": []
},
{
"id": 9,
"title": "Listing Number 1",
"slug": "listing-number-1",
"description": "Description",
"booking_details": "Booking Details",
"cost": "25.00",
"active": 0,
"user_id": 2,
"deleted_at": null,
"created_at": "2020-06-03T22:36:42.000000Z",
"updated_at": "2020-06-03T22:36:42.000000Z",
"pivot": {
"category_id": 1,
"listing_id": 9
},
"assets": [],
"primary_image": []
},
{
"id": 10,
"title": "Listing Number 1",
"slug": "listing-number-1",
"description": "Description",
"booking_details": "Booking Details",
"cost": "25.00",
"active": 0,
"user_id": 2,
"deleted_at": null,
"created_at": "2020-06-03T22:37:40.000000Z",
"updated_at": "2020-06-03T22:37:40.000000Z",
"pivot": {
"category_id": 1,
"listing_id": 10
},
"assets": [],
"primary_image": []
},
{
"id": 11,
"title": "Listing Number 2",
"slug": "listing-number-2",
"description": "Description 1",
"booking_details": "Booking Details 1",
"cost": "25.00",
"active": 0,
"user_id": 2,
"deleted_at": null,
"created_at": "2020-06-03T22:46:31.000000Z",
"updated_at": "2020-06-03T22:46:31.000000Z",
"pivot": {
"category_id": 1,
"listing_id": 11
},
"assets": [
{
"id": 3,
"type": "image",
"path": "https://xx-xxxx-xxxx-1.s3.eu-west-2.amazonaws.com/listing-number-2/primary-image-lg.jpg",
"is_primary": 1,
"assetable_type": "App\\Listing",
"assetable_id": 11,
"user_id": 2,
"deleted_at": null,
"created_at": "2020-06-03T22:46:33.000000Z",
"updated_at": "2020-06-03T22:46:33.000000Z"
},
{
"id": 4,
"type": "image",
"path": "https://xx-xxxx-xxxx-1.s3.eu-west-2.amazonaws.com/listing-number-2/primary-image-sm.jpg",
"is_primary": 1,
"assetable_type": "App\\Listing",
"assetable_id": 11,
"user_id": 2,
"deleted_at": null,
"created_at": "2020-06-03T22:46:33.000000Z",
"updated_at": "2020-06-03T22:46:33.000000Z"
}
],
"primary_image": [
{
"id": 3,
"type": "image",
"path": "https://xx-xxxx-xxxx-1.s3.eu-west-2.amazonaws.com/listing-number-2/primary-image-lg.jpg",
"is_primary": 1,
"assetable_type": "App\\Listing",
"assetable_id": 11,
"user_id": 2,
"deleted_at": null,
"created_at": "2020-06-03T22:46:33.000000Z",
"updated_at": "2020-06-03T22:46:33.000000Z"
},
{
"id": 4,
"type": "image",
"path": "https://xx-xxxx-xxxx-1.s3.eu-west-2.amazonaws.com/listing-number-2/primary-image-sm.jpg",
"is_primary": 1,
"assetable_type": "App\\Listing",
"assetable_id": 11,
"user_id": 2,
"deleted_at": null,
"created_at": "2020-06-03T22:46:33.000000Z",
"updated_at": "2020-06-03T22:46:33.000000Z"
}
]
},
{
"id": 12,
"title": "Listing Number 3",
"slug": "listing-number-3",
"description": "Description 1",
"booking_details": "Booking Details 1",
"cost": "25.00",
"active": 0,
"user_id": 2,
"deleted_at": null,
"created_at": "2020-06-03T22:50:31.000000Z",
"updated_at": "2020-06-03T22:50:31.000000Z",
"pivot": {
"category_id": 1,
"listing_id": 12
},
"assets": [],
"primary_image": []
},
{
"id": 13,
"title": "Listing Number 3",
"slug": "listing-number-3",
"description": "Description 1",
"booking_details": "Booking Details 1",
"cost": "25.00",
"active": 0,
"user_id": 2,
"deleted_at": null,
"created_at": "2020-06-03T22:57:12.000000Z",
"updated_at": "2020-06-03T22:57:12.000000Z",
"pivot": {
"category_id": 1,
"listing_id": 13
},
"assets": [],
"primary_image": []
},
{
"id": 15,
"title": "Listing Number 3",
"slug": "listing-number-3",
"description": "Description 1",
"booking_details": "Booking Details 1",
"cost": "25.00",
"active": 0,
"user_id": 2,
"deleted_at": null,
"created_at": "2020-06-03T23:00:49.000000Z",
"updated_at": "2020-06-03T23:00:49.000000Z",
"pivot": {
"category_id": 1,
"listing_id": 15
},
"assets": [],
"primary_image": []
},
{
"id": 16,
"title": "Listing Number 3",
"slug": "listing-number-3",
"description": "Description 1",
"booking_details": "Booking Details 1",
"cost": "25.00",
"active": 0,
"user_id": 2,
"deleted_at": null,
"created_at": "2020-06-03T23:01:21.000000Z",
"updated_at": "2020-06-03T23:01:21.000000Z",
"pivot": {
"category_id": 1,
"listing_id": 16
},
"assets": [],
"primary_image": []
},
{
"id": 17,
"title": "Listing Number 3",
"slug": "listing-number-3",
"description": "Description 1",
"booking_details": "Booking Details 1",
"cost": "25.00",
"active": 0,
"user_id": 2,
"deleted_at": null,
"created_at": "2020-06-03T23:01:26.000000Z",
"updated_at": "2020-06-03T23:01:26.000000Z",
"pivot": {
"category_id": 1,
"listing_id": 17
},
"assets": [],
"primary_image": []
},
{
"id": 18,
"title": "Listing Number 3",
"slug": "listing-number-3",
"description": "Description 1",
"booking_details": "Booking Details 1",
"cost": "25.00",
"active": 0,
"user_id": 2,
"deleted_at": null,
"created_at": "2020-06-03T23:01:49.000000Z",
"updated_at": "2020-06-03T23:01:49.000000Z",
"pivot": {
"category_id": 1,
"listing_id": 18
},
"assets": [],
"primary_image": []
},
{
"id": 19,
"title": "Listing Number 3",
"slug": "listing-number-3",
"description": "Description 1",
"booking_details": "Booking Details 1",
"cost": "25.00",
"active": 0,
"user_id": 2,
"deleted_at": null,
"created_at": "2020-06-03T23:02:19.000000Z",
"updated_at": "2020-06-03T23:02:19.000000Z",
"pivot": {
"category_id": 1,
"listing_id": 19
},
"assets": [],
"primary_image": []
},
{
"id": 20,
"title": "Listing Number 3",
"slug": "listing-number-3",
"description": "Description 1",
"booking_details": "Booking Details 1",
"cost": "25.00",
"active": 0,
"user_id": 2,
"deleted_at": null,
"created_at": "2020-06-03T23:03:31.000000Z",
"updated_at": "2020-06-03T23:03:31.000000Z",
"pivot": {
"category_id": 1,
"listing_id": 20
},
"assets": [],
"primary_image": []
},
{
"id": 21,
"title": "Listing Number 3",
"slug": "listing-number-3",
"description": "Description 1",
"booking_details": "Booking Details 1",
"cost": "25.00",
"active": 0,
"user_id": 2,
"deleted_at": null,
"created_at": "2020-06-03T23:06:25.000000Z",
"updated_at": "2020-06-03T23:06:25.000000Z",
"pivot": {
"category_id": 1,
"listing_id": 21
},
"assets": [
{
"id": 5,
"type": "image",
"path": "https://xx-xxxx-xxxx-1.s3.eu-west-2.amazonaws.com/listing-number-3/primary-image-lg.jpg",
"is_primary": 1,
"assetable_type": "App\\Listing",
"assetable_id": 21,
"user_id": 2,
"deleted_at": null,
"created_at": "2020-06-03T23:06:26.000000Z",
"updated_at": "2020-06-03T23:06:26.000000Z"
},
{
"id": 6,
"type": "image",
"path": "https://xx-xxxx-xxxx-1.s3.eu-west-2.amazonaws.com/listing-number-3/primary-image-sm.jpg",
"is_primary": 1,
"assetable_type": "App\\Listing",
"assetable_id": 21,
"user_id": 2,
"deleted_at": null,
"created_at": "2020-06-03T23:06:27.000000Z",
"updated_at": "2020-06-03T23:06:27.000000Z"
}
],
"primary_image": [
{
"id": 5,
"type": "image",
"path": "https://xx-xxxx-xxxx-1.s3.eu-west-2.amazonaws.com/listing-number-3/primary-image-lg.jpg",
"is_primary": 1,
"assetable_type": "App\\Listing",
"assetable_id": 21,
"user_id": 2,
"deleted_at": null,
"created_at": "2020-06-03T23:06:26.000000Z",
"updated_at": "2020-06-03T23:06:26.000000Z"
},
{
"id": 6,
"type": "image",
"path": "https://xx-xxxx-xxxx-1.s3.eu-west-2.amazonaws.com/listing-number-3/primary-image-sm.jpg",
"is_primary": 1,
"assetable_type": "App\\Listing",
"assetable_id": 21,
"user_id": 2,
"deleted_at": null,
"created_at": "2020-06-03T23:06:27.000000Z",
"updated_at": "2020-06-03T23:06:27.000000Z"
}
]
}
]
}
]
I only want to see a collection that contains items that don't have empty assets array,
I have tried this,
$category = Category::whereHas('listings.assets')->with(['listings.assets', 'listings.primaryImage' => function($query){
$query->where('assets.is_primary', '=', 1);
}])->get();
return $category->filter(function($item){
foreach($item->listings as $i) {
return !empty($i->assets);
}
});
But this just returns an empty [] what am I doing wrong?
You are returning in your loop, so as soon as it finds listings that does not have assets it will break the loop and be false. Instead use contains(), that check if any of the elements, passes the given condition. If one does, it will return true. Therefor not breaking the loop on empty collections, as you do now.
return $category->filter(function($item){
return $item->listings->contains(function ($listing) {
return $listing->assets->isNotEmpty();
});
});

How to sort the collection on custom condition in laravel 5.7?

I have an api ready that returns the following json response.
Response:
{
"success": true,
"conversation": [{
"id": 37,
"type": "1",
"name": "Sonali",
"created_at": "2019-02-18 13:26:10",
"updated_at": "2019-02-18 20:32:54",
"unread_count": 2,
"chat": {
"id": 357,
"conversation_id": "23",
"type": "text",
"sender_id": "37",
"receiver_id": "39",
"data": "{\"text\":\"hello....\"}",
"delivered_at": "2019-02-20 13:25:27",
"seen_at": null,
"created_at": "2019-02-20 13:25:10",
"updated_at": "2019-02-20 13:25:27",
"sender_name": "Sonali"
}
},
{
"id": 38,
"type": "1",
"name": "Raviraj",
"created_at": "2019-02-18 20:23:55",
"updated_at": "2019-02-18 20:32:47",
"unread_count": 0,
"chat": {
"id": 354,
"conversation_id": "22",
"type": "text",
"sender_id": "39",
"receiver_id": "38",
"data": "{\"text\":\"hey....\"}",
"delivered_at": null,
"seen_at": null,
"created_at": "2019-02-20 13:24:35",
"updated_at": "2019-02-20 13:24:35",
"sender_name": "Nitesh Kesarkar"
}
},
{
"id": 27,
"type": "1",
"name": "Rakesh",
"created_at": "2019-02-01 10:48:19",
"updated_at": "2019-02-07 11:35:10",
"unread_count": 1,
"chat": {
"id": 358,
"conversation_id": "21",
"type": "text",
"sender_id": "27",
"receiver_id": "39",
"data": "{\"text\":\"hello\"}",
"delivered_at": "2019-02-20 13:25:27",
"seen_at": null,
"created_at": "2019-02-20 13:25:24",
"updated_at": "2019-02-20 13:25:27",
"sender_name": "Rakesh Patil"
}
}
]
}
This response consists of list of users and their associated last chat message for currently logged in user. I want to sort this collection according to the latest message first. How can I do that?
Sorting should be based on the chat.created_at field.
Expected Result:
{
"success": true,
"conversation": [
{
"id": 27,
"type": "1",
"name": "Rakesh",
"created_at": "2019-02-01 10:48:19",
"updated_at": "2019-02-07 11:35:10",
"unread_count": 1,
"chat": {
"id": 358,
"conversation_id": "21",
"type": "text",
"sender_id": "27",
"receiver_id": "39",
"data": "{\"text\":\"hello\"}",
"delivered_at": "2019-02-20 13:25:27",
"seen_at": null,
"created_at": "2019-02-20 13:25:24",
"updated_at": "2019-02-20 13:25:27",
"sender_name": "Rakesh Patil"
}
},
{
"id": 37,
"type": "1",
"name": "Sonali",
"created_at": "2019-02-18 13:26:10",
"updated_at": "2019-02-18 20:32:54",
"unread_count": 2,
"chat": {
"id": 357,
"conversation_id": "23",
"type": "text",
"sender_id": "37",
"receiver_id": "39",
"data": "{\"text\":\"hello....\"}",
"delivered_at": "2019-02-20 13:25:27",
"seen_at": null,
"created_at": "2019-02-20 13:25:10",
"updated_at": "2019-02-20 13:25:27",
"sender_name": "Sonali"
}
},
{
"id": 38,
"type": "1",
"name": "Raviraj",
"created_at": "2019-02-18 20:23:55",
"updated_at": "2019-02-18 20:32:47",
"unread_count": 0,
"chat": {
"id": 354,
"conversation_id": "22",
"type": "text",
"sender_id": "39",
"receiver_id": "38",
"data": "{\"text\":\"hey....\"}",
"delivered_at": null,
"seen_at": null,
"created_at": "2019-02-20 13:24:35",
"updated_at": "2019-02-20 13:24:35",
"sender_name": "Nitesh Kesarkar"
}
}
]
}
UPDATE :
Adding these lines worked as expected. Thanks #JCode
$sorted = $chats->sortByDesc('chat.created_at');
$chats = $sorted->values()->all();
You are looking for the sortBy() method.
This will of course work if your JSON output can be turned into a collection via collect()—in a perfect scenario, your chat messages would be a model in Laravel.

Count Json elements wiht PHP

So I have
{
"members": [
{
"username": "John",
"status": "offline",
"avatar_url": "...",
"id": "830232882252102064"
},
{
"username": "Momo",
"status": "online",
"avatar_url": "...",
"id": "259137993351102464"
}
]
}
How do I count (in php) how many users are offline and how many users are online and return them into a value like $memonline and $memoffline.
Here in below code we are getting values of all statuses and then we are counting values of all statuses.
Try this code snippet here
<?php
ini_set('display_errors', 1);
$string='{
"channels": [
{
"position": 13,
"id": "304700935878213642",
"name": "20KBPS"
},
{
"position": 12,
"id": "304700895978061835",
"name": "50KBPS"
},
{
"position": 11,
"id": "304701193261809672",
"name": "70KBPS"
},
{
"position": 10,
"id": "304701326288224256",
"name": "90KBPS"
},
{
"position": 1,
"id": "304699877621891072",
"name": "=================="
},
{
"position": 9,
"id": "304700570592346114",
"name": "=================="
},
{
"position": 4,
"id": "304701407221514240",
"name": "=================="
},
{
"position": 14,
"id": "304700808883339264",
"name": "=================="
},
{
"position": 2,
"id": "304700525939523584",
"name": "Channel 1"
},
{
"position": 3,
"id": "304700547426942976",
"name": "Channel 2"
},
{
"position": 0,
"id": "304692483973971990",
"name": "General Channel"
},
{
"position": 6,
"id": "304701637446991873",
"name": "Private/2"
},
{
"position": 5,
"id": "304701480605319178",
"name": "Private/2"
},
{
"position": 7,
"id": "304701680010788866",
"name": "Private/3"
},
{
"position": 8,
"id": "304701738999611394",
"name": "Private/3"
},
{
"position": 15,
"id": "304700776691793921",
"name": "Trash Bin"
}
],
"instant_invite": null,
"id": "304692483973971989",
"members": [
{
"username": "Dyno",
"status": "online",
"bot": true,
"game": {
"name": "dynobot.net | ?help"
},
"avatar_url": "https://cdn.discordapp.com/avatars/155149108183695360/5aeb68c29b56b3d92eddb6f46df5051c.jpg",
"avatar": "5aeb68c29b56b3d92eddb6f46df5051c",
"discriminator": "3861",
"id": "155149108183695360"
},
{
"username": "Momo",
"status": "online",
"bot": true,
"game": {
"name": "$help | $info"
},
"avatar_url": "https://cdn.discordapp.com/avatars/259137993351102464/a3005ab7aff3eb829fecf375931a76f1.jpg",
"avatar": "a3005ab7aff3eb829fecf375931a76f1",
"discriminator": "4649",
"id": "259137993351102464"
},
{
"username": "Ronny Dark",
"status": "online",
"avatar_url": "https://cdn.discordapp.com/avatars/152855546574143492/6d9b98972ca6f0308be4dd2aec5aaca3.jpg",
"avatar": "6d9b98972ca6f0308be4dd2aec5aaca3",
"discriminator": "1480",
"id": "152855546574143492"
}
],
"name": "Server name"
}';
$array=json_decode($string,true);
$result=array_column($array["members"],"status");
$members=array_count_values($result);
echo isset($members["offline"]) ? $members["offline"] : 0;

Categories