I am trying to run a function for every element in an array using array_walk and try and catch so i can tell if any elements haven't run through the function successfully. 
Once all elements have run through the function i need to respond with a complete callback.
In my code below though the complete json_ecnode is being run everytime rather than at the very end. What am i doing wrong? Also is this the most efficient way to do this?
$products = array('shirt','skirt','jumper','lingerie','makeup','top','trousers','coats');
$i = 0;
function createProducts(&$item, $key){
    try {
        // try something
    }
    catch(Exception $e) {
        // error
    }
    $i++;
    if($i > count($products)) { json_encode('complete'); }
}
array_walk($products, 'createProducts');
 
     
     
    