Anil Astrio
Anil Astrio

Reputation: 103

Add $_POST to array

I have 1 array:

$meta_array = array(
   'lot',
   'floor',
   'block'
);

How can I convert the above array to show

'lot' => $_POST['lot'], 'floor' => $_POST['floor']  

so I can update_post_meta($new, 'number' , $meta_array) like this. I am trying to save some KBs as my form php size is getting rather large.

Thank you for your advice.

FINALLY - I DID THIS

        $new_meta = array();
        foreach($meta_array as $val){
            if (isset($_POST[$val])) {
                $new_meta[$val] = sanitize_array_text_field($_POST[$val]);
            }
        }
        update_post_meta($new, 'property', $new_meta);

Upvotes: 0

Views: 103

Answers (3)

Brandon J Brotsky
Brandon J Brotsky

Reputation: 523

You could also do:

if(isset($_POST['lot']))
    $meta_array['lot'] = $_POST['lot'];
if(isset($_POST['floor']))
    $meta_array['floor'] = $_POST['floor'];
if(isset($_POST['block']))
    $meta_array['block'] = $_POST['block'];

Using isset() is a little safer.

Upvotes: 0

Yoric
Yoric

Reputation: 1784

use :

$posted = array_combine ($meta_array, $_POST);

You will get :

Array(
   [lot]   => $_POST['lot'],
   [floor] => $_POST['floor'],
   [block] => $_POST['block']
)

Upvotes: 1

Sharanya Dutta
Sharanya Dutta

Reputation: 4021

Use a foreach loop:

$meta_array = array('lot', 'floor', 'block');
$another_array = array();
foreach($meta_array as $val){
    $another_array[$val] = $_POST[$val];
}

Upvotes: 0

Related Questions