簡體   English   中英

使用PHP從3個數組構建一個數組

[英]Build an array from 3 arrays with PHP

我在構造數組時遇到了一些問題。

數組A:

Array
(
    [0] => 2015-09-13
    [1] => 2015-09-14
    [2] => 2015-09-15
    [3] => 2015-09-16
    [4] => 2015-09-17
    [5] => 2015-09-18
    [6] => 2015-09-19
)

數組B:

Array
(
    [0] => 1
    [1] => 8
)

數組C:

Array
(
    [0] => Leaves-19
    [1] => Shifts-18
    [2] => Shifts-18
    [3] => Shifts-18
    [4] => Shifts-18
    [5] => Shifts-18
    [6] => Leaves-19
    [7] => Leaves-19
    [8] => Shifts-12
    [9] => Shifts-12
    [10] => Shifts-12
    [11] => Shifts-12
    [12] => Shifts-12
    [13] => Leaves-19
)

期望的最終輸出:

Array
(
    [0] => 2015-09-13|1|Leaves-19
    [1] => 2015-09-14|1|Shifts-18
    [2] => 2015-09-15|1|Shifts-18
    [3] => 2015-09-16|1|Shifts-18
    [4] => 2015-09-17|1|Shifts-18
    [5] => 2015-09-18|1|Shifts-18
    [6] => 2015-09-19|1|Leaves-19
    [7] => 2015-09-13|8|Leaves-19
    [8] => 2015-09-14|8|Shifts-12
    [9] => 2015-09-15|8|Shifts-12
    [10] => 2015-09-16|8|Shifts-12
    [11] => 2015-09-17|8|Shifts-12
    [12] => 2015-09-18|8|Shifts-12
    [13] => 2015-09-19|8|Leaves-19
)

我迷失了for並且foreach

這是邏輯:

  • 1st parameter是一個日期,它來自array B 6個條目后重復。
  • 2nd parameter是用戶ID。 它在6個條目后發生變化並傳遞給下一個id。
  • 3rd parameterarray B的條目。

Oter信息:

  • 陣列的長度不同。
  • Array A ,計數6個條目。
  • Array B ,計算隨機條目。
  • Array C ,是Array A x 2。

我已經嘗試為我的array Barray A的foreach做一個for ,但是它不起作用。

我不知道我需要從哪里開始。

希望我能得到任何幫助或暗示。

非常感謝。

你可以使用模數運算符

$OutputArray = Array();

for($i=0; $i < max(count($a1),count($a2),count($a3)); $i++){
  array_push($OutputArray, $a1[ $i % count($a1) ] . "|" . 
             $a2[ $i % count($a2) ] . "|" . $a3[ $i % count($a3) ]);
}

print_r($OutputArray);

你得到:

Array
(
    [0] => 2015-09-13|1|Leaves-19
    [1] => 2015-09-14|8|Shifts-18
    [2] => 2015-09-15|1|Shifts-18
    [3] => 2015-09-16|8|Shifts-18
    [4] => 2015-09-17|1|Shifts-18
    [5] => 2015-09-18|8|Shifts-18
    [6] => 2015-09-19|1|Leaves-19
    [7] => 2015-09-13|8|Leaves-19
    [8] => 2015-09-14|1|Shifts-12
    [9] => 2015-09-15|8|Shifts-12
    [10] => 2015-09-16|1|Shifts-12
    [11] => 2015-09-17|8|Shifts-12
    [12] => 2015-09-18|1|Shifts-12
    [13] => 2015-09-19|8|Leaves-19
)

如果你想按順序(預期):

$OutputArray = Array();

$max = max(count($a1),count($a2),count($a3));
for($i=0; $i < $max; $i++){
  array_push($OutputArray, $a1[$i%count($a1)] . "|" . 
             $a2[ $i*count($a2) / $max ] . "|" . $a3[$i%count($a3)]);
}

print_r($OutputArray);

你得到:

Array
(
    [0] => 2015-09-13|1|Leaves-19
    [1] => 2015-09-14|1|Shifts-18
    [2] => 2015-09-15|1|Shifts-18
    [3] => 2015-09-16|1|Shifts-18
    [4] => 2015-09-17|1|Shifts-18
    [5] => 2015-09-18|1|Shifts-18
    [6] => 2015-09-19|1|Leaves-19
    [7] => 2015-09-13|8|Leaves-19
    [8] => 2015-09-14|8|Shifts-12
    [9] => 2015-09-15|8|Shifts-12
    [10] => 2015-09-16|8|Shifts-12
    [11] => 2015-09-17|8|Shifts-12
    [12] => 2015-09-18|8|Shifts-12
    [13] => 2015-09-19|8|Leaves-19
)

試試這個,它遍歷最大的數組,並將次要數據的計數條件設置為所需的列表行為。

<?php

$arrA = ['2015-09-13','2015-09-14','2015-09-15','2015-09-16',
        '2015-09-17','2015-09-18','2015-09-19'];
$arrB = [1,8];
$arrC = ['Leaves-19','Shifts-18','Shifts-18','Shifts-18','Shifts-18','Shifts-18',
'Leaves-19','Leaves-19','Shifts-12','Shifts-12','Shifts-12','Shifts-12','Shifts-12',
'Leaves-19'];
$a = $b = 0;

for ($c = 0; $c < count($arrC); $c++) {
    $arrC[$c] = $arrA[$a].'|'.$arrB[$b].'|'.$arrC[$c];
    $a++;
    if ($a == count($arrA)) {
        $a = 0;
        $b++;
    }
};

echo "<pre>";
print_r($arrC);

OUTPUT:

Array
(
    [0] => 2015-09-13|1|Leaves-19
    [1] => 2015-09-14|1|Shifts-18
    [2] => 2015-09-15|1|Shifts-18
    [3] => 2015-09-16|1|Shifts-18
    [4] => 2015-09-17|1|Shifts-18
    [5] => 2015-09-18|1|Shifts-18
    [6] => 2015-09-19|1|Leaves-19
    [7] => 2015-09-13|8|Leaves-19
    [8] => 2015-09-14|8|Shifts-12
    [9] => 2015-09-15|8|Shifts-12
    [10] => 2015-09-16|8|Shifts-12
    [11] => 2015-09-17|8|Shifts-12
    [12] => 2015-09-18|8|Shifts-12
    [13] => 2015-09-19|8|Leaves-19
)

試試這段代碼,我將你的3個數組命名為$arrA$arrB$arrC

$arrA = Array(
  '2015-09-13',
  '2015-09-14',
  '2015-09-15',
  '2015-09-16',
  '2015-09-17',
  '2015-09-18',
  '2015-09-19'
);
$arrB = array(1,8);
$arrC  = array(
  'Leaves-19',
  'Shifts-18',
  'Shifts-18',
  'Shifts-18',
  'Shifts-18',
  'Shifts-18',
  'Leaves-19',
  'Leaves-19',
  'Shifts-12',
  'Shifts-12',
  'Shifts-12',
  'Shifts-12',
  'Shifts-12',
  'Leaves-19'
);

$output = array();
$indx = 0;
foreach($arrB as $b){
    foreach($arrA as $a){
        $output[] = $a.'|'.$b.'|'.$arrC[$indx];
        $indx++;/*new line addded*/
    }
    //$indx++;/*remove this line from here*/
}

echo "<pre>";
print_r($output);
echo "</pre>";

我的版本:

$arrayA = array('2015-09-13','2015-09-14','2015-09-15','2015-09-16','2015-09-17','2015-09-18','2015-09-19');
$arrayB = array(1,8);
$arrayC = array('Leaves-19','Leaves-18','Leaves-17','Leaves-16','Leaves-15','Leaves-14','Leaves-13','Leaves-12','Leaves-11','Leaves-10','Leaves-9','Leaves-8','Leaves-7','leaves-6');
$output = array();
$count = 0;

foreach($arrayB as $arrayBElement){
    foreach($arrayA as $arrayAElement){
        $output[] = $arrayC[$count] . '|' . $arrayAElement . '|' . $arrayBElement;
        $count++;
    }
}

var_dump($output);

E:刪除了錯誤的計數

這是一個家庭作業的問題,所以我將以偽代碼回答,所以希望你可以應用一些提示並自己解決問題。

似乎可能是陣列B( [1,8] )是一個索引列表,可以從中開始打印下一個索引。 你自己的陳述( 2nd parameter is the user id. It changes after 6 entries and pass to the next id. )根據你想要的最終輸出不准確:

[6] => 2015-09-19|1|Leaves-19
[7] => 2015-09-13|8|Leaves-19

這將轉移到索引7(計數8)的下一個id而不是索引6(計數7或6個條目之后的一個)。

所以我的偽解決方案是這樣的:

  • 通過重復Array A創建一個新數組,直到它與Array C的長度匹配
  • 通過查找等於或小於(1+index)的數組B的元素將數組B附加到數組A:

    0 => '1', 1 => '1' ... 7 => '8'

  • 將數組C連接到具有索引奇偶校驗的每個數組(例如newArray[ 0 ] . ArrayC[ 0 ]

這為您提供了所需的最終輸出。

使用不同的大小嘗試猜測maxlength

//Create arrays
var Adates= ["2015-09-13", "2015-09-14", "2015-09-15"];
var Bnums= ["1", "2", "3"];
var Cnums= ["Leaves-19", "Shifts-18", "Shifts-18"];

//get size
var maxLenght=0;
if(Adates.length>maxLenght) maxLenght=Adates.length;
if(Bnums.length>maxLenght) maxLenght=Bnums.length;
if(Cnums.length>maxLenght) maxLenght=Cnums.length;

//merge and populate
var OutputArray;
for (i = 0; i < maxLenght; i++) {
      OutputArray[i]=Adates[i]+"|"+Bnums[i]+"|"+Cnums[i];
}

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM