简体   繁体   English

使用PHP解析/遍历JSON

[英]Parse/traverse JSON with PHP

After parsing this JSON in PHP, how can I access a particular value without using a foreach ? 在PHP中解析此JSON之后,如何在不使用foreach情况下访问特定值?

[
    {
        "currency": "CAD",
        "exchange": "1"
    },
    {
        "currency": "EUR",
        "exchange": "0.7158"
    },
    {
        "currency": "GBP",
        "exchange": "0.5131"
    },
    {
        "currency": "MXN",
        "exchange": "12.4601"
    },
    {
        "currency": "USD",
        "exchange": "0.8122"
    }
]

Is it possible to make like this ? 可以这样吗?

$string = file_get_contents("datas.json");
$json = json_decode($string, true);
$json['currency']['USD']['exchange'];

Thanks a lot for your help. 非常感谢你的帮助。

You have an array of objects defined there but because you used the TRUE option on the json_decode they will get converted to an array of arrays so you would need to address them as 您在那里定义了一个对象数组,但是因为您在json_decode上使用了TRUE选项,所以它们将被转换为数组数组,因此您需要将它们寻址为

$string = file_get_contents("datas.json");
$json = json_decode($string, true);
echo $json[0]['currency'];  // CAD
echo $json[0]['exchange'];  // 1

If you want to use the currency name as a key you will have to change the data structure of the json data file. 如果要使用货币名称作为键,则必须更改json数据文件的数据结构。

You can use array_search() if you don't want to see foreach within your code. 如果不想在代码中看到foreach ,可以使用array_search()

$key = array_search("USD", array_column($json, "currency"));
echo $json[$key]['exchange'];

But one way or another, to find an exact value, you need to iterate over the array to have an exact match. 但是,要找到确切的值,您需要反复遍历数组以具有完全匹配的值。

The first index in the data you get from json_decode() is numeric, starting from zero. json_decode()获得的数据中的第一个索引是数字,从零开始。 If you intend to directly access the part where GBP is mentioned, you have to iterate over it, you cannot do it directly. 如果要直接访问提到GBP的部分,则必须对其进行迭代,而不能直接进行。

You have to do it at least once in order to create a data structure that is better suited to your needs and avoid iterating more than once. 您必须至少执行一次,才能创建更适合您的需求的数据结构,并避免重复多次。

I was thinking about whether or not it would possible to stuff the data inside an intelligent object that would try to avoid iterating, but I didn't come to an obvious solution. 我当时在考虑是否有可能将数据填充到一个智能对象中,该对象将试图避免迭代,但是我没有找到一个明显的解决方案。 You'd have to explain why iterating at least once would not do what you want, or why it has drawbacks you cannot afford. 您必须解释为什么至少重复一次不做您想要的事情,或者为什么它有您负担不起的缺点。 That example data set of five currencies doesn't look too bad, and I think there are at most 200 currencies in the world. 以五种货币表示的示例数据集看起来还不错,而且我认为世界上最多有200种货币。 Performance and memory shouldn't be of a big issue here. 性能和内存在这里不应该成为大问题。

how about this? 这个怎么样?

$json = json_decode($string, true);

$currency = array();

foreach($json as $arr) {
    foreach($arr as $key => $value) {
        $currency[$key] = $value;
    }
}

echo $currency['USD']; // echoes "0.8122"

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

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