1

I am creating an associative array from a json code so that i can use in my curl code that connects to an API. Now it seems like the associative array outputted is incorrect. I would like to format it correctly, but now I get message an error saying the array is incorrect.

The json code:

{
"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"
        }
    }
]


}

The php code to build the array:

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

print_r($payment);

The output:

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

My main goal is to remove the [0] indexes without messing up the array. please assist

5
  • 1
    You can't, since 'payment' is an array of objects. the 0 is necessary in php - array entries MUST have keys. Commented Jul 15, 2015 at 16:54
  • @JonathanKuhn only got rid of the first [0] zero index, thanks. any idea how to get rid of the rest of them ? Commented Jul 15, 2015 at 16:57
  • @kya eval.in/399423 But if you exactly know that all arrays have only one Item Commented Jul 15, 2015 at 17:04
  • @splash58 Yes, each array has exactly one item Commented Jul 15, 2015 at 17:06
  • @kya then look the link Commented Jul 15, 2015 at 17:08

3 Answers 3

1

instead of $payment = ($json['payment']);

change that to $payment = reset($json['payment']);

However if there are multiple entries under payment, then you should just loop over them like:

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

The loop also would work if there was any number of elements under payment, so not just multiple.

Sign up to request clarification or add additional context in comments.

2 Comments

it worked for the first [0] zero index, but not for nested zero indexes
@kya reset will only get the first index. If you have more you would want to loop over them or you would need to set them each to a different variable. There is no way to get rid of the numbers otherwise. Even in javascript/json, you don't see the numbers, but javascript will add them when parsing the json into an object in script.
0

more or less safe function

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

Comments

0

In addition to Jonathan Khun.

For your nested arrays you just do the same. Reset the internal pointer of that array.

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

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.