0

I'm building a webhook that will receive data from an external service.

In order to retrieve data, I'm using the following array:

$data = [
$_POST['aaa'],
$_POST['bbb'],
$_POST['ccc'],
$_POST['ddd'],
$_POST['eee']
];

Is it possible to build the same array without repeating $_POST? I mean something like:

$data = $_POST [
['aaa'],
['bbb'],
['ccc'],
['ddd'],
['eee']
];

Obviously, that code is wrong.

Thanks.

0

3 Answers 3

1

There's no shortcut like that. You could use array_map():

$data = array_map(function($key) {
    return $_POST[$key];
}, ['aaa', 'bbb', 'ccc', ...]);
Sign up to request clarification or add additional context in comments.

Comments

0
$data = $_POST

This will build your new $data array the same way as your GLOBAL $_POST array including the POST Key names and their assigned values.

Also checkout extract($_POST), as this will extract each key to its same name variable, which may be easier to then reference in your web-hook or any functions or procedural code you uses from then on. This essentially does this:

$aaa = $POST['aaa']
$bbb = $POST['bbb']
$ccc = $POST['ccc']

etc etc https://www.php.net/manual/en/function.extract.php

1 Comment

"$data = $_POST;" is exactly what I need (but I won't use "extract()" because of security reasons). Thanks! And thanks to Barmar and Christoff X, too: I've learned something new.
0

The simplest way for me would be:

  1. For getting all the values in the $_POST array

foreach($_POST as $value){ $data[] = $value; }

  1. For getting all the values and want to keep the key

foreach($_POST as $key=>$value){ $data[$key] = $value; }

  1. For getting a specific array of values from the array

foreach(['aaa','bbb','ccc','ddd','eee'] as $column){ $data[] = $_POST[$column]; }

  1. Just a good idea to always escape variables ;)

$con = mysqli_connect('hostcleveranswer.com','USERyouearn','PWDanupv0Te','dropthemicDB') foreach($_POST as $key => $value) { $data[] = mysqli_escape_string($con,$value); }

//i feel that was a good simple answer using tools without having to a learn a new function syntax

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.