prakash Posted August 10, 2008 Share Posted August 10, 2008 Hi, I have a session data in the format of "1:USA:2,4:UK:2,4:LONDON:2" now I need to build a function so that it can produce an associative array in following format $finalOutput=array( array ( productID=>"1", productCountry=>"USA", deliveryOption=>"2" ), array ( productID=>"4", productCountry=>"UK", deliveryOption=>"2" ), array ( productID=>"4", productCountry=>"LONDON", deliveryOption=>"2" ) ); I have created the function below but I think there is some error on the code and because of that it doesn't output all values. So could anyone help me to solve this or if there is any other easy solution let me know: <?php function extractArray($data) { if (!$data=="") { $explodeData=explode(',',$data); foreach($explodeData as $items) { $itemsArray=explode(':',$items); $finalOutput=array(); if (count($finalOutput)==0) { $finalOutput="array(productID=>$itemsArray[0], productCountry=>$itemsArray[1], deliveryOption=>$itemsArray[2])"; } else { $finalOutput.=", array(productID=>$itemsArray[0], productCountry=>$itemsArray[1], deliveryOption=>$itemsArray[2])"; } } return $finalOutput; } } echo extractArray("1:USA:2,4:UK:2,4:LONDON:2"); ?> Quote Link to comment https://forums.phpfreaks.com/topic/119008-creating-associative-array-from-plain-data/ Share on other sites More sharing options...
Barand Posted August 10, 2008 Share Posted August 10, 2008 try <?php function extractArray ($str) { $a = explode (',', $str); $k=0; $res = array(); foreach ($a as $val) { list($res[$k]['product'], $res[$k]['country'], $res[$k]['delivery']) = explode(':', $val); ++$k; } return $res; } $ar = extractArray ('1:USA:4,2:UK:2,3:EUR:1'); // view results echo '<pre>', print_r($ar, true), '</pre>'; ?> --> Array ( [0] => Array ( [delivery] => 4 [country] => USA [product] => 1 ) [1] => Array ( [delivery] => 2 [country] => UK [product] => 2 ) [2] => Array ( [delivery] => 1 [country] => EUR [product] => 3 ) ) Quote Link to comment https://forums.phpfreaks.com/topic/119008-creating-associative-array-from-plain-data/#findComment-612830 Share on other sites More sharing options...
JasonLewis Posted August 10, 2008 Share Posted August 10, 2008 Here try something like this: <?php $string = "1:USA:2,4:UK:2,4:LONDON:2"; //We need to explode at the comma first. $comma_explode = explode(",", $string); //Declare the output array $final_output = array(); //Now we should loop through each and do the explosions there.... foreach($comma_explode as $tmp_data){ //$tmp_data would hold something like 1:USA:2 //We want this to look something like: array('productID' => 1, 'productCountry' => USA, 'deliveryOption' => 2) //So we need to explode at the colon ( $data = explode(":",$tmp_data); //Now create the new element and array $final_output[] = array("productID" => $data[0], "productCountry" => $data[1], "deliveryOption" => $data[2]); } echo "<pre>"; print_r($final_output); echo "</pre>"; ?> Edit: Barand beat me too it. Quote Link to comment https://forums.phpfreaks.com/topic/119008-creating-associative-array-from-plain-data/#findComment-612832 Share on other sites More sharing options...
prakash Posted August 11, 2008 Author Share Posted August 11, 2008 thanks to both ProjectFear & Barand, and how can I search if any value already exist on productID. suppose I get value "1" from my script and I need to check if that value already exist or not for productID. so how can I do that? Quote Link to comment https://forums.phpfreaks.com/topic/119008-creating-associative-array-from-plain-data/#findComment-613224 Share on other sites More sharing options...
Barand Posted August 11, 2008 Share Posted August 11, 2008 change the array structure so product is the key. Quote Link to comment https://forums.phpfreaks.com/topic/119008-creating-associative-array-from-plain-data/#findComment-613246 Share on other sites More sharing options...
prakash Posted August 11, 2008 Author Share Posted August 11, 2008 change the array structure so product is the key. ok now I have changed the structure and below is the array output Array ( [0] => Array ( 2 => [delivery] USA => [country] 1 => [productID] ) [1] => Array ( 2 => [delivery] UK => [country] 2 => [productID] ) ) Now I got confusion on using array_key_exists. If I need to search if the key "2" for value "productID" how can I do it? Quote Link to comment https://forums.phpfreaks.com/topic/119008-creating-associative-array-from-plain-data/#findComment-613251 Share on other sites More sharing options...
Barand Posted August 11, 2008 Share Posted August 11, 2008 Sorry, I just looked at your example and it wont work as I suggested since product code is not unique $finalOutput=array( array ( productID=>"1", productCountry=>"USA", deliveryOption=>"2" ), array ( productID=>"4", productCountry=>"UK", deliveryOption=>"2" ), array ( productID=>"4", productCountry=>"LONDON", deliveryOption=>"2" ) ); Quote Link to comment https://forums.phpfreaks.com/topic/119008-creating-associative-array-from-plain-data/#findComment-613444 Share on other sites More sharing options...
prakash Posted August 11, 2008 Author Share Posted August 11, 2008 Sorry, I just looked at your example and it wont work as I suggested since product code is not unique $finalOutput=array( array ( productID=>"1", productCountry=>"USA", deliveryOption=>"2" ), array ( productID=>"4", productCountry=>"UK", deliveryOption=>"2" ), array ( productID=>"4", productCountry=>"LONDON", deliveryOption=>"2" ) ); sorry for the example, the productID will be unique. Use below as example: $finalOutput=array( array ( productID=>"1", productCountry=>"USA", deliveryOption=>"2" ), array ( productID=>"2", productCountry=>"UK", deliveryOption=>"2" ), array ( productID=>"3", productCountry=>"LONDON", deliveryOption=>"2" ) ); now how can I search if the value "2" for key "productID" exist or not? Quote Link to comment https://forums.phpfreaks.com/topic/119008-creating-associative-array-from-plain-data/#findComment-613917 Share on other sites More sharing options...
sasa Posted August 11, 2008 Share Posted August 11, 2008 try <?php $data = "1:USA:2,2:UK:2,5:LONDON:2"; foreach (explode(',', $data) as $a) { $b = explode(';',$a); $finalOutput[$a[0]]=array ( 'productID'=>$a[0], 'productCountry'=>$a[1], 'deliveryOption'=>$a[2] ); } if (array_key_exists(2,$finalOutput)) echo 'yes'; else echo 'no'; ?> Quote Link to comment https://forums.phpfreaks.com/topic/119008-creating-associative-array-from-plain-data/#findComment-613946 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.