Php 仅将某些数组键合并并分组到新数组中
我有一个Php 仅将某些数组键合并并分组到新数组中,php,arrays,Php,Arrays,我有一个foreach循环,里面有一个if语句,它根据字符串包含的内容创建数组 foreach ($dataArray2 as $item) { $items = []; if(strpos($item, 'your date is') !== false) { //Converting 22052018 to 2018-05-22 $month_words = ["may"]; $month_numbers = ["0
foreach
循环,里面有一个if
语句,它根据字符串包含的内容创建数组
foreach ($dataArray2 as $item) {
$items = [];
if(strpos($item, 'your date is') !== false)
{
//Converting 22052018 to 2018-05-22
$month_words = ["may"];
$month_numbers = ["05"];
$item = str_replace($month_words, $month_numbers, $item);
$item = preg_replace('/[^0-9]/', '', $item);
$insertion = "-";
$index = 2;
$index2 = 5;
$item = substr_replace($item, $insertion, $index, 0);
$item = substr_replace($item, $insertion, $index2, 0);
$time = strtotime($item);
$date = date('Y-m-d', $time);
$items['date']= $date;
} else if (strpos($item, 'hat') !== false) {
$item = str_replace('.', '', $item);
$items['hat'] = $item;
} else {
$items['tshirt'] = $item;
}
$dataArr[] = array_filter($items);
}
echo '<pre>';
print_r($dataArr);
echo '</pre>';
我需要它的样子:
array (
0 => 'your date is 22 may 2018, Tuesday.',
1 => 'hat: blue.',
2 => 'tshirt: white',
3 => 'hat: black.',
4 => 'tshirt: cyan',
5 => 'your date is 21 may 2018, Tuesday.',
6 => 'hat: red.',
7 => 'tshirt: blue',
编辑:var\u导出
输入数组的格式:
Array
(
[0] => Array
(
[date] => 2018-05-22
[content] => Array
(
[0] => Array
(
[0] => blue
[1] => white
)
[1] => Array
(
[0] => black
[1] => cyan
)
[2] => Array
(
[0] => red
[1] => blue
)
)
)
[1] => Array
(
[date] => 2018-05-21
[content] => Array
(
[0] => Array
(
[0] => blue
[1] => white
)
编辑#2:如何将content
中的数组分解为x2个块,如下所示:
$arr = array (
0 => 'your date is 22 may 2018, Tuesday.',
1 => 'hat: blue.',
2 => 'tshirt: white',
3 => 'hat: black.',
4 => 'tshirt: cyan',
5 => 'your date is 21 may 2018, Monday.',
6 => 'hat: red.',
7 => 'tshirt: blue',
);
$i =-1;
foreach($arr as $item){
if(strpos($item, 'your date is') !== false){
$i++;
$res[$i]['date'] = date("Y-m-d", strtotime(str_replace("your date is ", "", $item)));
}else{
list($key, $val) = explode(": ", $item);
$res[$i][$key][] = rtrim($val, ".");
}
}
var_dump($res);
此方法可以创建一个数组。
我不使用正则表达式来解析日期,而是使用较轻的日期() 因为T恤和帽子是“相同”的,所以我使用相同的方法来解析它们
array(2) {
[0]=>
array(3) {
["date"]=>
string(10) "2018-05-22"
["hat"]=>
array(2) {
[0]=>
string(4) "blue"
[1]=>
string(5) "black"
}
["tshirt"]=>
array(2) {
[0]=>
string(5) "white"
[1]=>
string(4) "cyan"
}
}
[1]=>
array(3) {
["date"]=>
string(10) "2018-05-21"
["hat"]=>
array(1) {
[0]=>
string(3) "red"
}
["tshirt"]=>
array(1) {
[0]=>
string(4) "blue"
}
}
}
输出:
编辑我也更正了你的日期。5月21日和22日都不可能是周二。告诉我们输入数组也是以var_导出格式或json_编码的。如果在同一个数组中有两个hat和两个tshirt密钥,您的预期结果是不可能的。我远不是专家。我已经对我需要的数组结构做了一些编辑,并添加了
var\u export
output。但是,var\u export不是输入,是吗?输入数组来自XPath请求,我想它不是发布大型脚本的正确位置?我的意思是,你可以清楚地看到,我得到了我需要的结果,我只需要重新组织它们。你能解释一下这些信息是如何帮助任何人了解情况的吗?还是我遗漏了什么?使用$I
增量的酷把戏。您能否建议如何进一步分解数组,以便在内容
中使用hat
和tshirt
创建一个新数组?因此,如果我总共有24个项目,那么输出将是内容中的12个数组,其中包含hat
和tshirt
?谢谢,谢谢!我不太明白你说的24项是什么意思。你能举个例子吗?编辑问题:)你的意思是这样吗?在你的例子中,你不再有帽子和t恤了,我想这是个错误?这不是我的意思,但无论如何,我已经用很多if
s和像你这样的递增技巧解决了这个问题。再次感谢。
$arr = array (
0 => 'your date is 22 may 2018, Tuesday.',
1 => 'hat: blue.',
2 => 'tshirt: white',
3 => 'hat: black.',
4 => 'tshirt: cyan',
5 => 'your date is 21 may 2018, Monday.',
6 => 'hat: red.',
7 => 'tshirt: blue',
);
$i =-1;
foreach($arr as $item){
if(strpos($item, 'your date is') !== false){
$i++;
$res[$i]['date'] = date("Y-m-d", strtotime(str_replace("your date is ", "", $item)));
}else{
list($key, $val) = explode(": ", $item);
$res[$i][$key][] = rtrim($val, ".");
}
}
var_dump($res);
array(2) {
[0]=>
array(3) {
["date"]=>
string(10) "2018-05-22"
["hat"]=>
array(2) {
[0]=>
string(4) "blue"
[1]=>
string(5) "black"
}
["tshirt"]=>
array(2) {
[0]=>
string(5) "white"
[1]=>
string(4) "cyan"
}
}
[1]=>
array(3) {
["date"]=>
string(10) "2018-05-21"
["hat"]=>
array(1) {
[0]=>
string(3) "red"
}
["tshirt"]=>
array(1) {
[0]=>
string(4) "blue"
}
}
}