[英]how to combine two specific arrays keys and values together, in PHP?
我有兩個有趣的數組,即時通訊試圖結合在一起。 簡單地說:
$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"
}
我想得到的是這樣的東西(不是數組):
$x = 1, 8, 8, 9, 9, 9;
$y = test1, test2, test2, test3, test3, test3;
基本上,第二個數組值決定了第一個數組值被重復多少次。
有任何想法嗎?
$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;
工作演示:
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.