PHP数组技巧

时间:2016-01-13 11:09:08

标签: php mysql arrays

我想构建一个特定的数组

我从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获取查询吗?

1 个答案:

答案 0 :(得分:0)

有两种选择。

首先使用循环

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查询以获取所有案例的列表。然后对每个案例执行查询以获取所有相关任务。假设您有一个表个案

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);
}