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

时间:2021-10-23 21:30:41

Here is the query string.

这是查询字符串。

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

And this is the array i would like the query result to turn into.

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

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

How can i create the above array? I sure know how to define multi dimensional array, regretfully though i am having difficulty creating this complex array dynamically. As a beginner i don't even know where to begin.

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

3 个解决方案

#1


0  

Here is an example that might help you out. Try starting with simple multi dimensional arrays, once you get a hold of it, you can move onto building complex ones. You will then find that the array you want to build is not really difficult than you initially thought it to be.

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

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

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

#2


0  

You could create the array just as you have here. I'm not gonna write the whole thing out, but something like this...

你可以像在这里一样创建数组。我不会写出整件事,但这样的话......

    $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'],

...etc, Hope this makes sense for you.

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

#3


0  

Set up a structure array first that defines which columns will be stored in a sub array like

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

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

In your while ($a=mysqli_fetch_assoc($res)) loop you can now use this structure to decide whether you want to store an element directly in your target array or whether you want to place it in the subarray named in this structure array. Like

在您的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 
}

This is the complete code, no more is needed. It was typed up on my iPod, so it is NOT tested yet.

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

The generated array should be equivalent to your $user['allTask'] array.

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

#1


0  

Here is an example that might help you out. Try starting with simple multi dimensional arrays, once you get a hold of it, you can move onto building complex ones. You will then find that the array you want to build is not really difficult than you initially thought it to be.

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

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

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

#2


0  

You could create the array just as you have here. I'm not gonna write the whole thing out, but something like this...

你可以像在这里一样创建数组。我不会写出整件事,但这样的话......

    $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'],

...etc, Hope this makes sense for you.

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

#3


0  

Set up a structure array first that defines which columns will be stored in a sub array like

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

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

In your while ($a=mysqli_fetch_assoc($res)) loop you can now use this structure to decide whether you want to store an element directly in your target array or whether you want to place it in the subarray named in this structure array. Like

在您的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 
}

This is the complete code, no more is needed. It was typed up on my iPod, so it is NOT tested yet.

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

The generated array should be equivalent to your $user['allTask'] array.

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