Merge array values and array_walk_recursive

19 views Asked by At

The title is terrible, but unfortunately I can't think of anything better to describe my situation.

Suppose the following code, and I'll then explain what I want.

$invalid_id = array('100', '110', '120');
$invalid_status = array('200', '210');
$already_exported = array('400', '450');

$tmp = array(
    'There is no order with that ID' => $invalid_id,
    'The order has an invalis status' => $invalid_status,
    'The order was previously exported' => $already_exported,
);

echo '<div><p>' . sprintf('No orders were exported (%s).', implode(', ', array_merge($invalid_id, $invalid_status, $already_exported))) . '</p></div>';

So the code above produces an output like this: <div><p>No orders were exported (100, 110, 120, 200, 210, 400, 450).</p></div>

The problem is I want to add a reason for why each order id was rejected. So I used the $tmp array were the keys are the reasons why each array's values (order ids) were rejected. So that key should be used in a <span title="the reason - appropriate key of the $tmp array - goes here">each id</span> scenario to show the reason once the user hovers over each rejected id.

I can easily build a function to achieve what I want, but I'm sure there is an elegant, super short way to do it as well. It involves array_walk_recursive() but I can't get my mind around it yet, so I'm asking for your help guys!

1

There are 1 answers

1
Xupitan On

you can use array_diff after using array_merge for checking elements were rejected