简体   繁体   English

如何通过键从数组中获取单个值

[英]how to get single value from array by key

how to get single value from php array by key.如何通过键从php数组中获取单个值。 below is the data stored in $table.下面是存储在 $table 中的数据。 Now i want to access every single value from $table by key.现在我想通过键访问 $table 中的每个值。 eg "technology" is the key and i want to access the value "GSM / HSPA / LTE" by key of Tecnology.例如“技术”是关键,我想通过技术的关键访问值“GSM/HSPA/LTE”。

i use the below code but it's not by key, it's by array index.我使用下面的代码,但不是按键,而是按数组索引。

Need solution with example...需要示例的解决方案...

echo array_values($table)[0]; echo "<br />";
echo array_values($table)[1]; echo "<br />";
echo array_values($table)[2]; echo "<br />";
echo array_values($table)[3]; echo "<br />";
{
    "Technology": "GSM \/ HSPA \/ LTE",
    "2G bands": "GSM 850 \/ 900 \/ 1800 \/ 1900 - SIM 1 & SIM 2",
    "3G bands": "HSDPA 850 \/ 900 \/ 1900 \/ 2100 ",
    "4G bands": "LTE band 1(2100), 3(1800), 5(850), 8(900), 20(800), 40(2300), 41(2500)",
    "Speed": "HSPA, LTE-A",
    "Announced": "2019, February",
    "Status": "Available. Released 2019, March",
    "Dimensions": "159 x 75.1 x 8.5 mm (6.26 x 2.96 x 0.33 in)",
    "Weight": "174 g (6.14 oz)",
    "Build": "Front glass, plastic body",
    "SIM": "Dual SIM (Nano-SIM, dual stand-by)",
    "Type": "Super AMOLED capacitive touchscreen, 16M colors",
    "Size": "6.4 inches, 100.5 cm2 (~84.2% screen-to-body ratio)",
    "Resolution": "1080 x 2340 pixels, 19.5:9 ratio (~403 ppi density)",
    "OS": "Android 8.1 (Oreo), upgradable to Android 9.0 (Pie); One UI",
    "Chipset": "Exynos 7904 (14 nm)",
    "CPU": "Octa-core (2x1.8 GHz Cortex-A73 & 6x1.6 GHz Cortex-A53)",
    "GPU": "Mali-G71 MP2",
    "Card slot": "microSD, up to 1 TB (dedicated slot)",
    "Internal": "64GB 4GB RAM, 128GB 6GB RAM",
    "Triple": "13 MP, f\/1.9, PDAF\r\n  5 MP, f\/2.2, 12mm (ultrawide)\r\n  5 MP, f\/2.2, depth sensor",
    "Features": "HDR",
    "Video": "1080p@30fps",
    "Single": "16 MP, f\/2.0",
    "Loudspeaker ": "Yes",
    "3.5mm jack ": "Yes",
    " ": "Non-removable Li-Po 5000 mAh battery",
    "WLAN": "Wi-Fi 802.11 a\/b\/g\/n\/ac, dual-band, Wi-Fi Direct, hotspot",
    "Bluetooth": "5.0, A2DP, LE",
    "GPS": "Yes, with A-GPS, GLONASS, BDS",
    "Radio": "FM radio, RDS, recording",
    "USB": "2.0, Type-C 1.0 reversible connector",
    "Sensors": "Fingerprint (rear-mounted), accelerometer, gyro, proximity, compass",
    "Charging": "Fast battery charging 15W",
    "Colors": "Black, Blue",
    "Models": "SM-M305F, SM-M305FN\/DS, SM-M305G\/DS, SM-M305M",
    "SAR": "0.41 W\/kg (head)     ",
    "Price": "About 260 EUR",
    "Performance": "  Basemark OS II: 1956 \/ Basemark OS II 2.0: 1432\r\nBasemark X: 12223",
    "Display": "  Contrast ratio: Infinite (nominal)",
    "Camera": "  Photo \/ Video",
    "Loudspeaker": "  Voice 65dB \/ Noise 66dB \/ Ring 70dB    ",
    "Audio quality": "  Noise -90.5dB \/ Crosstalk -90.0dB",
    "Battery life": "    Endurance rating 119h    ",
    "": null
}

PHP can't read json objects, you first need to convert it to a PHP array. PHP无法读取 json 对象,首先需要将其转换为PHP数组。
You can use json_decode() to do that.您可以使用json_decode()来做到这一点。
Then you can access the values using the key "Technology" .然后您可以使用键"Technology"访问这些值。

$table = '{}'; // your json object in string format
$table = json_decode($table, true); // decode the json string to PHP array
echo $table['Technology']; // show the Technology value

You can then also loop through your object using this:然后,您还可以使用以下方法循环遍历您的对象:

foreach($table as $value){
   echo $value."<br/>";
}
// this will show all results on a new line

Here is an example.这是一个例子。

$array =json_decode('{"Technology": "GSM \/ HSPA \/ LTE"}');
$json_array =($array);
$values = $json_array->Technology;
//echo $values;
$text =  explode("/", $values);
var_dump( $text);

Output: array(3) { [0]=> string(4) "GSM " [1]=> string(6) " HSPA " [2]=> string(4) " LTE" }输出:array(3) { [0]=> string(4) "GSM " [1]=> string(6) " HSPA " [2]=> string(4) " LTE" }

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM