smilesmita Posted August 9, 2007 Share Posted August 9, 2007 hi there, i have an array which looks like this: Array ( [0_0_0_0_VALUE] => Test XML [0_0_0_0_TAG] => CUSTOMERCONTEXT [0_0_0_1_VALUE] => 1.0007 [0_0_0_1_TAG] => XPCIVERSION [0_0_0_TAG] => TRANSACTIONREFERENCE [0_0_1_VALUE] => 1 [0_0_1_TAG] => RESPONSESTATUSCODE [0_0_2_VALUE] => Success [0_0_2_TAG] => RESPONSESTATUSDESCRIPTION [0_0_TAG] => RESPONSE [0_1_0_VALUE] => smita_b [0_1_0_TAG] => SUBSCRIBERID [0_1_1_0_VALUE] => Alternate Billing [0_1_1_0_TAG] => NAME [0_1_1_1_VALUE] => 82C0E7922B3BA32B [0_1_1_1_TAG] => NUMBER [0_1_1_2_0_VALUE] => A [0_1_1_2_0_TAG] => CODE [0_1_1_2_1_VALUE] => Active [0_1_1_2_1_TAG] => DESCRIPTION [0_1_1_2_TAG] => SUBSCRIPTIONSTATUS [0_1_1_3_0_VALUE] => 070801_151740001 ) I wanted to have some preg_replace pattern which would get me an even output like this i would like to just keep the words "tag " and "value" in the array key field instead of the numbers" Array ( [VALUE] => Test XML [TAG] => CUSTOMERCONTEXT [VALUE] => 1.0007 [TAG] => XPCIVERSION ......so on and so forth... Please help. -smita Quote Link to comment https://forums.phpfreaks.com/topic/64093-solved-preg_replace-for-array-values/ Share on other sites More sharing options...
MadTechie Posted August 9, 2007 Share Posted August 9, 2007 erm.. wouldn't that mess the array up ? as you can only have the key name once! Quote Link to comment https://forums.phpfreaks.com/topic/64093-solved-preg_replace-for-array-values/#findComment-319414 Share on other sites More sharing options...
smilesmita Posted August 9, 2007 Author Share Posted August 9, 2007 ohh..dint realise that..hmm.. actually what i want is..later i wanted to extract and insert in a DB where TAG will go in as columname and VALUE will go in as the value for that columname.. thats the reason i wanted to make it even so that i can do something like this: foreach($return_array AS $field => $type){ { switch ( $v["TAG"] ) { case "SUBSCRITPTIONEVENTS": $wo["wo_SubscriptionEvents"] = $v["VALUE"]; print_r($wo); break; so on an so forth and get these into DB by insert into table respective TAG and respective VALUE of it. any idea? OR yes you are right..i realized that the moment i posted the question. wht if now my array looks like this: array( [#document_1_QuantumViewResponse_0_Response_0_TransactionReference_0_CustomerContext_0] => Test XML [#document_1_QuantumViewResponse_0_Response_0_TransactionReference_1_XpciVersion_0] => 1.0007 [#document_1_QuantumViewResponse_0_Response_1_ResponseStatusCode_0] => 1 [#document_1_QuantumViewResponse_0_Response_2_ResponseStatusDescription_0] => Success [#document_1_QuantumViewResponse_1_QuantumViewEvents_0_SubscriberID_0] => smita_b ) and i want it to look like this: [CustomerContext_0] => Test XML [XpciVersion_0] => 1.0007 [ResponseStatusCode_0] => 1 [ResponseStatusDescription_0] => Success [subscriberID_0] => smita_b Can i do get result like this? Quote Link to comment https://forums.phpfreaks.com/topic/64093-solved-preg_replace-for-array-values/#findComment-319419 Share on other sites More sharing options...
lemmin Posted August 9, 2007 Share Posted August 9, 2007 $pos = strrpos($yourstring, "_"); echo substr($yourstring, $pos+1, strlen($yourstring)-$pos-1); This is pretty sketchy, but it works as long as the keys always have that syntax ("_" right before the name) Quote Link to comment https://forums.phpfreaks.com/topic/64093-solved-preg_replace-for-array-values/#findComment-319433 Share on other sites More sharing options...
MadTechie Posted August 9, 2007 Share Posted August 9, 2007 maybe <?php $theArray = array( '0_0_0_0_VALUE' => "Test XML", '0_0_0_0_TAG' => "CUSTOMERCONTEXT", '0_0_0_1_VALUE' => "1.0007", '0_0_0_1_TAG' => "XPCIVERSION", '0_0_0_TAG' => "TRANSACTIONREFERENCE", '0_0_1_VALUE' => "1", '0_0_1_TAG' => "RESPONSESTATUSCODE", '0_0_2_VALUE' => "Success", '0_0_2_TAG' => "RESPONSESTATUSDESCRIPTION", '0_0_TAG' => "RESPONSE", '0_1_0_VALUE' => "smita_b", '0_1_0_TAG' => "SUBSCRIBERID", '0_1_1_0_VALUE' => "Alternate Billing", '0_1_1_0_TAG' => "NAME", '0_1_1_1_VALUE' => "82C0E7922B3BA32B", '0_1_1_1_TAG' => "NUMBER", '0_1_1_2_0_VALUE' => "A", '0_1_1_2_0_TAG' => "CODE", '0_1_1_2_1_VALUE' => "Active", '0_1_1_2_1_TAG' => "DESCRIPTION", '0_1_1_2_TAG' => "SUBSCRIPTIONSTATUS", '0_1_1_3_0_VALUE' => "070801_1517400012"); $newArray = array(); foreach($theArray as $K => $A) { if (preg_match('/TAG/i', $K)) { $Key = preg_replace('/TAG/i', 'VALUE', $K); $ZKey = $theArray[$K]; $newArray[$ZKey] = $theArray[$Key]; } } echo "<pre>"; print_r($newArray) ?> Array ( [CUSTOMERCONTEXT] => Test XML [XPCIVERSION] => 1.0007 [TRANSACTIONREFERENCE] => [RESPONSESTATUSCODE] => 1 [RESPONSESTATUSDESCRIPTION] => Success [RESPONSE] => [sUBSCRIBERID] => smita_b [NAME] => Alternate Billing [NUMBER] => 82C0E7922B3BA32B [*CODE] => A [DESCRIPTION] => Active [sUBSCRIPTIONSTATUS] => ) Note [*CODE] is without the * Quote Link to comment https://forums.phpfreaks.com/topic/64093-solved-preg_replace-for-array-values/#findComment-319438 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.