本文介绍了在PHP中读取JSON时is_array()无法正常工作的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

示例JSON内容:

{"payment_records":{"Currency":"USD","Paid_Ammounts":{"1st Payment":"50","2nd Payment":"75","3rd Payment":"100"}}}

过去几天我一直在使用这些代码来读取PHP中的JSON文件

I had been using these codes for the past few days to read JSON file in PHP

$phpArray = json_decode($jsoncontents, false);
$retrievepayments = $phpArray->payment_records;

最近我添加了一个代码以获取每次付款

And recently I added a code to get each payments

foreach ($retrievepayments as $key1 => $value1)
     if (is_array($value1))

is_array的代码似乎无法正常工作.当它为true时,它会一直为false,$value1确实具有数组内容.如果我错了,我需要使用什么来检查?我需要检查的天气$value1仅包含1个数组,例如'{"Currency":"USD"}'(也许根本不将1个数组称为数组),或者$value1包含'{"Currency":"USD","Paid_Ammounts":{"1st Payment":"50","2nd Payment":"75","3rd Payment":"100"}}'.

The code for is_array doesn't seem to work right. It kept going to false when it should be true, $value1 do have array contents. If I'm wrong, what do I need to use to check? What I need is to check weather $value1 only contain 1 array like '{"Currency":"USD"}' (perhaps 1 array is not called an array at all) or if $value1 contains '{"Currency":"USD","Paid_Ammounts":{"1st Payment":"50","2nd Payment":"75","3rd Payment":"100"}}'.

我使用phptester.net并放置了以下脚本

I used phptester.net and palced the following scripts

$decodecontents = '{"Payment_History":{"Currency":"USD","Paid_Ammounts":{"1st Payment":"50","2nd Payment":"75","3rd Payment":"100"}}}';

$phpArray = json_decode($decodecontents, false);
$retrievepayments = $phpArray->Payment_History;

foreach ($retrievepayments as $key1 => $value1)
{
     #if (is_array($value1))
    if (is_object($value1))
     {
         echo "Hit!";
     }
    else
    {
        echo "NOT Hit!";
    }
}

得到

NOT Hit!Hit!

嗯,我想这是在告诉我"Paid_Ammounts":{"1st Payment":"50","2nd Payment":"75","3rd Payment":"100"}是物体吗?

Hmm, I think it's telling me that "Paid_Ammounts":{"1st Payment":"50","2nd Payment":"75","3rd Payment":"100"} is an object?

推荐答案

评论太久了.

您的解决方案在那里,以下内容将循环遍历Paid_Ammounts

Your solution is there, the following will loop through all entries in the Paid_Ammounts

$jsoncontents = '{"Currency":"USD","Paid_Ammounts":{"1st Payment":"50","2nd Payment":"75","3rd Payment":"100"}}';

$jsonObject = json_decode($jsoncontents, false);

foreach ($jsonObject->Paid_Ammounts as $key => $val)
{
        echo "{$key} --> {$val}\n";
}

上面的输出将是

1st Payment --> 50
2nd Payment --> 75
3rd Payment --> 100

更新对我来说还不清楚您实际上要实现什么,但是您可以执行类似的操作来遍历对象

UPDATEIt's a bit unclear to me what you are actually trying to achieve, but you could do something like this to traverse the object

$jsoncontents = '{"Currency":"USD","Paid_Ammounts":{"1st Payment":"50","2nd Payment":"75","3rd Payment":"100"}}';

$jsonObject = json_decode($jsoncontents, false);

function printKeyVal($obj) 
{
    foreach ($obj as $key => $val)
    {
        if (is_object($val))
        {
            echo "{$key}\n";
            printKeyVal($val);
        }
        else
        {
            echo "{$key} --> {$val}\n";
        }
    }
}

printKeyVal($jsonObject);

将输出

Currency --> USD
Paid_Ammounts
1st Payment --> 50
2nd Payment --> 75
3rd Payment --> 100

如果对象嵌套得更深,应该保持挖掘"状态

And should keep "digging" if the object is nested deeper

这篇关于在PHP中读取JSON时is_array()无法正常工作的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!

10-29 01:02