[英]Sorting and displaying values systematically from a multi-dimensional PHP array
在下面,我附加了通過涉及HTML表單的用戶操作創建的數組的$ var_dump。
我當然願意考慮實現最終效果的其他策略(老實說,我不是很肯定通過WordPress函數不能更好地實現它,因為這是問題產生的環境),但是最簡單答案將是采用下面描述的數組,擺脫“ NULL”響應,然后按姓氏將其余所有集按字母順序排列(我最初把它放在第一位,因為在同一任務的更早版本,相似版本中,它恰巧最后排序一下)。
輸出將在事件發生時顯示在登錄列表上,第一列編號,第二列顯示名字-姓氏在其下的公司,簽名行,描述“機票類型”的行以及機票價格。
因此,為了使其易於描繪:
第一張/最后一張/公司簽名票類型價格
(#)Amy Abrams,Carolco .........會員$ 5
對我來說,完成此操作的自然方法是生成一個HTML表,其中通過PHP輸出通過某種索引定位的每個變量。 我有一些版本可以使用ALMOST,在我對數組操作的理解的極限范圍內涉及到foreach循環。
我懷疑可以回答我問題的人可以寫我在他或她的睡眠中嘗試過的方法,但是,只是為了給出一種味道而無需轉儲更多代碼...在一個名為$的變量中獲取了以下數組meta,我可以遍歷它foreach ($meta as $reservation)
,然后使用$reservation['first_name'][0]
打印一些變量,但不是全部,也不是全部按正確順序排列。 我嘗試將$ i替換為[0],並鬼混其他鍵/值/迭代器組合,但此時,我只是在遍歷自己的內部ca數組。 999個錯誤與1個正確的解決方案。
我知道這是一種常見的問題,但是在這里瀏覽線程時,我還沒有找到一個可以直接回答的版本。
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"
}
}
}
因此,首先,您以一種非常奇怪的方式構建了陣列。 如果您要做的只是輸出所有不同的來賓,則最好在數組中為每個來賓創建一個新元素,這樣您就可以對它們進行排序並輕松地對其進行迭代。 但是,正如您所提出的問題,我將為您提供解決方案...
為了能夠對您的數組進行排序,我們需要將您的數組轉換為我上面描述的數組。 這個數組看起來像這樣...
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;
}
現在,例如,您未格式化的保留數組存儲在變量$reservations
您可以使用...對其進行排序。
$guests = sort_reservations( $reservations );
然后,您可以使用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>
鮑勃是你的叔叔,你有一張客人的桌子,按姓氏的字母順序排列。
希望能有所幫助
擔
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.