mardi 30 septembre 2014

haw to convert index array to associative array?


Vote count:

0




I have an array like that



$products = array(array(354),array(1),array(375),array(1),array(344),array(2));


and i want to achieve array like that



$arrProducts= array(array('product_id'=>354,'qty'=>1),array('product_id'=>375,'qty'=>1),array('product_id'=>344,'qty'=>2));


I achieved this array using this code



foreach($products as $val)
{
$abc[] =$val[0];

}

for($i=0;$i<count($abc);$i++)
{
if($i%2==0)
{
$newarr[]['product_id'] = $abc[$i];
}
else{
$newarr[]['qty'] = $abc[$i];
}
}


for($j=0;$j<count($newarr);$j++)
{
if($j%2==0)
{
$arrProducts[] = array_merge($newarr[$j],$newarr[$j+1]);
}
else{
continue;
}
}

echo '<pre>';
print_r($arrProducts);


but i think my way to get this array is too long so how can i get this array in short way using some array functions or should i use this code?



asked 38 secs ago







haw to convert index array to associative array?

Aucun commentaire:

Enregistrer un commentaire