2015-10-15 52 views
-2

我无法获取数组的值。请帮帮我。我使用的是angularjs和php slim框架。PHP:获取数组中的特定数据

这是我在PHP代码:

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内部服务器错误)。谢谢!

+0

检查您的日志。 –

+0

'$ data'包含什么。 – Darren

+0

我使用json_decode方法post来包含来自工厂和控制器的值$ scope.arr –

回答

0

它看起来像你期待在服务器的JSON,但你从$httpform-urlencoded

添加标题删除headers

此外,你必须自己序列化数据发送表格编码

+0

仍然不起作用。不知道该怎么办:$ mynames = array_values($ data ['name']); ..是我的线是正确的?我要序列化数组中的对象。对不起,我是PHP新手。谢谢您的回答。 –