0

I creating APP with SMS function and I want to display text messages and their details. I tried do something like this but this doesn't work. Here is format data from SMS API:

object(stdClass)#30 (1) {
  ["items"]=>
  array(2) {
    [0]=>
    object(stdClass)#31 (7) {
      ["id"]=>
      int(396629)
      ["type"]=>
      string(2) "nd"
      ["phone"]=>
      string(12) "123456789"
      ["recived"]=>
      string(19) "2016-01-16 16:36:42"
      ["blacklist"]=>
      bool(false)
      ["text"]=>
      string(19) "Vrs wifi nie dziala"
      ["contact"]=>
      object(stdClass)#32 (10) {
        ["id"]=>
        string(9) "272075197"
        ["first_name"]=>
        string(6) "FirstName"
        ["last_name"]=>
        string(5) "LastName"
        ["company"]=>
        string(0) ""
        ["phone"]=>
        string(12) "123456789"
        ["email"]=>
        string(0) ""
        ["tax_id"]=>
        string(0) ""
        ["city"]=>
        string(0) ""
        ["address"]=>
        string(0) ""
        ["description"]=>
        string(0) ""
      }
    }
    [1]=>
    object(stdClass)#33 (7) {
      ["id"]=>
      int(396609)
      ["type"]=>
      string(2) "nd"
      ["phone"]=>
      string(12) "123456789"
      ["recived"]=>
      string(19) "2016-01-16 14:36:53"
      ["blacklist"]=>
      bool(false)
      ["text"]=>
      string(3) "vrs"
      ["contact"]=>
      object(stdClass)#34 (10) {
        ["id"]=>
        string(9) "272075197"
        ["first_name"]=>
        string(6) "FirstName"
        ["last_name"]=>
        string(5) "LastName"
        ["company"]=>
        string(0) ""
        ["phone"]=>
        string(12) "123456789"
        ["email"]=>
        string(0) ""
        ["tax_id"]=>
        string(0) ""
        ["city"]=>
        string(0) ""
        ["address"]=>
        string(0) ""
        ["description"]=>
        string(0) ""
      }
    }
  }
}

I do somethink like this:

$result = $serwersms->messages->recived('eco|nd|ndi|pre');

    $output = array();
    foreach ( $result->items as $key ) {
        $row = array();
        $row[] = $key->id;
        //var_dump RETURN: int(396609), etc.
        $row[] = $key->phone;
        $row[] = $key->text;
        $output[] = $row;

    }

    $output = array( "data" => $output );
    echo json_encode($output);

And AJAX:

$.ajax({
        url : "MY_URL_TO_FUNCTION",
        type: "GET",
        dataType: "JSON",
        success: function(data) {

            console.log(data.id);

        },
        error: function (jqXHR, textStatus, errorThrown) {
            console.log('Error');
        }
});

But I don't getting data, only 'undefined' in console. AJAX response:

{"data":[[396629,"123456789","Vrs wifi nie dziala"],[396609,"123456789","vrs"]]}

What is wrong?

Damian
  • 525
  • 2
  • 11
  • 24
  • Instead of only printing `'Error'` you should check/print `textStatus` and `errorThrown` and the console/network tab of your developer tools ;) – Andreas Jan 16 '16 at 16:28
  • I update question, now I getting 'undefined' – Damian Jan 16 '16 at 16:30
  • 2
    `data` is an object with one property `"data"` which holds an array containing arrays which hold the data you're looking for. There is no object with a property `id`. You have to iterate over the elements of `data["data"]`. – Andreas Jan 16 '16 at 16:35
  • I do somethink like this: I add index to array in function loop: $row['id'] = $key->id and now, how can I get only 'id' from this? Because from your idea, I can't get it – Damian Jan 16 '16 at 16:43

0 Answers0