Jump to content

how to turn a string into an array


tate_etc

Recommended Posts

i want to convert a string (that is sent via POST):

$_POST['RateRequest_Version_ServiceId'] = 'crs';

 

to an array:

$request = array();

$request['RateRequest']['Version']['ServiceId'] = 'crs';

 

the only way i can think of to make this happen is eval(), but i can't get it to work. does anyone have any suggestions?!

 

thanks in advance!

Link to comment
https://forums.phpfreaks.com/topic/84947-how-to-turn-a-string-into-an-array/
Share on other sites

Here's a non-eval solution:

 

$name = "Request_Version_Blah";
$value = "foo";

function to_array($name_parts, $value) {
  if (empty($name_parts)) return $value;
  $first_name_part = array_shift($name_parts);
  $rest_of_array = to_array($name_parts, $value);
  $return[$first_name_part] = $rest_of_array;
  return $return;
}

$name_parts = explode('_', $name);
$result = to_array($name_parts, $value);
var_dump($result);

what i'm trying to accomplish is a WSDL viewer for SOAP. all of the fields are a part of a tree with the parent element "RateRequest." below is the full tree (i'm using FedEx Web Services as an example). i use the complete tree path to the value (using underscores), since there are a few "AccountNumber" fields in the request. if i'm looking at this the wrong way, please point me in the right direction!

 

array (
  'RateRequest_WebAuthenticationDetail_UserCredential_Key' => '1',
  'RateRequest_WebAuthenticationDetail_UserCredential_Password' => '',
  'RateRequest_ClientDetail_AccountNumber' => '',
  'RateRequest_ClientDetail_MeterNumber' => '',
  'RateRequest_ClientDetail_Localization_LanguageCode' => '',
  'RateRequest_ClientDetail_Localization_LocaleCode' => '',
  'RateRequest_TransactionDetail_CustomerTransactionId' => '',
  'RateRequest_TransactionDetail_Localization_LanguageCode' => '',
  'RateRequest_TransactionDetail_Localization_LocaleCode' => '',
  'RateRequest_Version_ServiceId' => 'crs',
  'RateRequest_Version_Major' => '2',
  'RateRequest_Version_Intermediate' => '0',
  'RateRequest_Version_Minor' => '0',
  'RateRequest_Origin_StreetLines' => '',
  'RateRequest_Origin_City' => '',
  'RateRequest_Origin_StateOrProvinceCode' => '',
  'RateRequest_Origin_PostalCode' => '',
  'RateRequest_Origin_UrbanizationCode' => '',
  'RateRequest_Origin_CountryCode' => '',
  'RateRequest_Destination_StreetLines' => '',
  'RateRequest_Destination_City' => '',
  'RateRequest_Destination_StateOrProvinceCode' => '',
  'RateRequest_Destination_PostalCode' => '',
  'RateRequest_Destination_UrbanizationCode' => '',
  'RateRequest_Destination_CountryCode' => '',
  'RateRequest_CustomsValue_Currency' => '',
  'RateRequest_CurrencyType' => '',
  'RateRequest_Payment_Payor_AccountNumber' => '',
  'RateRequest_Payment_Payor_CountryCode' => '',
  'RateRequest_VariableHandlingChargeDetail_FixedValue_Currency' => '',
  'RateRequest_VariableHandlingChargeDetail_PercentValue' => '',
  'RateRequest_ShipDate' => '',
  'RateRequest_SpecialServicesRequested_CodDetail_CodRecipient_AccountNumber' => '',
  'RateRequest_SpecialServicesRequested_CodDetail_CodRecipient_Tin_Number' => '',
  'RateRequest_SpecialServicesRequested_CodDetail_CodRecipient_Contact_PersonName' => '',
  'RateRequest_SpecialServicesRequested_CodDetail_CodRecipient_Contact_Title' => '',
  'RateRequest_SpecialServicesRequested_CodDetail_CodRecipient_Contact_CompanyName' => '',
  'RateRequest_SpecialServicesRequested_CodDetail_CodRecipient_Contact_Department' => '',
  'RateRequest_SpecialServicesRequested_CodDetail_CodRecipient_Contact_PhoneNumber' => '',
  'RateRequest_SpecialServicesRequested_CodDetail_CodRecipient_Contact_PhoneExtension' => '',
  'RateRequest_SpecialServicesRequested_CodDetail_CodRecipient_Contact_PagerNumber' => '',
  'RateRequest_SpecialServicesRequested_CodDetail_CodRecipient_Contact_FaxNumber' => '',
  'RateRequest_SpecialServicesRequested_CodDetail_CodRecipient_Contact_EMailAddress' => '',
  'RateRequest_SpecialServicesRequested_CodDetail_CodRecipient_Address_StreetLines' => '',
  'RateRequest_SpecialServicesRequested_CodDetail_CodRecipient_Address_City' => '',
  'RateRequest_SpecialServicesRequested_CodDetail_CodRecipient_Address_StateOrProvinceCode' => '',
  'RateRequest_SpecialServicesRequested_CodDetail_CodRecipient_Address_PostalCode' => '',
  'RateRequest_SpecialServicesRequested_CodDetail_CodRecipient_Address_UrbanizationCode' => '',
  'RateRequest_SpecialServicesRequested_CodDetail_CodRecipient_Address_CountryCode' => '',
  'RateRequest_SpecialServicesRequested_CodCollectionAmount_Currency' => '',
  'RateRequest_SpecialServicesRequested_HoldAtLocationDetail_PhoneNumber' => '',
  'RateRequest_SpecialServicesRequested_HoldAtLocationDetail_Address_StreetLines' => '',
  'RateRequest_SpecialServicesRequested_HoldAtLocationDetail_Address_City' => '',
  'RateRequest_SpecialServicesRequested_HoldAtLocationDetail_Address_StateOrProvinceCode' => '',
  'RateRequest_SpecialServicesRequested_HoldAtLocationDetail_Address_PostalCode' => '',
  'RateRequest_SpecialServicesRequested_HoldAtLocationDetail_Address_UrbanizationCode' => '',
  'RateRequest_SpecialServicesRequested_HoldAtLocationDetail_Address_CountryCode' => '',
  'RateRequest_SpecialServicesRequested_DangerousGoodsDetail_HazMatCertificateData_DotProperShippingName' => '',
  'RateRequest_SpecialServicesRequested_DangerousGoodsDetail_HazMatCertificateData_DotHazardClassOrDivision' => '',
  'RateRequest_SpecialServicesRequested_DangerousGoodsDetail_HazMatCertificateData_DotIdNumber' => '',
  'RateRequest_SpecialServicesRequested_DangerousGoodsDetail_HazMatCertificateData_DotLabelType' => '',
  'RateRequest_SpecialServicesRequested_DangerousGoodsDetail_HazMatCertificateData_PackingGroup' => '',
  'RateRequest_SpecialServicesRequested_DangerousGoodsDetail_HazMatCertificateData_Quantity' => '',
  'RateRequest_SpecialServicesRequested_DangerousGoodsDetail_HazMatCertificateData_TwentyFourHourEmergencyResponseContactNumber' => '',
  'RateRequest_SpecialServicesRequested_DangerousGoodsDetail_HazMatCertificateData_TwentyFourHourEmergencyResponseContactName' => '',
  'RateRequest_SpecialServicesRequested_EMailNotificationDetail_PersonalMessage' => '',
  'RateRequest_SpecialServicesRequested_EMailNotificationDetail_Recipients_EMailAddress' => '',
  'RateRequest_SpecialServicesRequested_EMailNotificationDetail_Recipients_Localization_LanguageCode' => '',
  'RateRequest_SpecialServicesRequested_EMailNotificationDetail_Recipients_Localization_LocaleCode' => '',
  'RateRequest_SpecialServicesRequested_ReturnShipmentDetail_Rma_Number' => '',
  'RateRequest_SpecialServicesRequested_ReturnShipmentDetail_Rma_Reason' => '',
  'RateRequest_SpecialServicesRequested_ReturnShipmentDetail_EMailLabelDetail_UrlDetail_ExpirationDate' => '',
  'RateRequest_SpecialServicesRequested_ReturnShipmentDetail_EMailLabelDetail_UrlDetail_NotificationEMailAddress' => '',
  'RateRequest_SpecialServicesRequested_ReturnShipmentDetail_EMailLabelDetail_UrlDetail_NotificationMessage' => '',
  'RateRequest_SpecialServicesRequested_ReturnShipmentDetail_EMailLabelDetail_MerchantPhoneNumber' => '',
  'RateRequest_SpecialServicesRequested_ShipmentDryIceDetail_TotalWeight_Value' => '',
)

Using the array you posted, the following code using eval() works:

<?php
$new_array = array();
foreach($test as $k => $v) {
        $ka = explode('_',$k);
        $tmp = "\$new_array['" . implode("']['",$ka) . "'] = '$v';";
        eval($tmp);
}
echo '<pre>' . print_r($new_array,true) . '</pre>';
?>

 

Ken

 

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.