0

I have an object that has private properties (accessible via getters and setters, and specified in the _sleep magic function). Some of those private properties are actually arrays of objects, which themselves have private properties in the same manner. Some of those properties are arrays of objects, and so on and so forth, down the rabbit hole we go.

I want to json_encode the parent object and all of the children arrays of objects, etc, etc.

Here is the function I have so far:

json_encode(recursiveFlatten($contract));

function recursiveFlatten($object) {
    $arr = array();
    $return = array();

    if (!is_array($object)) {
        error_log("Preparing to flatten " . get_class($object));
        $arr[] = $object;
    } else {
        error_log("Preparing to flatten array");
        $arr = $object;
    }

    foreach ($arr as $object) {

        error_log("Flattening " . get_class($object));

        $flattenedObject = array();
        foreach (get_class_methods(get_class($object)) as $method) {
            error_log("Should I flatten " . $method . "?");
            if (startsWith($method, "get")) {
                error_log("Yes, flattening " . $method);
                $parameter = lcfirst(substr($method, 3));
                $value = $object->$method();

                if (is_array($value)) {
                    error_log($method . " gives " . $parameter . " which yields an array value... recursing");
                    $value = recursiveFlatten($value);
                    error_log("Recursion yielded the following value: " . $value);
                }

                error_log($method . " gives " . $parameter . " which is not an array so it's value is " . $value);

                error_log("Assign " . $parameter . " to flattenedObject with value " . $value);

                $flattenedObject[$parameter] = $value;
            }
        }

        $return[] = $flattenedObject;
    }

    return $return;
}

But this is returning json which doesn't look right to me.

[
   {
      "accountId":"7",
      "billingContactId":"1",
      "designFeeId":"295",
      "id":"1",
      "isDeleted":"0",
      "leadSourceId":"1",
      "managerId":"415",
      "notes":"A note",
      "prodContactId":"1",
      "statusId":"1",
      "tradedValue":"0",
      "createdById":"415",
      "createdDate":"2013-07-02 10:05:53",
      "designFeeValue":"295",
      "doInvoice":"0",
      "isPaper":"1",
      "primaryContactId":"1",
      "firstMonthDisplay":"01\/2014",
      "managerDisplayName":"Lynn Owens",
      "numInsertions":3,
      "statusText":"Proposal",
      "totalValue":75,
      "paidValue":50,
      "unpaidValue":25,
      "insertions":[
         {
            "adId":"1",
            "contractId":"1",
            "createdById":"415",
            "createdDate":"2013-07-02 16:09:19",
            "earlyOut":"0",
            "id":"1",
            "isCanceled":"0",
            "magazineId":"1",
            "month":"1",
            "notes":"insertion one",
            "paidValue":"25",
            "value":"25",
            "year":"2014"
         },
         {
            "adId":"1",
            "contractId":"1",
            "createdById":"415",
            "createdDate":"2013-07-02 16:10:03",
            "earlyOut":"0",
            "id":"2",
            "isCanceled":"0",
            "magazineId":"1",
            "month":"2",
            "notes":"insertion two",
            "paidValue":"25",
            "value":"25",
            "year":"2014"
         },
         {
            "adId":"1",
            "contractId":"1",
            "createdById":"415",
            "createdDate":"2013-07-02 16:10:03",
            "earlyOut":"0",
            "id":"3",
            "isCanceled":"0",
            "magazineId":"1",
            "month":"3",
            "notes":"insertion three",
            "paidValue":"0",
            "value":"25",
            "year":"2014"
         }
      ]
   }
]

I see here that the parent object's "insertions" parameter, which is an array of three insertion objects, is not itself one parameter with three insertion children but rather three "insertions" parameters.

Is this right?

When I try to access the individual insertion objects on the client side with javascript, I'm not having any luck.

// Set the account details
    $.ajax({
        type: 'POST',
        url: 'ajaxController.php',
        dataType: 'json',
        data: {
            e: "getContractById",
            contractId: selectedContractId
        },
        success: function (data, textStatus, jqXHR) {
            console.log(data);
            console.log(data.accountId);
            console.log(data.insertions);
            $.each(data.insertions, function(key, insertion) {
                console.log(insertion.notes);
            });
        }
    });

This is printing:

[The JSON]
undefined
undefined
Type error, e is undefined (jquery.min.js)

I sense that I'm making some simple mistake, but I can't for the life of me see it.

Please help? I'm stuck in the way that I've just looked at this for way too long.

lonesomeday
  • 233,373
  • 50
  • 316
  • 318
Lurk21
  • 2,307
  • 12
  • 37
  • 55
  • 6
    *"To help the readability of this, I'll convert the JSON into XML"* Perhaps the first time in history this sentence has been uttered. :D –  Jul 05 '13 at 16:34
  • I have removed the XML and prettified the JSON to make it readable. http://jsonformatter.curiousconcept.com/ is a good tool for this. – lonesomeday Jul 05 '13 at 16:36
  • Unlike the XML you posted before, in the JSON `insertions` *is* an array of 3 insertion objects. – Barmar Jul 05 '13 at 16:39
  • 1
    Your json has an array on the top level, in your js code you're assuming it to be an object. Try `data[0].accountId` etc. – georg Jul 05 '13 at 16:40
  • `insertion` is an **ARRAY** (`[...]`) that contains three **OBJECTS** (`{...}`). – Marc B Jul 05 '13 at 17:13

2 Answers2

2

try:

data[0].accountId
// or
data[0]['accountId']
bitWorking
  • 12,485
  • 1
  • 32
  • 38
0

your data is an array that has 1 element did you try the below in your javascript :

data[0].accountId;
data[0].insertions;
Mehdi Karamosly
  • 5,388
  • 2
  • 32
  • 50
  • Thanks, this resolved it. Unfortunately I can only award one answer with the check mark, but thank you!! – Lurk21 Jul 05 '13 at 16:54