I'm using the PHP Flight micro framework (http://flightphp.com/). On a POST request, the variables can be retrieved by using Flight::request()->data (http://flightphp.com/learn#requests). As is, it appears to be typed: flight\util\Collection Object
. As I am passing this data to another class, I would like to convert it to a standard associative array. I can simply foreach through the data, but is there a better way? What is the best method to achieve this? Am I asking the wrong question?
Asked
Active
Viewed 4,894 times
1

Jason
- 4,079
- 4
- 22
- 32
4 Answers
3
You can get the internal array by using Collection::getData()
:
Flight::request()->data->getData();

Gras Double
- 15,901
- 8
- 56
- 54
1
You can convert the flight\util\Collection Object
to an array by casting it to an array.
For example try:
$myArray = (array)Flight::request()->data;
// Sometimes you need to pop the first element
$myArray = array_pop($myArray);

tmuecksch
- 6,222
- 6
- 40
- 61
0
You can just use $_POST[], as of standard php. http://se1.php.net/manual/en/reserved.variables.post.php

user3840316
- 11
- 1
0
Yau don't need another class, you can use self-PHP global varriable, it is easy
I'm using that like;
<?php
Flight::route('POST /post-meta', function(){
print_r($_POST);
});
?>
JSON sample;
<?php
Flight::route('POST /report', function(){
if(isset($_POST['reportcode'])){
$id = (int)base64_decode($_POST['reportcode']);
if(Flight::db()->count() == 0){
$return['status'] = "ok";
$return['content'] = "<b>Succesfuly</b> sent your report this link"; //lang
}else{
$return['status'] = "already";
$return['content'] = "<b>Already</b> this link reported"; //lang
}
}else{
$return['status'] = 0;
}
echo json_encode($return);
});
?>

Fatih Mert Doğancan
- 1,016
- 14
- 21