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
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);

Link to comment
Share on other sites

thanks for the quick response!

 

i should've mentioned that the array will have a variety of levels like this:

 

$_POST['RateRequest_ShipDate'] = "20080107";

$_POST['RateRequest_SpecialServicesRequested_CodDetail_CodRecipient_AccountNumber'] = "";

Link to comment
Share on other sites

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' => '',
)

Link to comment
Share on other sites

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

 

Link to comment
Share on other sites

This thread is more than a year old. Please don't revive it unless you have something important to add.

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.

Guest
Reply to this topic...

×   Pasted as rich text.   Restore formatting

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

×
×
  • 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.