php - Combine 2 arrays into a third array with inconstant unique keys from one array -


i have 2 arrays below.

$keys = [1,2,3,4-1,99,1,2,3,4-1,4-2,4-3,99,1,2,3,4-1,4-2,99] $values = [a,b,c,d,x,a1,b1,c1,d1,e,g,x,a2,b2,c2,d2,e,x] 

i want combine array like:

$result = array(   [0]=>array(1=>a,2=>b,3=>c,4-1=>d,99=>x),   [1]=>array(1=>a1,2=>b1,3=>c1,4-1=>d1,4-2=>e,4-3=>g,99=>x),   [2]=>array(1=>a2,2=>b2,3=>c2,4-1=>d2,4-2=>e,99=>x ); 

the rule break anytime $key=99. currently, tried use array_chunk syntax allows me chunk array number of unique keys, not constant in example. advice?

you need write custom script combines these 2 arrays logic.

you need fetch each key $keys array , combine same element position $values array.

example:

<?php $keys = ['1', '2', '3', '4-1', '99', '1', '2', '3', '4-1', '4-2', '4-3', '99', '1', '2', '3', '4-1', '4-2', '99']; $values = ['a', 'b', 'c', 'd', 'x', 'a1', 'b1', 'c1', 'd1', 'e', 'g', 'x', 'a2', 'b2', 'c2', 'd2', 'e', 'x'];  $i = 0; $result = []; foreach ($keys $index => $key) {     if (empty($result[$i]))         $result[$i] = [$key => $values[$index]];     else         $result[$i][$key] = $values[$index];      if ($key == 99)         ++$i; }  print_r($result); 

Comments

Popular posts from this blog

angular - Ionic slides - dynamically add slides before and after -

minify - Minimizing css files -

Add a dynamic header in angular 2 http provider -