Warning: file_get_contents(/data/phpspider/zhask/data//catemap/3/arrays/14.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 从json代码创建嵌套关联数组_Php_Arrays_Json - Fatal编程技术网

Php 从json代码创建嵌套关联数组

Php 从json代码创建嵌套关联数组,php,arrays,json,Php,Arrays,Json,我正在从json代码创建一个关联数组,以便在连接到API的curl代码中使用。现在,输出的关联数组似乎不正确。我想正确格式化它,但现在我收到一条错误消息,说数组不正确 json代码: { "payment": [ { "clientCorrelator": "54321", "endUserId": "tel:+16309700001", "merchantCode": "01234", "merchantPin": "12

我正在从json代码创建一个关联数组,以便在连接到API的curl代码中使用。现在,输出的关联数组似乎不正确。我想正确格式化它,但现在我收到一条错误消息,说数组不正确

json代码:

{
"payment": [
    {
        "clientCorrelator": "54321",
        "endUserId": "tel:+16309700001",
        "merchantCode": "01234",
        "merchantPin": "1234",
        "merchantNumber": "tel:+16309700001",
        "notifyURL": "http://example.com/notifyURL",


        "paymentAmount": {
            "chargingInformation": [
                {
                    "amount": "10",
                    "currency": "USD",
                    "description": "AlienInvadersGame"
                }
            ],
            "chargingMetaData": [
                {
                    "onBehalfOf": "Example Games Inc",
                    "purchaseCategoryCode": "Game",
                    "channel": "WAP",
                    "taxAmount": "0"
                }
            ],
            "referenceCode": "REF-12345",
            "transactionOperationStatus": "Charged"
        }
    }
]


}
构建阵列的php代码:

jasondata = file_get_contents("payment.json");
$json = json_decode($jasondata, true);
$payment = ($json['payment']) ;

print_r($payment);
输出:

Array ( [0] => Array ( [clientCorrelator] => 54321 [endUserId] => tel:+16309700001 [merchantCode] => 01234 [merchantPin] => 1234 [merchantNumber] => tel:+16309700001 [notifyURL] => http://example.com/notifyURL [paymentAmount] => Array ( [chargingInformation] => Array ( [0] => Array ( [amount] => 10 [currency] => USD [description] => AlienInvadersGame ) ) [chargingMetaData] => Array ( [0] => Array ( [onBehalfOf] => Example Games Inc [purchaseCategoryCode] => Game [channel] => WAP [taxAmount] => 0 ) ) [referenceCode] => REF-12345 [transactionOperationStatus] => Charged ) ) ) 

我的主要目标是删除[0]索引,而不会弄乱数组。请协助而不是
$payment=($json['payment'])

将其更改为
$payment=reset($json['payment'])

但是,如果在
付款
下有多个条目,则您应该像这样循环它们:

foreach($json['payment'] as $payment){
    print_r($payment);
}

如果付款项下有任意数量的元素,那么循环也会工作,所以不仅仅是多个元素。

除了Jonathan Khun

对于嵌套数组,您也可以这样做。重置该数组的内部指针

<?php

$jasondata = '{
"payment": [
    {
        "clientCorrelator": "54321",
        "endUserId": "tel:+16309700001",
        "merchantCode": "01234",
        "merchantPin": "1234",
        "merchantNumber": "tel:+16309700001",
        "notifyURL": "http://example.com/notifyURL",


        "paymentAmount": {
            "chargingInformation": [
                {
                    "amount": "10",
                    "currency": "USD",
                    "description": "AlienInvadersGame"
                }
            ],
            "chargingMetaData": [
                {
                    "onBehalfOf": "Example Games Inc",
                    "purchaseCategoryCode": "Game",
                    "channel": "WAP",
                    "taxAmount": "0"
                }
            ],
            "referenceCode": "REF-12345",
            "transactionOperationStatus": "Charged"
        }
    }
]


}';

$json = json_decode($jasondata, true);
$payment = reset($json['payment']);
$payment['paymentAmount']['chargingInformation'] = reset($payment['paymentAmount']['chargingInformation']);
$payment['paymentAmount']['chargingMetaData'] = reset($payment['paymentAmount']['chargingMetaData']);

echo "<pre>";
print_r($payment);
?>

或多或少的安全功能


您不能,因为
'payment'
是一个对象数组。
0
在php中是必需的-数组项必须有键。@JonathanKuhn只去掉了第一个[0]零索引,谢谢。知道如何去掉其余的吗?@kya但如果你确实知道所有数组只有一个Item@splash58是的,每个数组正好有一个item@kya然后查看链接它对第一个[0]零索引有效,但对嵌套零无效indexes@kya
reset
将只获取第一个索引。如果你有更多的数据,你会想要在它们上面循环,或者你需要将它们分别设置为一个不同的变量。否则就没有办法摆脱这些数字。即使在javascript/json中,您也看不到数字,但javascript在将json解析为脚本中的对象时会添加数字。
$payment = json_decode($str, true);

function RemoveZeroIndex(&$arr) {
  foreach($arr as $key => &$item) {       // walk array
     if (is_array($item) &&               // if array
        (count($item) === 1) &&           // with one item
        isset($item[0]))                  // and numeric index
            $item = $item[0];             // shift it
     if (is_array($item)) 
        RemoveZeroIndex($item);           // call recursively
   }
}

RemoveZeroIndex($payment);
print_r($payment);