gpt4 book ai didi

PHP数组技巧

转载 作者:行者123 更新时间:2023-11-30 22:27:35 24 4
gpt4 key购买 nike

我想构建一个特定的数组

我从 MySQL 查询中获取这些数组

$result = $db->prepare($sql);
$result->execute(array());
foreach($result as $row)
{
$test[$i]['case'] = $row[0];
$test[$i]['name'] = $row[1];
$test[$i]['task'] = $row[2];
$test[$i]['current'] = $row[3];
}

echo var_dump($test)

array (size=32)
0 =>
array (size=4)
'case' => string '174' (length=3)
'name' => string 'Bone Tumor' (length=10)
'task' => string '201006' (length=6)
'current' => string '0' (length=1)
1 =>
array (size=4)
'case' => string '174' (length=3)
'name' => string 'Bone Tumor' (length=10)
'task' => string '207001' (length=6)
'current' => string '0' (length=1)
2 =>
array (size=4)
'case' => string '174' (length=3)
'name' => string 'Bone Tumor' (length=10)
'task' => string '205003' (length=6)
'current' => string '0' (length=1)
3 =>
array (size=4)
'case' => string '174' (length=3)
'name' => string 'Bone Tumor' (length=10)
'task' => string '209002' (length=6)
'current' => string '1' (length=1)

我想重新组合案例和名称(总是以 4 为单位)并以此结束

array ()
0 =>
array ()
'case' => string '174' (length=3)
'name' => string 'Bone Tumor' (length=10)
array ()
'task1' => string '201006' (length=6)
't1_current' => string '0' (length=1)
'task2' => string '207001' (length=6)
't2_current' => string '0' (length=1)
'task3' => string '205003' (length=6)
't3_current' => string '0' (length=1)
'task4' => string '209002' (length=6)
't4_current' => string '1' (length=1)

1 =>
array ()
'case' => string '182' (length=3)
'name' => string 'Cranioplasty' (length=10)
array ()
'task1' => string '201006' (length=6)
't1_current' => string '0' (length=1)
'task2' => string '207001' (length=6)
't2_current' => string '0' (length=1)
'task3' => string '205003' (length=6)
't3_current' => string '0' (length=1)
'task4' => string '209002' (length=6)
't4_current' => string '1' (length=1)

我是否应该更改我的初始方式以直接从 SQL 获取查询?

最佳答案

有两种选择。

首先使用循环

foreach($result as $row)
{

// skip duplicates
$flag = false;

foreach($test as $row1)
{
if($row[0] == $row1['case'])
{
$flag = true;
break;
}
}

if($flag)
continue;

$test[$i]['case'] = $row[0];
$test[$i]['name'] = $row[1];


// Fill tasks

$test[$i]['tasks'] = array();

foreach($result as $row1)
{
if($test[$i]['case'] == $row1[0])
{
$test[$i]['tasks'][] = array('task' => $row1[2], 'current' => $row1[3]);
}
}
}

第二个选项是执行 SQL 查询以获取所有案例的列表。然后对每个案例执行查询以获取所有关联的任务。假设您有一个包含列的 Cases

id | case | name | task | current 

下面是代码的样子

$sql = 'SELECT c.case, c.name FROM Cases c GROUP BY c.case;';

$result = $db->prepare($sql);
$result->execute(array());

foreach($result as $row)
{
$sql = 'SELECT c.task, c.current FROM Cases c WHERE c.case = '.$row[0].';';

$tasks = $db->prepare($sql);
$tasks->execute(array());

$test[] = array('case' => $row[0],
'name' => $row[1],
'tasks' => $tasks);
}

关于PHP数组技巧,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/34765092/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com