从php中获取数组的结果

时间:2016-01-28 16:42:17

标签: php mysql arrays

我有一个这样的数组,它是数据库选择操作的结果。

Array
(
    [0] => Array
        (
            [id] => 1
            [name] => shamonsha
            [username] => sham1s670
            [password] => 5hYZJPu3s/oAE7KbFfgsvQfX4/GCvnK1MpR0bIbWWxroUgLb+E3M7/AFyJNcZ9WirkAGCe6JBofh54dOPENdrg==
            [email] => shamonsha665@gmail.com
            [mobile_number] => 7736527089
            [address] => hk
            [date] => 24 Dec 2015 01:32:53
            [active] => 1
            [commission] => 25
            [agent_id] => 1
            [ticket_number] => ETS263B0PT10469
            [refund_amount] => 700
            [date_of_cancel] => 28-Jan-2016
        )

    [1] => Array
        (
            [id] => 2
            [name] => shamonsha
            [username] => sham1s670
            [password] => 5hYZJPu3s/oAE7KbFfgsvQfX4/GCvnK1MpR0bIbWWxroUgLb+E3M7/AFyJNcZ9WirkAGCe6JBofh54dOPENdrg==
            [email] => shamonsha665@gmail.com
            [mobile_number] => 7736527089
            [address] => hk
            [date] => 24 Dec 2015 01:32:53
            [active] => 1
            [commission] => 25
            [agent_id] => 1
            [ticket_number] => ETS311B0PT10470
            [refund_amount] => 700
            [date_of_cancel] => 28-Jan-2016
        )

    [2] => Array
        (
            [id] => 3
            [name] => shamonsha
            [username] => sham1s670
            [password] => 5hYZJPu3s/oAE7KbFfgsvQfX4/GCvnK1MpR0bIbWWxroUgLb+E3M7/AFyJNcZ9WirkAGCe6JBofh54dOPENdrg==
            [email] => shamonsha665@gmail.com
            [mobile_number] => 7736527089
            [address] => hk
            [date] => 24 Dec 2015 01:32:53
            [active] => 1
            [commission] => 25
            [agent_id] => 1
            [ticket_number] => ETS788B0PT10472
            [refund_amount] => 395
            [date_of_cancel] => 28-Jan-2016
        )

    [3] => Array
        (
            [id] => 4
            [name] => shamonsha
            [username] => sham1s670
            [password] => 5hYZJPu3s/oAE7KbFfgsvQfX4/GCvnK1MpR0bIbWWxroUgLb+E3M7/AFyJNcZ9WirkAGCe6JBofh54dOPENdrg==
            [email] => shamonsha665@gmail.com
            [mobile_number] => 7736527089
            [address] => hk
            [date] => 24 Dec 2015 01:32:53
            [active] => 1
            [commission] => 25
            [agent_id] => 1
            [ticket_number] => ETS562B0PT10471
            [refund_amount] => 395
            [date_of_cancel] => 28-Jan-2016
        )

    [4] => Array
        (
            [id] => 5
            [name] => shamonsha
            [username] => sham2s7b8
            [password] => dN0Br+3D86pGTAhlvOJ4OD6YH1KVHL/SkfYOu71Do7OCxrRnMIq9CLVWX7mpTnJso1pYxVwvjzFWo1a1GVq+8Q==
            [email] => shamonsha665@gmail.com
            [mobile_number] => 7736527089
            [address] => jk
            [date] => 24 Dec 2015 01:33:20
            [active] => 1
            [commission] => 0
            [agent_id] => 2
            [ticket_number] => ETS562B0PT10471
            [refund_amount] => 395
            [date_of_cancel] => 28-Jan-2016
        )

)

从此我想以这种方式为每个用户名显示ticket_amountrefund_amountdate_of_cancel 例如:

Username:sham1s670
  ticket-no:ETS263B0PT10469 ,refund-amount:700, cancel-data:28-Jan-2016
  ticket-no:ETS263B0PT10461 ,refund-amount:500, cancel-data:28-Jan-2016
  ticket-no:ETS263B0PT10462 ,refund-amount:200, cancel-data:28-Jan-2016

Username:sham1s674
  ticket-no:ETS263B0PT10462 ,refund-amount:700, cancel-data:28-Jan-2016
  ticket-no:ETS263B0PT10468 ,refund-amount:600, cancel-data:28-Jan-2016
  ticket-no:ETS263B0PT10469 ,refund-amount:200, cancel-data:28-Jan-2016

这是我尝试过的代码

$i=0;
foreach($result as $value)
{
    $old = $value['username'];
    if($old==$value['username'])
    {
        if($i==0){
            echo 'Username'.$value['username'];
            echo '<br>';
        }
        echo "TIcket-no".$value['ticket_number'].'Refund-amound'.$value['refund_amount'].'Cancel-date'.$value['date_of_cancel'];

        $i++;
    }
    else
    {
        $old='';
        $i=0;
    }
}

但它没有给我预期的结果?

3 个答案:

答案 0 :(得分:0)

尝试此操作(未经测试!),假设您按用户名

对结果进行排序
$old='';
foreach($result as $value)
{
    if($old==$value['username'])
    {   
        echo "TIcket-no".$value['ticket_number'].'Refund-amound'.$value['refund_amount'].'Cancel-date'.$value['date_of_cancel'];

    }
    else
    {

        echo 'Username'.$value['username'];
        echo '<br>';
    }
    $old = $value['username'];
}

答案 1 :(得分:0)

您可以使用新数组对结果进行排序。

像这样:

<?php
$NewTab = array();
foreach($results as $val) {
    $NewTab[$val["username"]][] = $val;
}

foreach($NewTab as $username => $entries) {
    echo "Username:", $username;
    foreach($entries as $entry) {
        echo "ticket-no: ", $entry["ticket_amount"];
        echo ", refund-amount: ", $entry["refund_amount"];
        echo ", date_of_cancel: ", $entry["date_of_cancel"];
    }
}

答案 2 :(得分:0)

它没有给出理想的结果,因为$ old永远不会像$ value [&#39;用户名&#39;](你永远不会开始那样),hense总是触发其他选项。

您必须重新编写查询并按用户分组,或者在解析结果后将结果排序以匹配具有故障单的用户

未测试:

<?php

  $data = array();
  $output = false;

  // this first foreach could be done on db fetch rows instead
  foreach ( $result as $value ) {

      if ( !isset( $data[$value['username']]['name'] ) ) {
        $data[$value['username']]['name'] = $value['name'];
      }
      $data[$value['username']]['data'][] = 'TIcket-no ' . $value['ticket_number'] . ', Refund-amound ' . $value['refund_amount'] . ', Cancel-date ' . $value['date_of_cancel'];

  }

  foreach( $data as $username => $dataset ) {

    $output .= '<p>Name: ' . $dataset['name'] . '<br>';
    $output .= 'Username: ' . $username . '<br>';
    $output .= implode( '<br>', $dataset['data'] );
    $output .= '</p>';

  }

  echo $output;

?>

**每次请求更新**

添加用户名(仍未经测试)

    // Declare the snapshot boundaries
    let top: CGFloat = 100
    let bottom: CGFloat = 150

    // The size of the cropped image
    let size = CGSize(width: view.frame.size.width, height: view.frame.size.height - top - bottom)

    // Start the context
    UIGraphicsBeginImageContext(size)

    // we are going to use context in a couple of places
    let context = UIGraphicsGetCurrentContext()!

    // Transform the context so that anything drawn into it is displaced "top" pixels up
    // Something drawn at coordinate (0, 0) will now be drawn at (0, -top)
    // This will result in the "top" pixels being cut off
    // The bottom pixels are cut off because the size of the of the context
    CGContextTranslateCTM(context, 0, -top)

    // Draw the view into the context (this is the snapshot)
    view.layer.renderInContext(context)
    let snapshot = UIGraphicsGetImageFromCurrentImageContext()

    // End the context (this is required to not leak resources)
    UIGraphicsEndImageContext()

    // Save to photos
    UIImageWriteToSavedPhotosAlbum(snapshot, nil, nil, nil)
相关问题