0

Dumb question. Below is my array. I got the first dimension to return to a table with a foreach loop, how do I iterate the 'work_order_status' part of the array to be able to set it into a table value?

Array
    (
        [transaction_id] => 2014102413362746N1SSCSYY9PFSUS85-0
        [response_code] => 200
        [work_order] => 151262
        [percent_complete] => 100
        [duplicate_records] => 0
        [work_order_status] => Array
            (
                [record] => Array
                    (
                        [0] => Array
                            (
                                [record_count] => 1590
                                [percent_of_total] => 40.52
                                [description] => Successful delivery
                            )

                        [1] => Array
                            (
                                [record_count] => 2
                                [percent_of_total] => .05
                                [description] => Invalid Number - too short
                            )

                        [2] => Array
                            (
                                [record_count] => 2
                                [percent_of_total] => .05
                                [description] => Invalid Number - Illegal NPA-NXX
                            )

                        [3] => Array
                            (
                                [record_count] => 2
                                [percent_of_total] => .05
                                [description] => Invalid Number - NULL submission
                            )

                        [4] => Array
                            (
                                [record_count] => 1
                                [percent_of_total] => .03
                                [description] => Invalid Number - Not a dialable number
                            )

                        [5] => Array
                            (
                                [record_count] => 996
                                [percent_of_total] => 25.38
                                [description] => Invalid Request - not a WIRELESS number
                            )

                        [6] => Array
                            (
                                [record_count] => 867
                                [percent_of_total] => 22.09
                                [description] => Max Account Retries Exceeded
                            )

                        [7] => Array
                            (
                                [record_count] => 18
                                [percent_of_total] => .46
                                [description] => Voicemail delivery unconfirmed
                            )

                        [8] => Array
                            (
                                [record_count] => 3
                                [percent_of_total] => .08
                                [description] => Voicemail played for 0 seconds
                            )

                        [9] => Array
                            (
                                [record_count] => 341
                                [percent_of_total] => 8.69
                                [description] => Voicemail played for less than message length.
                            )

                        [10] => Array
                            (
                                [record_count] => 18
                                [percent_of_total] => .46
                                [description] => No Answer
                            )

                        [11] => Array
                            (
                                [record_count] => 76
                                [percent_of_total] => 1.94
                                [description] => Network Disconnect (FEHU)
                            )

                        [12] => Array
                            (
                                [record_count] => 8
                                [percent_of_total] => .2
                                [description] => Duplicate Records
                            )

                    )

            )

    )
5
  • foreach($array['work_order_status'] as $something) {} Commented Oct 24, 2014 at 14:35
  • 1
    a second foreach loop ? provide what you have done, and what you would like your table to look like so we can help further Commented Oct 24, 2014 at 14:35
  • You have to iterate over the first array with a loop, and inside that loop, iterate again over the work_order_status array. Commented Oct 24, 2014 at 14:35
  • We don't allow dumb questions here. Commented Oct 24, 2014 at 14:38
  • I just want to return the contents of 'record' key into its own table, nothing more. Problem with testing this is the API I am connecting to doesn't let me query more than once every 5 minutes so my responses are going to slow as I am working on other stuff. Biggest problem is I am normally used to referring to the key position as [0] or [1] and in this case it's returning the first byte each time. I have not worked with multidimension arrays before in php obviously. Commented Oct 24, 2014 at 15:40

2 Answers 2

3

You can use a foreach loop such as:

foreach($array['work_order_status'] as $value) {
    //some code here
}

or if the ['record'] part of the array is actually the one you wanted to iterate over then you could do something like this:

foreach($array['work_order_status']['record'] as $value) {
    //some code here
}

Edit after comments:

foreach ($xml2['work_order_status']['record'] as $value)  {  
    echo "<td>" . $xml2 . "</td>" . 
    "<td>" . $value['record_count'] . "</td>" .
    "<td>" . $value['percent_of_total'] . "</td>" . 
    "<td>" . $value['description'] . "</td><tr>";
}
Sign up to request clarification or add additional context in comments.

6 Comments

Problem with attempting this is the API I am returning these results limits me to one query/5 minutes and oops enter saves the reply. foreach ($xml2['work_order_status'] as $value) { echo "<td>" . $xml2 . "</td><td>" . $value . "</td><tr>"; }
You would have to elaborate a bit more in order for me to be of any help to you.
Yep sorry. I want to return record_count, percent_of_total, and description of every 'record' array element. The output comes in as XML and I am using json_decode/json_encode to put it into an array, I just want to build a <td> of each 'record' array element instead of displaying it in print_r format. $data = curl_exec($ch); $xml2 = json_decode( json_encode(simplexml_load_string($data)) , 1); foreach ($xml2['work_order_status']['record'] as $key =>$value) { echo "<td>" . $xml2 . "</td><td>" . $value . "</td><tr>"; } is returning 'Array' over and over now at least.
@Alex Using that foreach loop your keys are 1,2,3,... and your values are arrays containing [record_count],[percent_of_total], and [description]. So if you change your foreach loop to something like this it should work as you want it to: foreach ($xml2['work_order_status']['record'] as $key =>$value) { echo "<td>" . $xml2 . "</td><td>" . $value['record_count'] . "</td><tr>" . "<td>" . $xml2 . "</td><td>" . $value['percent_of_total'] . "</td><tr>" . "<td>" . $xml2 . "</td><td>" . $value['description'] . "</td><tr>"; } This way you output 3 rows with the desired values on each iteration.
Thanks, I wil lgive that a try in about 5 minutes :)
|
0
foreach ($xml2['work_order_status']['record'] as $key =>$value) { 
    echo "<td>" . $xml2 . "</td><td>" . $value['record_count'] . "</td><tr>" . "<td>" . $xml2 . "</td><td>" . $value['percent_of_total'] . "</td><tr>" . "<td>" . $xml2 . "</td><td>" . $value['description'] . "</td><tr>";
     }

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.