从多维PHP数组中系统地排序和显示值
下面我添加了一个数组的$var_dump,该数组是我通过涉及HTML表单的用户操作创建的 <>我当然愿意考虑其他实现最终效果的策略,我不诚实地肯定它不会通过WordPress函数来实现,因为这是这个问题产生的环境,但是最简单的答案是采用下面描述的数组,去掉空响应,然后按姓氏按字母顺序排列所有剩余的集合,我最初将姓氏放在第一位,因为在同一任务的早期类似版本中,它碰巧使最后的排序很快 输出将出现在活动的登录列表上,第一列编号,第二列显示名字-姓氏,下面是公司,一个签名行,一行描述票类型和票价 因此,为了便于拍摄,请: 编号首张/末张/公司签名票类型价格 艾米·艾布拉姆斯,卡罗尔科。。。。。。。。。会员:5美元 对我来说,最自然的方法是生成一个HTML表,其中通过某种索引定位的每个变量都通过PHP输出。我已经有了一些几乎可以工作的版本,涉及foreach循环,但我对数组操作的理解还很有限 我怀疑能回答我问题的人可以写下我在他或她的睡眠中尝试过的东西,但是,只是为了给人一种味道,而不是在一堆代码中倾诉。。。在一个名为$meta的变量中获得了下面的数组后,我可以通过它foreach$meta作为$reservation,然后使用$reservation['first_name'][0]打印一些变量,但不是所有变量,也不是所有变量的顺序都正确。我试着用$I来代替[0],并在其他键/值/迭代器组合中游手好闲,但在这一点上,我只是在迭代我自己的内部数组ca.999错误对1正确的解决方案 我知道这是一个常见的问题,但在仔细研究这里的线索时,我还没有找到一个像这样直接回答的版本从多维PHP数组中系统地排序和显示值,php,arrays,wordpress,sorting,sub-array,Php,Arrays,Wordpress,Sorting,Sub Array,下面我添加了一个数组的$var_dump,该数组是我通过涉及HTML表单的用户操作创建的 我当然愿意考虑其他实现最终效果的策略,我不诚实地肯定它不会通过WordPress函数来实现,因为这是这个问题产生的环境,但是最简单的答案是采用下面描述的数组,去掉空响应,然后按姓氏按字母顺序排列所有剩余的集合,我最初将姓氏放在第一位,因为在同一任务的早期类似版本中,它碰巧使最后的排序很快 输出将出现在活动的登录列表上,第一列编号,第二列显示名字-姓氏,下面是公司,一个签名行,一行描述票类型和票价 因此,为了
array(8) {
[0]=>
array(5) {
["last_name"]=>
NULL
["first_name"]=>
NULL
["price"]=>
NULL
["company"]=>
NULL
["ticket_type"]=>
NULL
}
[1]=>
array(5) {
["last_name"]=>
array(3) {
[0]=>
string(6) "Winger"
[1]=>
string(6) "Dinger"
[2]=>
string(7) "Stassen"
}
["first_name"]=>
array(3) {
[0]=>
string(5) "Debra"
[1]=>
string(3) "Hum"
[2]=>
string(6) "Harold"
}
["price"]=>
array(3) {
[0]=>
string(2) "10"
[1]=>
string(2) "10"
[2]=>
string(4) "6.35"
}
["company"]=>
array(3) {
[0]=>
string(14) "Post Data Test"
[1]=>
string(14) "Post Data Test"
[2]=>
string(14) "Post Data Test"
}
["ticket_type"]=>
array(3) {
[0]=>
string(16) "Shippable Ticket"
[1]=>
string(16) "Shippable Ticket"
[2]=>
string(14) "Special Ticket"
}
}
[2]=>
array(5) {
["last_name"]=>
NULL
["first_name"]=>
NULL
["price"]=>
NULL
["company"]=>
NULL
["ticket_type"]=>
NULL
}
[3]=>
array(5) {
["last_name"]=>
array(3) {
[0]=>
string(9) "Mightwork"
[1]=>
string(1) "u"
[2]=>
string(3) "why"
}
["first_name"]=>
array(3) {
[0]=>
string(9) "Bizarrely"
[1]=>
string(11) "whatsamatta"
[2]=>
string(8) "done no "
}
["price"]=>
array(3) {
[0]=>
string(1) "5"
[1]=>
string(4) "12.7"
[2]=>
string(4) "12.7"
}
["company"]=>
array(3) {
[0]=>
string(13) "Get Data Test"
[1]=>
string(13) "Get Data Test"
[2]=>
string(13) "Get Data Test"
}
["ticket_type"]=>
array(3) {
[0]=>
string(16) "Shippable Ticket"
[1]=>
string(14) "Special Ticket"
[2]=>
string(14) "Special Ticket"
}
}
[4]=>
array(5) {
["last_name"]=>
NULL
["first_name"]=>
NULL
["price"]=>
NULL
["company"]=>
NULL
["ticket_type"]=>
NULL
}
[5]=>
array(5) {
["last_name"]=>
array(2) {
[0]=>
string(7) "Marlatt"
[1]=>
string(7) "Stewart"
}
["first_name"]=>
array(2) {
[0]=>
string(4) "Jeff"
[1]=>
string(3) "Al "
}
["price"]=>
array(2) {
[0]=>
string(1) "0"
[1]=>
string(1) "0"
}
["company"]=>
array(2) {
[0]=>
string(23) "Jeff Marlatt Consulting"
[1]=>
string(23) "Jeff Marlatt Consulting"
}
["ticket_type"]=>
array(2) {
[0]=>
string(16) "testing defaults"
[1]=>
string(16) "testing defaults"
}
}
[6]=>
array(5) {
["last_name"]=>
NULL
["first_name"]=>
NULL
["price"]=>
NULL
["company"]=>
NULL
["ticket_type"]=>
NULL
}
[7]=>
array(5) {
["last_name"]=>
array(3) {
[0]=>
string(10) "Flintstone"
[1]=>
string(10) "Flintstone"
[2]=>
string(6) "Rubble"
}
["first_name"]=>
array(3) {
[0]=>
string(4) "Fred"
[1]=>
string(5) "Wilma"
[2]=>
string(5) "Betty"
}
["price"]=>
array(3) {
[0]=>
string(1) "0"
[1]=>
string(1) "0"
[2]=>
string(1) "0"
}
["company"]=>
array(3) {
[0]=>
string(23) "Jeff Marlatt Consulting"
[1]=>
string(23) "Jeff Marlatt Consulting"
[2]=>
string(23) "Jeff Marlatt Consulting"
}
["ticket_type"]=>
array(3) {
[0]=>
string(6) "MEMBER"
[1]=>
string(6) "MEMBER"
[2]=>
string(6) "MEMBER"
}
}
}
首先,您以一种非常奇怪的方式构建了阵列。如果您只想输出所有不同的来宾,那么最好在数组中为每个来宾创建一个新元素,这样您就可以轻松地对它们进行排序和迭代。然而,正如你提出的问题,我会给你一个解决方案
//This function is used by usort() to sort the guests array by last name
//UPDATE: this function is now case insensitive and sorts on first name secondarily
function sort_by_last_name( $a, $b ) {
//Convert values to lowercase to make sorting case insensitive
$a_firstname = strtolower( $a['first_name'] );
$a_lastname = strtolower( $a['last_name'] );
$b_firstname = strtolower( $b['first_name'] );
$b_lastname = strtolower( $b['last_name'] );
//If the last names are the same sort by first name
if( $a_lastname == $b_lastname ) {
return strcmp( $a_firstname, $b_firstname );
}
return strcmp( $a_lastname, $b_lastname );
}
//This function returns a nicely formatted array of
//reservations sorted alphabetically by last name
function sort_reservations( $reservations ) {
//If reservations is empty get outta there
if( !$reservations || !is_array( $reservations ) ) { return false; }
//Create an empty array to store the formatted data
$guests = array();
//Loop through reservations
foreach ( $reservations as $res ) {
//If the reservation is empty skip onto the next one
if( !$res['last_name'] ) { continue; }
//See how many people are in the reservation
$count = count( $res['last_name'] );
//iterate through reservation n number of times
for( $i = 0; $i <= $count - 1; $i++ ) {
//Add reservations to the nsorted guests array
$guests[] = array(
'last_name' => $res['last_name'][$i],
'first_name' => $res['first_name'][$i],
'price' => $res['price'][$i],
'company' => $res['company'][$i],
'ticket_type' => $res['ticket_type'][$i]
);
}
}
//sort gusts by last name alphabetically
usort( $guests, 'sort_by_last_name' );
//Return our nicely formatted and sorted array
return $guests;
}
为了能够对您的数组进行排序,我们需要将您的数组转换为我上面描述的数组。这个数组看起来像这样
array(
[0] => array(
[last_name] => 'blah',
[first_name] => 'blah',
[company] => 'blah',
[ticket_type] => 'blah',
[price] => 'blah',
),
[1] => array(
[last_name] => 'blah',
[first_name] => 'blah',
[company] => 'blah',
[ticket_type] => 'blah',
[price] => 'blah',
),
etc...
);
然后,可以使用PHP函数usort根据子数组的值对多维数组进行排序。这是我的解决方案
//This function is used by usort() to sort the guests array by last name
//UPDATE: this function is now case insensitive and sorts on first name secondarily
function sort_by_last_name( $a, $b ) {
//Convert values to lowercase to make sorting case insensitive
$a_firstname = strtolower( $a['first_name'] );
$a_lastname = strtolower( $a['last_name'] );
$b_firstname = strtolower( $b['first_name'] );
$b_lastname = strtolower( $b['last_name'] );
//If the last names are the same sort by first name
if( $a_lastname == $b_lastname ) {
return strcmp( $a_firstname, $b_firstname );
}
return strcmp( $a_lastname, $b_lastname );
}
//This function returns a nicely formatted array of
//reservations sorted alphabetically by last name
function sort_reservations( $reservations ) {
//If reservations is empty get outta there
if( !$reservations || !is_array( $reservations ) ) { return false; }
//Create an empty array to store the formatted data
$guests = array();
//Loop through reservations
foreach ( $reservations as $res ) {
//If the reservation is empty skip onto the next one
if( !$res['last_name'] ) { continue; }
//See how many people are in the reservation
$count = count( $res['last_name'] );
//iterate through reservation n number of times
for( $i = 0; $i <= $count - 1; $i++ ) {
//Add reservations to the nsorted guests array
$guests[] = array(
'last_name' => $res['last_name'][$i],
'first_name' => $res['first_name'][$i],
'price' => $res['price'][$i],
'company' => $res['company'][$i],
'ticket_type' => $res['ticket_type'][$i]
);
}
}
//sort gusts by last name alphabetically
usort( $guests, 'sort_by_last_name' );
//Return our nicely formatted and sorted array
return $guests;
}
然后用html构建表,对于每个表行,可以迭代该数组,在每一行打印出一个新的来宾。像这样
<?php $guests = sort_reservations( $reservations ); ?>
<table>
<thead>
<tr>
<th class="firstname">First Name</th>
<th class="lastname">Last Name</th>
<th class="company">Company</th>
<th class="tickettype">Ticket Type</th>
<th class="price">Price</th>
</tr>
</thead>
<tbody>
<?php foreach( $guests as $guest ) { ?>
<tr>
<td class="firstname"><?php echo $guest['first_name']; ?></td>
<td class="lastname"><?php echo $guest['last_name']; ?></td>
<td class="company"><?php echo $guest['company']; ?></td>
<td class="tickettype"><?php echo $guest['ticket_type']; ?></td>
<td class="price">£<?php echo round( $guest['price'], 2 ); ?></td>
</tr>
<?php } ?>
</tbody>
</table>
鲍勃是你叔叔,你有一桌按姓氏字母顺序排列的客人
希望有帮助
Dan在我尝试您的解决方案之前,感谢您非常慷慨的工作。此外,我愿意从一开始就更智能、更高效地构建阵列,以使输出更容易。我怀疑知道如何去做这件事对其他从事这类工作的人是有用的。这个数组是由用户行为随着时间的推移填写一个表单,但是如果说,在注册过程中添加一个隐藏字段或者一个不同的构造方法将简化重新输出数据,我很乐意考虑这个项目,或者是将来的项目。答案是将数据以数组的格式存储在我答案的第一个代码块中。我不知道你的表单是什么样子的,很难说你可以使用什么机制,但问题是当你应该将每个预订保存为数组中的一个单独元素时,你将预订分为三组。我不得不说,你的方法太优雅了,我没有强烈的动机重新做它有机建筑。下面是您的解决方案是如何开箱即用的:显然,我想替换不区分大小写的排序,添加第二个名字排序或预排序,并正确处理数字,但总体来说这是一个巨大的进步。非常好的工作。我已经修改了我的答案,使排序函数不区分大小写,如果姓氏相同,还可以按名字进行第二次排序。出现的奇怪符号可能是您的字符集未定义,因此无法理解磅符号。我现在用html实体“£;”替换了它另外,如果您对数组是如何构造的感到好奇,那么在与此项目相关的前一个问题中有一个基本的解释