PHP:获取数组中的特定数据
我无法获取数组的值。请帮帮我。我正在使用angularjs和php slim框架 这是我用PHP编写的代码:PHP:获取数组中的特定数据,php,arrays,angularjs,Php,Arrays,Angularjs,我无法获取数组的值。请帮帮我。我正在使用angularjs和php slim框架 这是我用PHP编写的代码: function testArray(){ $app = Slim::getInstance(); $app->response()->header("Content-Type", "application/json"); $post = json_decode($app->request()->getBody()); $data = get_object_
function testArray(){
$app = Slim::getInstance();
$app->response()->header("Content-Type", "application/json");
$post = json_decode($app->request()->getBody());
$data = get_object_vars($post);
echo json_encode(array_values($data['name']));}
这是我在控制器和工厂中的代码:
app.controller('fooCtrl', ['$scope', 'adminFactory', function($scope, adminFactory) {
$scope.arr = [{name:"rence"}, {name:"asd"}, {name:"qwe"}, {name:"qwe"}]; //your array
adminFactory.sendData($scope.arr).then(function(data) {
console.log(data);
});}]);
adminFactory.sendData = function(arr) {
return $http({
withCredentials: false,
method: 'post',
url: urlBase + '/test',
headers: {'Content-Type': 'application/x-www-form-urlencoded'},
data: {'arr': arr}
});
};
我从php得到一个错误。500(内部服务器错误)。谢谢大家! 看起来您在服务器上需要json,但您添加了
表单urlencoded
从$http
中删除标题
此外,您还必须自己序列化数据以发送表单编码的什么是$data
包含的内容。我使用json_decode方法post来包含来自factory和controller$scope.arr的值仍然不起作用。不知道在这行中要做什么:$mynames=array_values($data['name'])。。我的线路对吗?我将序列化数组中的对象。对不起,我是PHP的新手。谢谢你的回答。