1

An incoming data feed is in the form of an array. However, each array element contains multiple data fields (both field name and field data). The sample below shows the content of each array element. Using PHP, how do I extract the field names and the associated data?

Thanks for your assistance!

stdClass Object (
    [Cancelled] =>
    [MessageID] => 999999
    [Queued] =>
    [ReferenceID] => FRIDAY
    [SMSError] => NoError
    [SMSIncomingMessages] => stdClass Object (
        [SMSIncomingMessage] => stdClass Object (
        [FromPhoneNumber] => 1999999999
        [IncomingMessageID] => 0byyyyyyy 
        [Message] => 45-64-07
        [ResponseReceiveDate] => 2012-01-07
    )
)
[Sent] => 1
[SentDateTime] => 2012-01-07)
Francis Lewis
  • 8,872
  • 9
  • 55
  • 65
Paul Thomas
  • 281
  • 5
  • 11
  • See this similar post which answers your question: http://stackoverflow.com/questions/2699086/php-sort-multidimensional-array-by-value – Martin Dandanell Jan 07 '12 at 15:36
  • Can you recopy the output of the array elements, maintaining whitespace, and use the `{}` button instead of `"` to format the output? – Jared Farrish Jan 07 '12 at 16:28

2 Answers2

1

What you have isn't exactly an array, but an object instead, so you have to access it using pointers. I'll show a couple examples: In this example, I'll call your output $output

If this were an array, you would call elements like this:

echo $output['MessageID'];
echo $output['SMSIncomingMessages']['SMSIncomingMessage']['IncomingMessageID'];

But since this is an stdClass Object, you'll have to access it like this:

// will echo 999999
echo $output->MessageID;

It's also the same with multi-dimensions:

// will echo 0byyyyyyy
echo $output->SMSIncomingMessages->SMSIncomingMessage->IncomingMessageID;

You can still loop through the object like you would an array, but when you access the elements of the array, they still have to be accessed using a pointer.

Francis Lewis
  • 8,872
  • 9
  • 55
  • 65
0

This function will sort multidimensional arrays:

function array_sort_by_column(&$arr, $col, $dir = SORT_ASC) {
    $sort_col = array();
    foreach ($arr as $key=> $row) {
        $sort_col[$key] = $row[$col];
    }

    array_multisort($sort_col, $dir, $arr);
}


array_sort_by_column($array, 'order');
Radix
  • 667
  • 5
  • 28