i have a really long form in wordpress that it filled out by the user and then submitted to a thirdparty crm via API.
The thing is if user doesn’t fill a field, these field will be false or empty and remote_post will fail.
I tried to add a custom value to empty fields with a foreach but they are in associative array and i dont know how to modify it.
This is my code so far (i changed values but is the same structure):
$body = [ 'array1' => [ 'key1' => user(value), 'key2' => user(value), 'key3' => user(value), ], 'array2' => [ 'key1' => user(value), 'key2' => user(value), 'key3' => user(value), ], 'array3' => [ [ 'key1' => user(value), 'key2' => user(value), 'key3' => user(value), ], [ 'key1' => user(value), 'key2' => user(value), 'key3' => user(value), ], ], 'array4' => [ 'subarrray1' => [ 'key1' => user(value), 'key2' => user(value), 'key3' => user(value), ], 'subarray2' => [ 'key1' => user(value), 'key2' => user(value), 'key3' => user(value), ], ],
];
//this work only with some arrays and only change value inside the second array foreach($body as $row => $innerArray){ foreach($innerArray as $key => $value){ if ( empty($value) ) $value = 'empty o whatever'; } }
Advertisement
Answer
You can use recursion
$body = [ 'array1' => [ 'key1' => 123, 'key2' => '', 'key3' => '', ], 'array2' => [ 'key1' => '', 'key2' => '', 'key3' => 123, ], 'array3' => [ [ 'key1' => '', 'key2' => '', 'key3' => '', ], [ 'key1' => '', 'key2' => '', 'key3' => 123, ], ], 'array4' => [ 'subarrray1' => [ 'key1' => '', 'key2' => 123, 'key3' => '', ], 'subarray2' => [ 'key1' => '', 'key2' => '', 'key3' => '', ], ] ]; function replace_empty(&$arr) { if(is_iterable($arr)) { foreach ($arr as &$row) { replace_empty($row); } } elseif (empty($arr)) { $arr = 'empty o whatever'; } } replace_empty($body); var_dump($body);