5

这是查询字符串。

$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') );

我怎样才能创建上面的数组?我确实知道如何定义多维数组,但遗憾的是我很难动态地创建这个复杂的数组。作为一个初学者,我什至不知道从哪里开始。

4

3 回答 3

0

这是一个可能对您有所帮助的示例。尝试从简单的多维数组开始,一旦掌握了它,就可以开始构建复杂的数组。然后你会发现你想要构建的数组并不像你最初想象的那么难。

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

于 2013-09-14T05:31:10.243 回答
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'],

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

于 2013-09-14T06:04:02.833 回答
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']数组。

于 2013-09-14T06:10:09.847 回答