php - Combine 2 arrays into 2d array without losing data -
i have 2 arrays , want combine third array 1 array key , value. tried use array_combine(), function eliminate repeated keys, want result array 2d array. sample array below:
$keys = {0,1,2,0,1,2,0,1,2}; $values = {a,b,c,d,e,f,g,h,i}; $result = array( [0]=>array(0=>a,1=>b,2=>c), [1]=>array(0=>d,1=>e,2=>f), [2]=>array(0=>g,1=>h,2=>i) ); //what using right is: $result = array_combine($keys,$values);
but returns array(0=>g,2=>h,3=>i). advice appreciated!
you can below:-
<?php $keys = array(0,1,2,0,1,2,0,1,2); $values = array('a','b','c','d','e','f','g','h','i'); $values = array_chunk($values,count(array_unique($keys))); foreach($values &$value){ $value = array_combine(array_unique($keys),$value); } print_r($values);
Comments
Post a Comment