我正在尝试从表单输入中获取数据,并且在PHP中,更改它在模板创建模块/应用程序中的使用顺序。我需要它在一个多维数组中使用。
//How the array currently looks
$templateData = [
0 => 'h',
1 => 1,
2 => 2,
3 => 3,
4 => 'c-1-3',
5 => 3,
6 => 5,
7 => 'c-2-3',
8 => 3,
9 => 'c-3-3',
10 => 'f',
11 => 2
];
//How the array should look
$templateData = [
0 =>[ //header row
0 => [1,2,3] //column
],
1 =>[ //content row(s)
0 => [3,5], //column
1 => [3],
2 => [0]
],
2 => [ //footer row
0 => [2] //column
]
]
'h'表示模板标题行的开头,'c-1-3'表示从第一列开始的3列的行的开头。 'f'代表页脚行的开头。我现在只是画一个空白,我无法绕过它。
这就是我现在所处的位置,但它仍未按预期工作:
$elements = $request->input('elements'); //laravel code that grabs array input
$row = 0;
$column = 0;
foreach($elements as $key => $element) {
//if value is a letter
if(preg_match('/^[a-zA-Z]/', $element)) {
//if the 3rd letter in the string is a 1
if(!isset($element[2]) || $element[2] == '1') { $row++; }
//cycle through values after current key
for($i=$key+1; $i < count($elements);$i++){
//until you hit another letter
if(preg_match('/^[a-zA-Z]/', $elements[$i])) { break; }
$column++;
$temp[$row][$column][] = $elements[$i];
}
$column = 0;
}
}
$ templateData和$ elements变量在上下文中相同。
答案 0 :(得分:1)
这是我最终得到的代码。终于明白了。这有效:
dostuff
答案 1 :(得分:0)
这有点矫枉过正,但你可以拥有全部功能,如果你想改变逻辑,你只需要改变处理程序。希望它能以某种方式提供帮助。
$templateData = [
0 => 'h',
1 => 1,
2 => 2,
3 => 3,
4 => 'c-1-3',
5 => 3,
6 => 5,
7 => 'c-2-3',
8 => 3,
9 => 'f',
10 => 2
];
$tmpKey = '';
$tmpArray = [];
class Test
{
/**
* @var []
*/
private $data = [];
/**
* @var []
*/
private $columns = [];
/**
* @var array
*/
private $numOfColumns = 0;
/**
* @var []
*/
private $header = [];
/**
* @var []
*/
private $footer = [];
/**
* @var string
*/
private $current = [
'key' => '',
'matches' => ''
];
/**
* @var []
*/
private $rules = [
'header' => [
'expression' => 'h',
'handler' => 'headerHandler',
'matches' => false
],
'footer' => [
'expression' => 'f',
'handler' => 'footerHandler',
'matches' => false
],
'columns' => [
'expression' => 'c-\d+-\d+',
'handler' => 'columnHandler',
'matches' => "columnMatches"
]
];
/**
* @param $data
*/
public function __construct($data)
{
$this->data = $data;
$this->process();
}
/**
* @throws Exception
*/
private function process()
{
foreach($this->data as $data) {
if (!is_int($data)) {
if (!$this->setRule($data)) {
throw new \RuntimeException("Rule Not found.");
}
} else {
$this->handleRule($data);
}
}
}
/**
* @param $data
*/
private function headerHandler($data)
{
$this->header[] = $data;
}
/**
* @param $data
*/
private function footerHandler($data)
{
$this->footer[] = $data;
}
/**
* @param $data
*/
private function columnHandler($data)
{
$columns = explode('-', $this->current['matches']);
if ($this->columns[($columns[1] - 1)][0] == 0){
$this->columns[($columns[1] - 1)][0] = $data;
} else {
array_push($this->columns[($columns[1] - 1)], $data);
}
}
/**
* @param $data
*/
private function columnMatches($data)
{
$columns = explode('-', $data);
if ($this->numOfColumns == 0) {
$this->numOfColumns = end($columns);
$this->columns = array_fill(0, $this->numOfColumns, [0]);
}
}
/**
* @param $data
* @return bool
*/
private function setRule($data)
{
foreach($this->rules as $key => $rules) {
if (preg_match("/^{$rules['expression']}$/", $data, $matches)) {
if ($rules['matches']) {
$this->{$rules['matches']}($data);
}
$this->current['key'] = $key;
$this->current['matches'] = $data;
return true;
}
}
return false;
}
/**
* @return mixed
*/
public function getHeader()
{
return $this->header;
}
/**
* @return mixed
*/
public function getColumns()
{
return $this->columns;
}
/**
* @return mixed
*/
public function getFooter()
{
return $this->footer;
}
/**
* @return array
*/
public function getResult()
{
return [
0 => [$this->header],
1 => $this->columns,
2 => $this->footer
];
}
/**
* @param $data
*/
private function handleRule($data)
{
$this->{$this->rules[$this->current['key']]['handler']}($data);
}
}
$test = new Test($templateData);
echo '<h1>Header</h1>';
var_dump($test->getHeader());
echo '<h1>Footer</h1>';
var_dump($test->getFooter());
echo '<h1>Columns</h1>';
var_dump($test->getColumns());
echo '<h1>Result</h1>';
var_dump($test->getResult());exit;
Ideone:Example Here