gpt4 book ai didi

php - 在php中从数组中获取结果

转载 作者:行者123 更新时间:2023-11-29 21:33:35 26 4
gpt4 key购买 nike

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

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

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

最佳答案

它没有给出期望的结果,因为 $old 永远不会像 $value['username'] (你永远不会启动它),因此总是触发 else 选项。

您必须重写查询并按用户分组,或者在解析结果后对结果进行排序,以将用户与票证相匹配

未经测试:

<?php

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

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

$data[$value['username']][] = 'TIcket-no ' . $value['ticket_number'] . ', Refund-amound ' . $value['refund_amount'] . ', Cancel-date ' . $value['date_of_cancel'];

}

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

$output .= '<p>Username ' . $username . '<br>';
$output .= implode( '<br>', $list );
$output .= '</p>';

}

echo $output;

?>

** 根据请求更新 **

添加用户名(尚未测试)

<?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;

?>

关于php - 在php中从数组中获取结果,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/35067100/

26 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com