1

I am sending an array over HTTP POST to backend API which is PHP.

js

var form_data = [];
for (var k = 0; k < $scope.Tablelist.length; k++){
  if($scope.Tablelist[k].selected == true){
    var id = $scope.Tablelist[k].id;
    var docno = $scope.Tablelist[k].quote_no;
    form_data.push({id: id, docno:docno})
  }
}
if(form_data.length>0){
  $http({
    method: 'POST',
    url: "api/purchase/purchase.php",
    data: {
      modul: 'PRICE',
      action: 'delete',
      form_data: form_data,
    },
    headers: {
      'Content-Type': 'application/x-www-form-urlencoded',
    }
  })

PHP

$form_data = (array)$request->form_data;
foreach ($form_data as $key => $value) {
     echo "Key: $key; Value: $value";
}

it gave me an error Object of class stdClass could not be converted to string.

I am trying to get the value of id and docno of each array.

How can i do that?

var_dump($form_data)

"array(3) {↵ [0]=>↵ object(stdClass)#3 (2) {↵ ["id"]=>↵ string(1) "6"↵ ["docno"]=>↵ string(5) "test4"↵ }↵ [1]=>↵ object(stdClass)#4 (2) {↵ ["id"]=>↵ string(1) "7"↵ ["docno"]=>↵ string(5) "test3"↵ }↵ [2]=>↵ object(stdClass)#5 (2) {↵ ["id"]=>↵ string(1) "4"↵ ["docno"]=>↵ string(5) "test2"↵ }↵}↵"

var_dump($request->form_data)

"array(3) {↵ [0]=>↵ object(stdClass)#3 (2) {↵ ["id"]=>↵ string(1) "6"↵ ["docno"]=>↵ string(5) "test4"↵ }↵ [1]=>↵ object(stdClass)#4 (2) {↵ ["id"]=>↵ string(1) "7"↵ ["docno"]=>↵ string(5) "test3"↵ }↵ [2]=>↵ object(stdClass)#5 (2) {↵ ["id"]=>↵ string(1) "4"↵ ["docno"]=>↵ string(5) "test2"↵ }↵}↵"

5
  • can you share the var_dump or print_r of $form_data or $request->form_data? Commented May 18, 2016 at 3:23
  • updated with var_dump Commented May 18, 2016 at 3:28
  • which one?? $form_data or $request->form_data Commented May 18, 2016 at 3:29
  • updated with both of $form_data and $request->form_data Commented May 18, 2016 at 3:31
  • Your $value is not a string. $value->docno and $value->id will work. Commented May 18, 2016 at 3:32

2 Answers 2

1

Remember one things, The type casting of like this never convert the inner Object. So you need to access them using the -> sign.

And your $value is also an array you can't echo it.

From your var_dump of objects, you can notice that the inner array looks an object not array. So i suggest to use this as object.

Important: You can encode as json the form_data in your JavaScript code and in PHP just decode with true properties so that you can get an associative array.

As per your requirement, I am trying to get the value of id and docno of each array.

$form_data = (array) $request->form_data;
foreach ($form_data as $key => $value) {
    echo "Key:".$key;
    echo $value->docno." and ".$value->id;
}

Try to access the inner array as object or again type case it.

Note: Also you can use get_object_vars for type case an object to an array. More details

Sign up to request clarification or add additional context in comments.

4 Comments

Thanks for the explanation, i am understanding now together with answer from @Sibiraj PR
after some consideration, i will mark this as helpful. i have learn much this answer including the answer from @SibirajPR
ow, never know this feature before, thanks again, learn again. haha
As per your requirement, I update my answer. Actually i did the main part for your by using print_r.
1

You can try like this.

$form_data = (array)$request->form_data;
foreach ($form_data as $key => $value) {
 echo "Key: $value->id; Value: $value->docno";
}

1 Comment

simple and to the point, in order to get the value, i have to access the object properties as explained by @Frayne Konok

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.