如何动态地将sql结果转换为多维数组?

时间:2013-09-14 05:22:25

标签: php arrays

以下是查询字符串。

$query = "SELECT t.id, t.assignee, t.owner, 
            d.code, d.status, d.target_completion_date, 
            d.target_extension_date, d.submission_date, d.approval_date,
            d.revision_start_date, d.revision_completion_date, d.message, 
            ty.name, f.orig_name, f.new_name, 
            b.payment_date, b.discount, b.total_cost, b.amount_payed, b.edit_level, 
            b.billing_type, b.pages, b.words
      FROM tasks t
      INNER JOIN details d ON t.detail_id = d.id
      INNER JOIN billing b ON t.billing_id = b.id
      INNER JOIN TYPE ty ON d.document_type_id = ty.id
      INNER JOIN files f ON t.file_id = f.id
      WHERE t.assignee = 'argie1234'";

这是我希望查询结果变成的数组。

$user = array('allTask'=>array(array('taskid' => 1,
                                     'assignee'=>'argie1234',
                     'owner'=>'austral1000',
                     'details' => array( 'code' => 'E',
                     'status'=>'TC',
                     'targetCompletionDateUTC'=>'1379401200',
                     'targetExtentionDateUTC'=>'1379401200',    
                     'submissionDateUTC'=>'1379401200',
                     'approvalDateUTC'=>'1379401200',
                     'revisionStartDateUTC'=>'1379401200',
                     'revisionCompletionDateUTC'=>'1379401200',
                     'messageToEditor'=>'Please work on it asap.',
                     'documentType' => 'Thesis'),
                     'file' => array('orig_name' =>'originalname.docx',
                                     'new_name' => 'newname.docx'),
                         'billing'=>array('paymentDate'=>'July 26,2013 12:40',
                          'discount' => '0',
                           'totalRevisionCharge' => '$20.00',
                          'totalAmountPayed' => '$20.00',
                          'revisionLevel' => '1',
                          'chargeType'=> '1',
                          'numPages' => '60',
                          'numWords' => '120,000' ) ),

                  array('taskid' => 12, 
                                  'assignee'=>'argie1234',
                                  'owner'=>'usaroberto',
                  'details' => array( 'code' => 'E',
                  'status'=>'TC',
                  'targetCompletionDateUTC'=>'1379401200',
                  'targetExtentionDateUTC'=>'1379401200',   
                  'submissionDateUTC'=>'1379401200',
                  'approvalDateUTC'=>'1379401200',
                  'revisionStartDateUTC'=>'1379401200',
                  'revisionCompletionDateUTC'=>'1379401200',
                  'messageToEditor'=>'Please work on it asap.',
                  'documentType' => 'Thesis'),
                  'file' => array('orig_name' => 'originalname.docx',
                                 'new_name' => 'newname.docx'),
                          'billing'=>array('paymentDate'=>'July 26,2013 12:40',
                           'discount' => '0',
                           'totalRevisionCharge' => '$20.00',
                           'totalAmountPayed' => '$20.00',
                           'revisionLevel' => '1',
                           'chargeType'=> '1',
                           'numPages' => '60',
                           'numWords' => '120,000' ) ),

    'account' => array( 'username' => 'marooon55',
            'emailadd' => 'marooon@yahoo.com',
            'firstname' => 'Maroon',
            'initial' => 'E',
            'lastname' => 'Young',
            'country' => 'Australia',
            'gender' => 'M',
            'password' =>'360e2801190744a2af74ef6cbfdb963078b59709',
            'activationDate' => '2013-09-13 14:30:34') );

如何创建上面的数组?我确定知道如何定义多维数组,遗憾的是我无法动态创建这个复杂的数组。作为初学者,我甚至不知道从哪里开始。

3 个答案:

答案 0 :(得分:0)

这是一个可以帮助你的例子。尝试从简单的多维数组开始,一旦掌握了它,就可以开始构建复杂的数组。然后,您将发现您要构建的阵列并不比您最初想象的要困难。

$ mycomplexarray = array('key1'=> array('val1','val2'),                          'key2'=>数组('val3','val4'=>数组('val5','val6')                                    )                   );

答案 1 :(得分:0)

您可以像在此处一样创建阵列。我不会写出整件事,但是这样......

    $result = $mysqli->query($query); // however you query the db is up to you.
    $row = $result->fetch_assoc(); //same as query use your prefered method to fetch
    $user = array('allTask'=>array(array('taskid' => $row['id'],
                                 'assignee'=>$row['assignee'],
                 'owner'=>$row['owner'],
                 'details' => array( 'code' => $row['code'],
                 'status'=>$row['status'],

......等等,希望这对你有意义。

答案 2 :(得分:0)

首先设置一个结构数组,定义哪些列将存储在子数组中,如

$struc=array('Id'->0, 'assignee'->0, 'owner'->0, 
            'code'->'detail', 'status'->'detail', 'target_completion_date'->'detail', 
            'target_extension_date'->'detail', 'submission_date'->'detail', 'approval_date'->'detail',
            'revision_start_date'->'detail', 'revision_completion_date'->'detail', 'message'->'detail', 
            'name'->'file', 'orig_name'->'file', 'new_name'->'file', 
            'payment_date'->'billing', 'discount'->'billing', 'total_cost'->'billing', 'amount_payed'->'billing', 'edit_level'->'billing', 'billing_type'->'billing', 'words');

while ($a=mysqli_fetch_assoc($res))循环中,您现在可以使用此结构来决定是否要将元素直接存储在目标数组中,或者是否要将其放在此结构数组中指定的子数组中。像

$res=mysqli_query($con,$sql);
$arr=array();
while($a=mysqli_fetch_assoc($res)) {
  // within result loop: $a is result from mysqli_fetch_assoc()
  $ta=array(); // temp array ...
  foreach ($a as $k => $v){
    if ($struc[$k]) $ta[struc[$k]][$k]=$v;
    else $ta[$k]=$v;
  }
  $arr[]=$ta;  // add to target array 
}

这是完整的代码,不再需要。它是在我的iPod上输入的,因此尚未经过测试。

生成的数组应该等同于$user['allTask']数组。