我有一个原始数据数组如下所示
$data = [
[
"content" => "Niche description 1",
"type" => "niche_des"
],
[
"content" => "Niche description 2",
"type" => "niche_des"
],
[
"content" => "Niche description 3",
"type" => "niche_des"
],
[
"content" => "Product description 1",
"type" => "product_des"
],
[
"content" => "Product description 2",
"type" => "product_des"
],
[
"content" => "Product description 3",
"type" => "product_des"
],
[
"content" => "Trust site description 1",
"type" => "trust_site"
],
[
"content" => "Trust site description 2",
"type" => "trust_site"
],
[
"content" => "Trust site description 3",
"type" => "trust_site"
]
];我试着把数据放入这样一个新的数组中,但是它没有工作
这就是我想要的结果
$newdata = [
[
"niche_des" => "Niche description 1",
"product_des" => "Product description 1",
"trust_site" => "Trust site description 1"
],
[
"niche_des" => "Niche description 2",
"product_des" => "Product description 2",
"trust_site" => "Trust site description 2"
],
[
"niche_des" => "Niche description 3",
"product_des" => "Product description 3",
"trust_site" => "Trust site description 3"
],
];我试着用这种方式处理
$newdata = [];
foreach ($data as $des) {
if ($des->type == 'niche_des') {
$niche = $des->content;
}
if ($des->type == 'product_des') {
$product_d = $des->content;
}
if ($des->type == 'trust_site') {
$trust_site = $des->content;
}
array_push($newdata, [
'niche_des' => $niche,
'product_des' => $product_d,
'trust_site' => $trust_site
]);
}发布于 2022-06-12 10:04:38
您的代码有两个问题,我可以看到:
->)访问数组。您需要使用数组访问符号(['...'])。因为您在评论中已经说过数据可能并不总是相同的,所以这个两步的过程就是我处理它的方式:
步骤1.按类型拆分原始数组:
$tempdata = [];
foreach ($data as $des) {
if (!isset($tempdata[$des['type']])) {
$tempdata[$des['type']] = [];
}
$tempdata[$des['type']][] = $des['content'];
}$tempdata现在看起来如下所示:
[
"niche_des" => [
"Niche description 1",
"Niche description 2",
"Niche description 3",
],
"product_des" => [
"Product description 1",
"Product description 2",
"Product description 3",
],
"trust_site" => [
"Trust site description 1"
"Trust site description 2"
"Trust site description 3"
],
];步骤2.将上面的拆分数组重新构建成所需的格式。
$newdata = [];
$types = array_keys($tempdata);
$itemcount = count($tempdata[$types[0]]);
for ($i = 0; $i < $itemcount; $i++) {
$item = [];
foreach ($types as $type) {
$item[$type] = $tempdata[$type][$i];
}
$newdata[] = $item;
}重要:上面的解决方案要求以下两个条件为真:
niche_des #1匹配product_des #1和trust_site #1)。发布于 2022-06-13 21:54:51
您可以使用许多技术来隔离每个content列值的尾随整数(我将演示ltrim()、ing字母和空格...and,ascii表上的六个无关符号)。对该整数分组,将所需数据推入每个组,然后在循环完成后,通过调用array_values()删除临时分组键。
代码:(演示)
$result = [];
foreach ($data as $row) {
$result[ltrim($row['content'], 'A..z ')][$row['type']] = $row['content'];
}
var_export(array_values($result));输出:
array (
0 =>
array (
'niche_des' => 'Niche description 1',
'product_des' => 'Product description 1',
'trust_site' => 'Trust site description 1',
),
1 =>
array (
'niche_des' => 'Niche description 2',
'product_des' => 'Product description 2',
'trust_site' => 'Trust site description 2',
),
2 =>
array (
'niche_des' => 'Niche description 3',
'product_des' => 'Product description 3',
'trust_site' => 'Trust site description 3',
),
)https://stackoverflow.com/questions/72585636
复制相似问题