Warning: file_get_contents(/data/phpspider/zhask/data//catemap/1/php/259.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
如何从php数组中计算相同的值_Php_Arrays - Fatal编程技术网

如何从php数组中计算相同的值

如何从php数组中计算相同的值,php,arrays,Php,Arrays,我有一个移动设备的json规范,我想计算一个特定节点的重复值。我的json格式如下: [{ "other": { "1": { "type": "Network", "label": "Technology", "value": "GSM / HSPA / LTE" }, "2": { "type": "Network",

我有一个移动设备的json规范,我想计算一个特定节点的重复值。我的json格式如下:

[{
    "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"
        }
    }
}]
我想计算一下这个JSON中有多少次
网络
正文
显示
…,
测试

我已经尝试了下面的代码,但无法找到关于相同的确切信息

$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]);
    }

}

对此问题的任何修改或建议都将不胜感激。

您可以使用
数组\u计数\u值
方法和
数组\u映射

就像下面

array_count_values(array_map(function($foo) {
    return $foo['type'];
}, $arr['other']));
这将为您获得每种类型的计数以及应用

输出:-

注意:-如果您希望根据
类型组合所有这些类型,并希望创建与现有结构相同的结构:-

$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);

输出:-

您可以将
foreach
数组\u键\u一起使用

$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);
}

您可以查看我以前的
$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);
}