0

I have below array, which includes a set of default indexes, that must be present in my final array:

'entities' => [
        'deliveredAt',
        'issuedAt',
        'totals' => [
            'due',
            'gross',
            'net',
            'tax' => [
                'amount',
                'net',
                'rate',
            ],
        ]
],

The above array is saved in a variable called $entities.

Now, I have a 3rd party API, that will return the above entities, but only include them in the response if the entity contains a value.

For example, a $response can look like this:

array:2 [▼
  "issuedAt" => "2020-08-20"
  "totals" => array:1 [▼
    "tax" => []
  ]
]

As you can see, if comparing the returned array with the indexes that I expect, there is a few missing:

  • deliveredAt
  • totals.due, totals.gross, totals.net, totals.tax.amount, totals.tax.net, totals.tax.rate

I am trying to make a method that can iterate over the $response array, and check if it contains the indexes that I expect. If not, I simply want to set the index with a value of null.

Below is what I have so far:

foreach ($entities as $key => $entity) {
         if (!is_array($entity)) {
             if (!isset($response[$entity])) {
                    $response[$entity] = null;
             }
         }
}

However, this will only add an index that is not an array. In this example, it will only add: deliveredAt => null.

How can I do, so the above method can iterate through multiple at least 2 nested arrays and add the index name and null value?

2
  • And if no tax provided - should it be tax => null, or tax => [amount => null etc ]? Commented Aug 22, 2020 at 9:54
  • @u_mulder it should be tax => ['amount' => null, 'net' => null, 'rate' => null] Commented Aug 22, 2020 at 9:55

2 Answers 2

2

You can define initial array with keys and NULL (or whatever you need) as values:

$entities = [
    'deliveredAt' => null,
    'issuedAt' => null,
    'totals' => [
        'due' => null,
        'gross' => null,
        'net' => null,
        'tax' => [
            'amount' => null,
            'net' => null,
            'rate' => null,
        ],
    ]
];

// here's your real data
$realData = [
  "issuedAt" => "2020-08-20",
  "totals" => [
    "tax" => [
      'net' => 42,    
    ]
  ]
];
// now use array_replace_recursive to replace keys in `$entities` with values of `$realData`
print_r(array_replace_recursive($entities, $realData));

Fiddle.

Also note that keys from $realData that do not exist in $entities will be added to result.

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

Comments

1

You can use array_replace_recursive for doing this. You had only to change your associative array entities a little bit, so every property needs a initiliasation (e.g. NULL or '').

$result = array_replace_recursive($entities, $array);

Here you can test it http://sandbox.onlinephpfunctions.com/code/4688ed3240050479edeef7c9e4da16f98dbe01de

Here is the hole code:

$array = [
  "issuedAt" => "2020-08-20",
  "totals" => [
    "tax" => [
        'amount' => 100
    ]
  ]
];

$entities = [
    'deliveredAt' => NULL,
    'issuedAt' => NULL,
    'totals' => [
        'due' => NULL,
        'gross' => NULL,
        'net' => NULL,
        'tax' => [
            'amount' => NULL,
            'net' => NULL,
            'rate' => NULL
        ],
    ]
];

$result = array_replace_recursive($entities, $array);
var_dump($result);

2 Comments

And what's the difference with my answer?
You are right. I was in the meantime 1 hour away and didn't saw your answer as I completed my answer and posted it. Give you 1 point.

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.