Jump to content

creating associative array from plain data


prakash

Recommended Posts

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

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
        )

)

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. :)

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?

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, 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?

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';
?>

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.