0

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';
    } 
}
1
  • Your title was very descriptive and a great start to your question :). We could probably improve it a bit by making it a real question for better SEO, thus helping reach other developers with the same problem. The "before post" part was bit misleading imho, though. Commented Dec 10, 2020 at 4:34

2 Answers 2

1

You can modify the original array like this

foreach($body as $row => $innerArray){
    foreach($innerArray as $key => $value){
        if ( empty($value) ) {
            $body[$row][$key] = 'empty o whatever';
        }
    } 
}

You can also get the values in a foreach loop by reference by prepending an & sign. Changing the value will then update the original array like the example below:

foreach($body as $row => &$innerArray){
    foreach($innerArray as $key => &$value){
        $value = 'empty o whatever';
    } 
}
Sign up to request clarification or add additional context in comments.

Comments

0

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

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.