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)
2
  • See this similar post which answers your question: stackoverflow.com/questions/2699086/… Commented Jan 7, 2012 at 15:36
  • Can you recopy the output of the array elements, maintaining whitespace, and use the {} button instead of " to format the output? Commented Jan 7, 2012 at 16:28

2 Answers 2

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.

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

2 Comments

Thanks for your help! I used your method plus a loop and was able to extract all the required data.
No problem! Glad it worked out for you :) Please mark the question as answered be selecting the checkmark under the answer.
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');

Comments

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.