3

i have a json of specification of a mobile, i want to count repeated value of a particular node. my json in as below:

[{
    "other": {
        "1": {
            "type": "Network",
            "label": "Technology",
            "value": "GSM / HSPA / LTE"
        },
        "2": {
            "type": "Network",
            "label": "2G bands",
            "value": "GSM 850 / 900 / 1800 / 1900 - SIM 1 & SIM 2"
        },
        "3": {
            "type": "Network",
            "label": "3G bands",
            "value": "HSDPA 850 / 900 / 1900 / 2100"
        },
        "4": {
            "type": "Network",
            "label": "4G bands",
            "value": "LTE band 1(2100), 3(1800), 5(850), 7(2600), 8(900), 20(800), 38(2600), 40(2300), 41(2500)"
        },
        "5": {
            "type": "Network",
            "label": "Speed",
            "value": "HSPA 42.2/5.76 Mbps, LTE-A (2CA) Cat12 600/50 Mbps"
        },
        "6": {
            "type": "Network",
            "label": "GPRS",
            "value": "Yes"
        },
        "7": {
            "type": "Network",
            "label": "EDGE",
            "value": "Yes"
        },
        "8": {
            "type": "Launch",
            "label": "Announced",
            "value": "2019, January"
        },
        "9": {
            "type": "Launch",
            "label": "Status",
            "value": "Available. Released 2019, February"
        },
        "10": {
            "type": "Body",
            "label": "Dimensions",
            "value": "156.4 x 74.5 x 8.8 mm (6.16 x 2.93 x 0.35 in)"
        },
        "11": {
            "type": "Body",
            "label": "Weight",
            "value": "186 g (6.56 oz)"
        },
        "12": {
            "type": "Body",
            "label": "SIM",
            "value": "Dual SIM (Nano-SIM, dual stand-by)"
        },
        "13": {
            "type": "Display",
            "label": "Type",
            "value": "PLS TFT capacitive touchscreen, 16M colors"
        },
        "14": {
            "type": "Display",
            "label": "Size",
            "value": "6.3 inches, 97.4 cm2 (~83.6% screen-to-body ratio)"
        },
        "15": {
            "type": "Display",
            "label": "Resolution",
            "value": "1080 x 2340 pixels, 19.5:9 ratio (~409 ppi density)"
        },
        "16": {
            "type": "Platform",
            "label": "OS",
            "value": "Android 8.1 (Oreo); Experience 9.5"
        },
        "17": {
            "type": "Platform",
            "label": "Chipset",
            "value": "Exynos 7904 (14 nm)"
        },
        "18": {
            "type": "Platform",
            "label": "CPU",
            "value": "Octa-core (2x1.8 GHz Cortex-A73 & 6x1.6 GHz Cortex-A53)"
        },
        "19": {
            "type": "Platform",
            "label": "GPU",
            "value": "Mali-G71 MP2"
        },
        "20": {
            "type": "Memory",
            "label": "Card slot",
            "value": "microSD, up to 1 TB (dedicated slot)"
        },
        "21": {
            "type": "Memory",
            "label": "Internal",
            "value": "64 GB, 4 GB RAM or 32 GB, 3 GB RAM"
        },
        "22": {
            "type": "Main Camera",
            "label": "Dual",
            "value": "13 MP, f/1.9, 1/3.1\", 1.12um, PDAF"
        },
        "23": {
            "type": "Main Camera",
            "label": "Dual",
            "value": "5 MP, f/2.2, 12mm (ultrawide)"
        },
        "24": {
            "type": "Main Camera",
            "label": "Features",
            "value": "LED flash, panorama, HDR"
        },
        "25": {
            "type": "Main Camera",
            "label": "Video",
            "value": "1080p@30fps"
        },
        "26": {
            "type": "Selfie camera",
            "label": "Single",
            "value": "8 MP, f/2.0, 25mm (wide)"
        },
        "27": {
            "type": "Selfie camera",
            "label": "Features",
            "value": "HDR"
        },
        "28": {
            "type": "Selfie camera",
            "label": "Video",
            "value": "1080p@30fps"
        },
        "29": {
            "type": "Sound",
            "label": "Loudspeaker",
            "value": "Yes"
        },
        "30": {
            "type": "Sound",
            "label": "3.5mm jack",
            "value": "Yes"
        },
        "31": {
            "type": "Sound",
            "value": "Active noise cancellation with dedicated mic"
        },
        "32": {
            "type": "Sound",
            "value": "Dolby Atmos sound"
        },
        "33": {
            "type": "Comms",
            "label": "WLAN",
            "value": "Wi-Fi 802.11 b/g/n, WiFi Direct, hotspot"
        },
        "34": {
            "type": "Comms",
            "label": "Bluetooth",
            "value": "5.0, A2DP, LE"
        },
        "35": {
            "type": "Comms",
            "label": "GPS",
            "value": "Yes, with A-GPS, GLONASS, BDS"
        },
        "36": {
            "type": "Comms",
            "label": "Radio",
            "value": "FM radio, RDS, recording"
        },
        "37": {
            "type": "Comms",
            "label": "USB",
            "value": "2.0, Type-C 1.0 reversible connector"
        },
        "38": {
            "type": "Features",
            "label": "Sensors",
            "value": "Fingerprint (rear-mounted), accelerometer, gyro, proximity, compass"
        },
        "39": {
            "type": "Battery",
            "value": "Non-removable Li-Po 5000 mAh battery"
        },
        "40": {
            "type": "Battery",
            "label": "Charging",
            "value": "Fast battery charging 15W"
        },
        "41": {
            "type": "Misc",
            "label": "Colors",
            "value": "Ocean Blue, Charcoal Black"
        },
        "42": {
            "type": "Misc",
            "label": "Models",
            "value": "SM-M205F, SM-M205FN, SM-M205G"
        },
        "43": {
            "type": "Misc",
            "label": "SAR EU",
            "value": "0.25 W/kg (head) 1.59 W/kg (body)"
        },
        "44": {
            "type": "Misc",
            "label": "Price",
            "value": "About 220 EUR"
        },
        "45": {
            "type": "Tests",
            "label": "Performance",
            "value": "Basemark OS II: 1846 / Basemark OS II 2.0: 2025Basemark X: 8434"
        },
        "46": {
            "type": "Tests",
            "label": "Display",
            "value": "Contrast ratio: 1333:1 (nominal), 2.960 (sunlight)"
        },
        "47": {
            "type": "Tests",
            "label": "Camera",
            "value": "Photo / Video"
        },
        "48": {
            "type": "Tests",
            "label": "Loudspeaker",
            "value": "Voice 67dB / Noise 66dB / Ring 68dB"
        },
        "49": {
            "type": "Tests",
            "label": "Audio quality",
            "value": "Noise -90.5dB / Crosstalk -91.7dB"
        },
        "50": {
            "type": "Tests",
            "label": "Battery life",
            "value": "Endurance rating 103h"
        }
    }
}]

I want to count how many times Network, Body, Display ..., Tests are there in this JSON.

I have tried below code but unable to found exact information regarding the same

$arr1 = above shared json;
$arr1 = json_decode($arr, true);
foreach ($arr1 as $key=>$value){
    $other = $value['other']; //array 
    $counter = 0;
    $lenOther = count($other); //get length of other array
    //reading each element of other array
    foreach($other as $k=>$v){
            $other[$k] = array_count_values($v);
            print_r($other[$k]);
    }

}

Any modification or suggestion of this question would be appreciated.

1
  • You can take a look to my previous answer Commented Jun 24, 2019 at 7:23

3 Answers 3

4

Apply array_count_values() along with array_column

$arr1 = json_decode($arr, true);

print_r(array_count_values(array_column($arr1[0]['other'],'type')));

Output:- https://3v4l.org/ZHRIa

Note:- If you want to combine all of them based on type and want to create same structure what you have:-

$arr1 = json_decode($arr, true);

$finalArray = array();

foreach($arr1[0]['other'] as $key=>$value){
   $finalArray[$value['type']][] = $value;
}
$newArray = [];
$newArray[] = array('other'=>$finalArray);
print_r($newArray);

Output:- https://3v4l.org/lUrgv

Sign up to request clarification or add additional context in comments.

Comments

1

You get the count by using array_count_values method with array_map

just like below

array_count_values(array_map(function($foo) {
    return $foo['type'];
}, $arr['other']));

this will get you the count for each type

Comments

1

you can use foreach with array_key_exists

$jarr = json_decode($json,true);
$r  = [];
foreach($jarr[0]['other'] as $v){
 array_key_exists($v['type'], $r) ? ($r[$v['type']]+=1) : ($r[$v['type']]=1);
}

Working example

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.