I have an array of data which contains child arrays, each child array can contain a different number of keys:
Example:
Parent array $service:
[0] => stdClass Object // 9 keys
(
[std] => 11:47
[etd] => On time
[platform] => 15
[operator] => Southern
[operatorCode] => SN
[serviceType] => train
[length] => 12
[serviceID] => 2R2kKgVAvyBh7d/FhIatXQ==
[rsid] => SN220703
)
[1] => stdClass Object // 7 keys
(
[std] => 11:47
[etd] => On time
[operator] => Southern
[operatorCode] => SN
[serviceType] => train
[serviceID] => OH2Ufaa4ToefjtmYSubfQA==
[rsid] => SN351100
)
If I dump the array for $service using:
foreach( $service as $index => $obj ){
$keys=array_keys( get_object_vars( $obj ) );
print_r ($keys);
}
I get:
Array
(
[0] => std
[1] => etd
[2] => operator
[3] => operatorCode
[4] => serviceType
[5] => length
[6] => serviceID
[7] => rsid
[8] => origin
[9] => destination
)
Array
(
[0] => std
[1] => etd
[2] => operator
[3] => operatorCode
[4] => serviceType
[5] => serviceID
[6] => rsid
[7] => origin
[8] => destination
)
The child arrays both contain a different number of keys, the first contains 10 and the second contain 9.
I then loop through each of the child arrays and write the results to a data table:
foreach( $keys as $key ){
${$key}=$obj->$key;
print_r (${$key});
}
The issue I have is if the second child array only has 9 keys, in this example "[5] => length" is missing from the second array, each successive loop writes the content of the missing key to my data table.
My question is, how can I check if the child arrays contain "[5] => length" before writing to the data table and if not present write nothing to the corresponding data table field.
Complete script:
require("../../../../sysscripts/rail/OpenLDBWS.php");
$OpenLDBWS = new OpenLDBWS("my_token");
$data = $OpenLDBWS->GetDepartureBoard(100,"$Station");
header("Content-Type: text/plain");
$results=$data->GetStationBoardResult;
$generatedAt=$results->generatedAt;
$origin_crs=$results->crs;
$message=$results->nrccMessages->message->{'_'};
$platform=$results->platformAvailable;
/* The services... */
$service=$results->trainServices->service;
if( is_array( $service ) ){
foreach( $service as $index => $obj ){
$keys=array_keys( get_object_vars( $obj ) );
print_r ($keys);
foreach( $keys as $key ){
${$key}=$obj->$key;
//print_r (${$key});
}
$origin_location=$origin->location->locationName;
$generatedAt = date("Y-m-d H:i");
$dest_location=$destination->location->locationName;
$crsName=$destination->location->crs;
$via=$destination->location->via;
$insertSQL = sprintf("INSERT INTO UKRail_departboards (RecordDate, ServiceID, Origin_location, Origin_crs, Platform, ScheduleTime, Estimated, Dest_location, Dest_crs, Operator, Message, IsCancelled, CancelReason, DelayedReason, DeleteCode, NoCarrages, Via, DeleteTime) VALUES (%s, %s, %s, %s, %s, %s, %s, %s, %s, %s, %s, %s, %s, %s, %s, %s, %s, %s)",
GetSQLValueString($_POST['RecordDate']= $generatedAt, "date"),
GetSQLValueString($_POST['ServiceID'] = $serviceID, "text"),
GetSQLValueString($_POST['Origin_location'] = $origin_location, "text"),
GetSQLValueString($_POST['Origin_crs'] = $origin_crs, "text"),
GetSQLValueString($_POST['Platform'] = $platform, "text"),
GetSQLValueString($_POST['ScheduleTime'] = $std, "text"),
GetSQLValueString($_POST['Estimated'] = $etd, "text"),
GetSQLValueString($_POST['Dest_location'] = $dest_location, "text"),
GetSQLValueString($_POST['Dest_crs']= $crsName, "text"),
GetSQLValueString($_POST['Operator'] = $operator, "text"),
GetSQLValueString($_POST['Message'] = $message, "text"),
GetSQLValueString($_POST['IsCancelled'] = $isCancelled, "text"),
GetSQLValueString($_POST['CancelReason'] = $cancelReason, "text"),
GetSQLValueString($_POST['DelayedReason'] = $delayReason, "text"),
GetSQLValueString($_POST['DeleteCode'] = $DeleteCode, "text"),
GetSQLValueString($_POST['NoCarrages'] = $length, "text"),
GetSQLValueString($_POST['Via'] = $via, "text"),
GetSQLValueString($_POST['DeleteTime'] = $DeleteTime, "text"));
mysql_select_db($database_ex, $ex);
$Result1 = mysql_query($insertSQL, $ex) or die(mysql_error());
}
}
}
Many thanks in advance for your time.
5was just missing ... but you have a different value there in your second array, so you would have to check the values and determine their meaning first. Assuming that you know the possible keys/property names beforehand, this would be a lot easier if you didn’t convert the objects to arrays in the first place - then sth. likeisset($obj->length)will help you determine whether the current object has that property or not ...array_search()function search an array for a value and returns the key. eg:array_search('length',$arrayname);.