Web Advertising | Pozycjonowanie | Loans | Loans | Credit Cards
Fun with PHP arrays [Archive] - ZGeek

PDA

View Full Version : Fun with PHP arrays


Pirate
25-10-2005, 12:41 PM
I'm stuck on a bit of php coding.

Heres the run down.

The script is a search script, it uses a webservice and thanks to Dwarfthrowers help that side all works perfectly. But my problem comes from the returned arrays.

If the search finds multiple people, it returns this array.
Array
(
[PhoneSearchResult] => Array
(
[Phone] => Array
(
[PhoneItem] => Array
(
[0] => Array
(
[PIN] => 0
[HIN] => 0
[GIVEN_NAME_1] => George
[GIVEN_NAME_2] => J
[GIVEN_NAME_3] =>
[SURNAME] => Bums
[AD1] => 666 MOUNTAIN RD
[FUN] =>
[TN1] => 666
[THN] => MOUNTAIN
[THT] => RD
[LOC] => MOUNTAINDALE
[STT] => NSW
[PCD] => 2347
[BG1] =>
[PDN] =>
[ALN] =>
[SOURCE] => Phone
[SOURCEDATE] => 25/08/2005 12:00:00 AM
[PUBLIC_NUMBER] => 0123456789
[FCS_USAGE_CODE] => R
[FCS_TYPE_OF_SERVICE] =>
[COMPANY_NAME] =>
)

[1] => Array
(
[PIN] => 0
[HIN] => 0
[GIVEN_NAME_1] => Garry
[GIVEN_NAME_2] =>
[GIVEN_NAME_3] =>
[SURNAME] => Fubble
[AD1] => 12 BORTHCOTT RD
[FUN] =>
[TN1] => 12
[THN] => BORTHCOTT
[THT] => RD
[LOC] => WALOR PARK
[STT] => NSW
[PCD] => 3256
[BG1] =>
[PDN] =>
[ALN] =>
[SOURCE] => PHONE
[SOURCEDATE] => 8/10/2004 12:00:00 AM
[PUBLIC_NUMBER] => 01233456867
[FCS_USAGE_CODE] => R
[FCS_TYPE_OF_SERVICE] => GSM
[COMPANY_NAME] =>
)

[2] => Array
(
[PIN] => 0
[HIN] => 0
[GIVEN_NAME_1] => Tom
[GIVEN_NAME_2] =>
[GIVEN_NAME_3] =>
[SURNAME] => Farts
[AD1] => 54 WEATBOM ST
[FUN] =>
[TN1] => 54
[THN] => WEATBOM
[THT] => ST
[LOC] => RANMORE
[STT] => NSW
[PCD] => 2458
[BG1] =>
[PDN] =>
[ALN] =>
[SOURCE] => Phone
[SOURCEDATE] => 13/09/2005 12:00:00 AM
[PUBLIC_NUMBER] => 021354987
[FCS_USAGE_CODE] => R
[FCS_TYPE_OF_SERVICE] =>
[COMPANY_NAME] =>
)

)

)

[Credit] => Array
(
[Total] => 88256
)

)

)

But if it only finds one person it returns this array

Array
(
[PhoneSearchResult] => Array
(
[Phone] => Array
(
[PhoneItem] => Array
(
[PIN] => 0
[HIN] => 0
[GIVEN_NAME_1] => Tom
[GIVEN_NAME_2] =>
[GIVEN_NAME_3] =>
[SURNAME] => Farts
[AD1] => 54 WEATBOM ST
[FUN] =>
[TN1] => 54
[THN] => WEATBOM
[THT] => ST
[LOC] => RANMORE
[STT] => NSW
[PCD] => 2458
[BG1] =>
[PDN] =>
[ALN] =>
[SOURCE] => Phone
[SOURCEDATE] => 13/09/2005 12:00:00 AM
[PUBLIC_NUMBER] => 021354987
[FCS_USAGE_CODE] => R
[FCS_TYPE_OF_SERVICE] =>
[COMPANY_NAME] =>
)

)

[Credit] => Array
(
[Total] => 88266
)

)

)Notice it drops the numbering on the array. This is my problem, without this number it goes screwy and the results are all garbled.

What I am trying to do is create a bit of code that loads a different table content if the array isn't numbered. And this is where I am very stuck as I can't find anything to tell the script that "if this value is nothing, then load this code"... sorry if this is confusing, Im not too good with terminology. Below is the script I'm using. <?php
/*
* $Id: wsdlclient1.php,v 1.2 2004/03/15 23:06:17 snichol Exp $
*
* WSDL client sample.
*
* Service: WSDL
* Payload: document/literal
* Transport: http
* Authentication: none
*/
require_once('../nusoap-0.7.2/lib/nusoap.php'); // Tony's path
//require_once('../lib/nusoap.php'); // Gabe's path
$proxyhost = isset($_POST['proxyhost']) ? $_POST['proxyhost'] : '';
$proxyport = isset($_POST['proxyport']) ? $_POST['proxyport'] : '';
$proxyusername = isset($_POST['proxyusername']) ? $_POST['proxyusername'] : '';
$proxypassword = isset($_POST['proxypassword']) ? $_POST['proxypassword'] : '';

$client = new soapclient('http://search.fcsonline.com.au/FCSSearchWS/FCSSearch.asmx?WSDL', true,
$proxyhost, $proxyport, $proxyusername, $proxypassword);
$fcsuser = '';
$fcspass = '';
//$fcsuser = $_GET['user'];
//$fcspass = $_GET['pass'];
$auth="<AuthHeader xmlns=\"http://202.125.163.83/FCSSearchWS/FCSSearch.asmx\"><UserName>$fcsuser</UserName><Password>$fcspass</Password></AuthHeader>";
$client->setHeaders($auth);
$err = $client->getError();
if ($err) {
echo '<h2>Constructor error</h2><pre>' . $err . '</pre>';
}

// Doc/lit parameters get wrapped

$strPin = $_GET['pin'];
$strName1 = '';
$strName2 = $_GET['name2'];
$strName3 = $_GET['name3'];
$strSurname = 'Brisciani';
$strUnit = $_GET['unit'];
$strNumLow = $_GET['numlow'];
$strNumHigh = $_GET['numhigh'];
$strStreet = $_GET['street'];
$strSuburb = $_GET['suburb'];
$strState = $_GET['state'];
$strPostcode = $_GET['postcode'];
$strBuildname = $_GET['buildname'];
$strPObox = $_GET['pobox'];
$strReference = $_GET['ref'];

$param = array( 'PIN' => $strPin,
'GivenName1' => $strName1,
'GivenName2' => $strName2,
'GivenName3' => $strName3,
'Surname' => $strSurname,
'UnitNumber' => $strUnit,
'StreetNumberLow' => $strNumLow,
'StreetNumberHigh' => $strNumHigh,
'StreetName' => $strStreet,
'Suburb' => $strSuburb,
'State' => $strState,
'Postcode' => $strPostcode,
'BuildingName' => $strBuildname,
'POBox' => $strPObox,
'ClientRef' => $strReference
);

$result = $client->call('PhoneSearch', array('parameters' => $param), '', '', false, true);

// Check for a fault
if ($client->fault) {
echo '<h2>Fault</h2><pre>';
print_r($result);
echo '</pre>';
} else {
// Check for errors
$err = $client->getError();
if ($err) {
// Display the error
echo '<h2>Error</h2><pre>' . $err . '</pre>';
} else {


/***
echo "<pre>"; //You can uncomment this stuff to get a look at the returned array
print_r($result);
echo "</pre>";
***/


//We can work our way down through the $result to get what we need:
$PhoneSearchResult = $result['PhoneSearchResult'];
$Phone = $PhoneSearchResult['Phone'];
$PhoneItem = $Phone['PhoneItem']; // <-- That's the one we want to loop through

//Set up our array to hold the post codes
$pCodes = array();
// $pCodeCount = 0;

echo "<table border=\"1\">";
echo "<tr><th>Surname</th><th>Given Name</th><th>Address</th><th>Suburb</th><th>State</th><th>Post Code</th><th>Phone</th></tr>";
foreach($Phone as &$item){
echo "<tr>";

echo "<td>single";
echo $item['SURNAME'];
echo "</td>";

echo "<td>";
echo $item['GIVEN_NAME_1'];
echo "</td>";

echo "<td>";
echo $item['AD1'];
echo "</td>";

echo "<td>";
echo $item['LOC'];
echo "</td>";

echo "<td>";
echo $item['STT'];
echo "</td>";

echo "<td>";
echo $item['PCD'];
echo "</td>";

echo "<td>";
echo $item['PUBLIC_NUMBER'];
echo "</td>";

echo "</tr>";
}

if ($somthing='0') { //THIS IS WHERE I'M STUCK
foreach($PhoneItem as &$item){
echo "<tr>";

echo "<td>1";
echo $item['SURNAME'];
echo "</td>";

echo "<td>";
echo $item['GIVEN_NAME_1'];
echo "</td>";

echo "<td>";
echo $item['AD1'];
echo "</td>";

echo "<td>";
echo $item['LOC'];
echo "</td>";

echo "<td>";
echo $item['STT'];
echo "</td>";

echo "<td>";
echo $item['PCD'];
echo "</td>";

echo "<td>";
echo $item['PUBLIC_NUMBER'];
echo "</td>";

echo "</tr>";
}
}
echo "</table>";


}
}
//echo '<h2>Request</h2><pre>' . htmlspecialchars($client->request, ENT_QUOTES) . '</pre>';
//echo '<h2>Response</h2><pre>' . htmlspecialchars($client->response, ENT_QUOTES) . '</pre>';
//echo '<h2>Debug</h2><pre>' . htmlspecialchars($client->debug_str, ENT_QUOTES) . '</pre>';
?>

Spingo
25-10-2005, 01:44 PM
Shouldn't you be able to use the is_array function to distinguish between the two results?

i.e, is_array($array[PhoneSearchResult][Phone][PhoneItem][0]) should be true for the first case, but false for the second.

Pirate
25-10-2005, 02:11 PM
In what context would I use that? after the if?

AsterisK
25-10-2005, 02:13 PM
agreed.

if(is_array($array[PhoneSearchResult][Phone][PhoneItem][0])) {
// code if it is an array
// loop through the numbered arrays to call a function such as exampleAction() for each array instead of repeating code
} else {
// not an array
// call the exampleAction() mentioned above
}

Pirate
25-10-2005, 02:27 PM
sorry, I'm going to show my lameless now. I tried this echo "<table border=\"1\">";
echo "<tr><th>Surname</th><th>Given Name</th><th>Address</th><th>Suburb</th><th>State</th><th>Post Code</th><th>Phone</th></tr>";
// if (is_numeric($ArrNum)) {
if(is_array($array[PhoneSearchResult][Phone][PhoneItem][0])) {
foreach($PhoneItem as &$item){
echo "<tr>";

echo "<td>";
echo $item['SURNAME'];
echo "</td>";

echo "<td>";
echo $item['GIVEN_NAME_1'];
echo "</td>";

echo "<td>";
echo $item['AD1'];
echo "</td>";

echo "<td>";
echo $item['LOC'];
echo "</td>";

echo "<td>";
echo $item['STT'];
echo "</td>";

echo "<td>";
echo $item['PCD'];
echo "</td>";

echo "<td>";
echo $item['PUBLIC_NUMBER'];
echo "</td>";

echo "</tr>";
}
}
// if (empty($ArrNum)){
else {
foreach($Phone as &$item){
echo "<tr>";

echo "<td>";
echo $item['SURNAME'];
echo "</td>";

echo "<td>";
echo $item['GIVEN_NAME_1'];
echo "</td>";

echo "<td>";
echo $item['AD1'];
echo "</td>";

echo "<td>";
echo $item['LOC'];
echo "</td>";

echo "<td>";
echo $item['STT'];
echo "</td>";

echo "<td>";
echo $item['PCD'];
echo "</td>";

echo "<td>";
echo $item['PUBLIC_NUMBER'];
echo "</td>";

echo "</tr>";
}
}
echo "</table>";


}
} but it didn't work. I can't find any info on php.net about the exampleAction() thingo. :(

AsterisK
25-10-2005, 02:34 PM
sorry, I'm going to show my lameless now. I tried this echo "<table border=\"1\">";
echo "<tr><th>Surname</th><th>Given Name</th><th>Address</th><th>Suburb</th><th>State</th><th>Post Code</th><th>Phone</th></tr>";
// if (is_numeric($ArrNum)) {
if(is_array($array[PhoneSearchResult][Phone][PhoneItem][0])) {
foreach($PhoneItem as &$item){
echo "<tr>";

echo "<td>";
echo $item['SURNAME'];
echo "</td>";

echo "<td>";
echo $item['GIVEN_NAME_1'];
echo "</td>";

echo "<td>";
echo $item['AD1'];
echo "</td>";

echo "<td>";
echo $item['LOC'];
echo "</td>";

echo "<td>";
echo $item['STT'];
echo "</td>";

echo "<td>";
echo $item['PCD'];
echo "</td>";

echo "<td>";
echo $item['PUBLIC_NUMBER'];
echo "</td>";

echo "</tr>";
}
}
// if (empty($ArrNum)){
else {
foreach($Phone as &$item){
echo "<tr>";

echo "<td>";
echo $item['SURNAME'];
echo "</td>";

echo "<td>";
echo $item['GIVEN_NAME_1'];
echo "</td>";

echo "<td>";
echo $item['AD1'];
echo "</td>";

echo "<td>";
echo $item['LOC'];
echo "</td>";

echo "<td>";
echo $item['STT'];
echo "</td>";

echo "<td>";
echo $item['PCD'];
echo "</td>";

echo "<td>";
echo $item['PUBLIC_NUMBER'];
echo "</td>";

echo "</tr>";
}
}
echo "</table>";


}
} but it didn't work. I can't find any info on php.net about the exampleAction() thingo. :(

indeed you are lame :P

the exampleAction() thingo is a function you could write to take place of all the echo commands you used. code reusage :clap:

as for your code, you need to dig down to get to the PhoneItem part. As you have like a 5 dimensional array, you need to crawl each dimension, so like use nested foreach() functions. i.e.

foreach ($array as $item) {
foreach ($item as $item1) {
foreach ($item1 as $item2) {
foreach ($item2 as $item3) {
// do something
}
}
}
}

Pirate
25-10-2005, 02:46 PM
aah, i see. Sorta. My php coding skills are still to lame to make my own actions. I'm going to do some more reading on how the arrays work and this may help me understand how to do it.

Time to experiment! :)

Spingo
25-10-2005, 02:52 PM
Shouldn't you be able to use the is_array function to distinguish between the two results?

i.e, is_array($array[PhoneSearchResult][Phone][PhoneItem][0]) should be true for the first case, but false for the second.

I should have also said that $array should be substituted for the actualy name of the array that your SOAP service spits its data back to - which I think is $result in your case.

Try this code:


<?php
/*
* $Id: wsdlclient1.php,v 1.2 2004/03/15 23:06:17 snichol Exp $
*
* WSDL client sample.
*
* Service: WSDL
* Payload: document/literal
* Transport: http
* Authentication: none
*/
require_once('../nusoap-0.7.2/lib/nusoap.php'); // Tony's path
//require_once('../lib/nusoap.php'); // Gabe's path
$proxyhost = isset($_POST['proxyhost']) ? $_POST['proxyhost'] : '';
$proxyport = isset($_POST['proxyport']) ? $_POST['proxyport'] : '';
$proxyusername = isset($_POST['proxyusername']) ? $_POST['proxyusername'] : '';
$proxypassword = isset($_POST['proxypassword']) ? $_POST['proxypassword'] : '';

$client = new soapclient('http://search.fcsonline.com.au/FCSSearchWS/FCSSearch.asmx?WSDL', true, $proxyhost, $proxyport, $proxyusername, $proxypassword);
$fcsuser = '';
$fcspass = '';
//$fcsuser = $_GET['user'];
//$fcspass = $_GET['pass'];
$auth="<AuthHeader xmlns=\"http://202.125.163.83/FCSSearchWS/FCSSearch.asmx\"><UserName>$fcsuser</UserName><Password>$fcspass</Password></AuthHeader>";
$client->setHeaders($auth);
$err = $client->getError();
if ($err) {
echo '<h2>Constructor error</h2><pre>' . $err . '</pre>';
}

// Doc/lit parameters get wrapped

$strPin = $_GET['pin'];
$strName1 = '';
$strName2 = $_GET['name2'];
$strName3 = $_GET['name3'];
$strSurname = 'Brisciani';
$strUnit = $_GET['unit'];
$strNumLow = $_GET['numlow'];
$strNumHigh = $_GET['numhigh'];
$strStreet = $_GET['street'];
$strSuburb = $_GET['suburb'];
$strState = $_GET['state'];
$strPostcode = $_GET['postcode'];
$strBuildname = $_GET['buildname'];
$strPObox = $_GET['pobox'];
$strReference = $_GET['ref'];

$param = array( 'PIN' => $strPin,
'GivenName1' => $strName1,
'GivenName2' => $strName2,
'GivenName3' => $strName3,
'Surname' => $strSurname,
'UnitNumber' => $strUnit,
'StreetNumberLow' => $strNumLow,
'StreetNumberHigh' => $strNumHigh,
'StreetName' => $strStreet,
'Suburb' => $strSuburb,
'State' => $strState,
'Postcode' => $strPostcode,
'BuildingName' => $strBuildname,
'POBox' => $strPObox,
'ClientRef' => $strReference
);

$result = $client->call('PhoneSearch', array('parameters' => $param), '', '', false, true);

// Check for a fault
if ($client->fault) {
echo '<h2>Fault</h2><pre>';
print_r($result);
echo '</pre>';
} else {
// Check for errors
$err = $client->getError();
if ($err) {
// Display the error
echo '<h2>Error</h2><pre>' . $err . '</pre>';
} else {


/***
echo "<pre>"; //You can uncomment this stuff to get a look at the returned array
print_r($result);
echo "</pre>";
***/


//We can work our way down through the $result to get what we need:
$PhoneSearchResult = $result['PhoneSearchResult'];
$Phone = $PhoneSearchResult['Phone'];
$PhoneItem = $Phone['PhoneItem']; // <-- That's the one we want to loop through

//Set up our array to hold the post codes
$pCodes = array();
//$pCodeCount = 0;

echo "<table border=\"1\">";
echo "<tr><th>Surname</th><th>Given Name</th><th>Address</th><th>Suburb</th><th>State</th><th>Post Code</th><th>Phone</th></tr>";

// OMG Here is Spingo's 1337 as code! you owe him a beer, but you'd have to SHOW UP TO HIS BBQ TO GIVE ONE TO HIM!!! lolzoriffic!

if (is_array($PhoneItem[0]) == false) {
echo "<tr>";

echo "<td>single";
echo $PhoneItem['SURNAME'];
echo "</td>";

echo "<td>";
echo $PhoneItem['GIVEN_NAME_1'];
echo "</td>";

echo "<td>";
echo $PhoneItem['AD1'];
echo "</td>";

echo "<td>";
echo $PhoneItem['LOC'];
echo "</td>";

echo "<td>";
echo $PhoneItem['STT'];
echo "</td>";

echo "<td>";
echo $PhoneItem['PCD'];
echo "</td>";

echo "<td>";
echo $PhoneItem['PUBLIC_NUMBER'];
echo "</td>";

echo "</tr>";

} else {
foreach($PhoneItem as &$item) {
echo "<tr>";

echo "<td>1";
echo $item['SURNAME'];
echo "</td>";

echo "<td>";
echo $item['GIVEN_NAME_1'];
echo "</td>";

echo "<td>";
echo $item['AD1'];
echo "</td>";

echo "<td>";
echo $item['LOC'];
echo "</td>";

echo "<td>";
echo $item['STT'];
echo "</td>";

echo "<td>";
echo $item['PCD'];
echo "</td>";

echo "<td>";
echo $item['PUBLIC_NUMBER'];
echo "</td>";

echo "</tr>";
}
}
echo "</table>";


}
}
//echo '<h2>Request</h2><pre>' . htmlspecialchars($client->request, ENT_QUOTES) . '</pre>';
//echo '<h2>Response</h2><pre>' . htmlspecialchars($client->response, ENT_QUOTES) . '</pre>';
//echo '<h2>Debug</h2><pre>' . htmlspecialchars($client->debug_str, ENT_QUOTES) . '</pre>';
?>

( I also took the liberty in tidying up your code a bit - I'm such a nazi..)

Pirate
25-10-2005, 03:10 PM
Perfect! Thanks to you and dwarfthrower who also sent me and email with the correct code. I see I need to learn more about the is_array business. But I must say that this php is pretty damn good one once you know what the commands do.

You all rock! thanks for helping me out of hard one. I really appreciate it for not only helping me with work, but teaching me php :)