0

i have two interesting arrays that im trying to combine together. Simple put:

$firstArr

array(3) {
  [0] => array(2) {
    [0] => string(1) "1"
    [1] => string(16) "test1"
  }
  [1] => array(2) {
    [0] => string(1) "8"
    [1] => string(26) "test2"
  }
  [2] => array(2) {
    [0] => string(1) "9"
    [1] => string(23) "test3"
  }
}


$secondArr

array(3) {
  [0] => string(1) "1"
  [1] => string(1) "2"
  [2] => string(1) "3"
}

what i would like to get is something like this (not arrays):

$x = 1, 8, 8, 9, 9, 9;
$y = test1, test2, test2, test3, test3, test3;

basically the second array values dictates how many times the first array values are duplicated.

any ideas?

4

1 に答える 1

0

$firstArr=array(array("1","test1"),array("8","test2"),array("9","test3"));


$secondArr=array("1","2","3");


$x=$y='';
foreach ($secondArr as $k=>$v){
$x.= str_repeat($firstArr[$k][0].',',$v);
$y.= str_repeat($firstArr[$k][1].',',$v);
}

echo rtrim($x,",").";\n";
echo rtrim($y,",").";";

出力:

1,8,8,9,9,9;
test1、test2、test2、test3、test3、test3;

作業デモ:

http://codepad.org/yFvWs0Rh

于 2012-06-13T23:05:38.630 に答える