Jump to content

Multi-Dimensional Array Help


RJS

Recommended Posts

I am using NuSoap to make a WSDL request to a server (that I don't own).

require_once('lib/nusoap.php');
$client = new soapclient('site.wsdl', true);
$param = array('address' => $address,'apt'=>$apt,'city'=>$city,'state'=>$state,'zip'=>$state);
$result = $client->call('getAddressData', array('parameters' => $param), '', '', false, true);

 

$results is a multi dimensional array.

 

If there is no results from the search print_r($result) displays:

 

Array
(
    [searchResults] => 
)

 

If one result is returned print_r($result) displays:

Array
(
    [searchResults] => Array
        (
            [ArrayOfString] => Array
                (
                    [0] => Array
                        (
                            [string] => Array
                                (
                                    [0] => 11101 WEST 580 SOUTH STREET
                                    [1] => 
                                    [2] => OREM
                                    [3] => UT
                                    [4] => 84458
                                    [5] => STATUS
                                )
                        )
                )
        )
)

 

If more than one result is return print_r($result) returns:

Array
(
    [searchResults] => Array
        (
            [ArrayOfString] => Array
                (
                    [0] => Array
                        (
                            [string] => Array
                                (
                                    [0] => 11101 WEST 580 SOUTH STREET
                                    [1] => 
                                    [2] => OREM
                                    [3] => UT
                                    [4] => 84458
                                    [5] => STATUS
                                )
                        )

                    [1] => Array
                        (
                            [string] => Array
                                (
                                    [0] => 11101 WEST 580 SOUTHWEST STREET
                                    [1] => 
                                    [2] => OREM
                                    [3] => UT
                                    [4] => 84458
                                    [5] => STATUS
                                )
                        )

                    [2] => Array
                        (
                            [string] => Array
                                (
                                    [0] => 11101 WEST 1580 EAST STREET
                                    [1] => 
                                    [2] => OREM
                                    [3] => UT
                                    [4] => 84458
                                    [5] => STATUS
                                )
                        )
                    [3] => Array
                        (
                            [string] => Array
                                (
                                    [0] => 11101 WEST 5560 NORTH STREET
                                    [1] => 
                                    [2] => OREM
                                    [3] => UT
                                    [4] => 84458
                                    [5] => STATUS
                                )
                        )
                )
        )
)

How can I tell if there are 0, 1 or more than one results returned?

 

I tried this: count($return[searchResults][ArrayOfString])

but that returns a 1 even if there are no results returned.

 

What is the most efficient way to tell how many results there are in my array?

 

Thank You.

 

Link to comment
https://forums.phpfreaks.com/topic/71053-multi-dimensional-array-help/
Share on other sites

try

$num_of_res = $result['searchResults']['ArrayOfString'] ? count($result['searchResults']['ArrayOfString']) : 0;

 

Worked like a charm.  I appreciate it.  Can you briefly explain the code? Especially with the "?".  I want to make sure I understand it.

 

Thanks again!

Archived

This topic is now archived and is closed to further replies.

×
×
  • Create New...

Important Information

We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue.