tate_etc Posted January 8, 2008 Share Posted January 8, 2008 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! Quote Link to comment https://forums.phpfreaks.com/topic/84947-how-to-turn-a-string-into-an-array/ Share on other sites More sharing options...
fert Posted January 8, 2008 Share Posted January 8, 2008 foreach($_POST as $p=>$k) { $b=explode("_",$k); $request[$b[0]][$b[1]][$b[2]]=$p; } that should work Quote Link to comment https://forums.phpfreaks.com/topic/84947-how-to-turn-a-string-into-an-array/#findComment-433159 Share on other sites More sharing options...
btherl Posted January 8, 2008 Share Posted January 8, 2008 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); Quote Link to comment https://forums.phpfreaks.com/topic/84947-how-to-turn-a-string-into-an-array/#findComment-433160 Share on other sites More sharing options...
tate_etc Posted January 8, 2008 Author Share Posted January 8, 2008 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'] = ""; Quote Link to comment https://forums.phpfreaks.com/topic/84947-how-to-turn-a-string-into-an-array/#findComment-433168 Share on other sites More sharing options...
fert Posted January 8, 2008 Share Posted January 8, 2008 why don't you separate fields? Quote Link to comment https://forums.phpfreaks.com/topic/84947-how-to-turn-a-string-into-an-array/#findComment-433170 Share on other sites More sharing options...
tate_etc Posted January 8, 2008 Author Share Posted January 8, 2008 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' => '', ) Quote Link to comment https://forums.phpfreaks.com/topic/84947-how-to-turn-a-string-into-an-array/#findComment-433176 Share on other sites More sharing options...
Ken2k7 Posted January 8, 2008 Share Posted January 8, 2008 What's with the long ass names!? ??? Do people just enjoy typing? Quote Link to comment https://forums.phpfreaks.com/topic/84947-how-to-turn-a-string-into-an-array/#findComment-433198 Share on other sites More sharing options...
kenrbnsn Posted January 8, 2008 Share Posted January 8, 2008 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 Quote Link to comment https://forums.phpfreaks.com/topic/84947-how-to-turn-a-string-into-an-array/#findComment-433201 Share on other sites More sharing options...
Recommended Posts
Join the conversation
You can post now and register later. If you have an account, sign in now to post with your account.