这里有一个设置来解释我的问题,但理论上,我需要做的就是在输出之前订购一个多维数组。我遇到的问题是它没有将所有的As,B和C等分组在一起。我认为在添加第一个字母之前需要对所有名称进行排序......
<?php query_posts ( array (
'post_type' => 'programme',
'category_name' => 'archive',
'order' => 'DESC' ) ); ?>
<div class="container_12">
<div class="prefix_1 grid_10 suffix_1">
<?php while ( have_posts() ) : the_post(); ?>
<?php
$groups = array();
if ( have_rows('artists') ) {
while ( have_rows('artists') ) {
the_row();
// vars
$first_name = get_sub_field('first_name');
$last_name = get_sub_field('last_name');
$first_letter = substr($last_name, 0, 1);
// add $first_letter holder to groups
if( !isset($groups[ $first_letter ]) ) {
$groups[ $first_letter ] = array();
}
// append artist to group
$groups[ $first_letter ][] = $first_name . ' ' . $last_name;
}
}
// ouput
if( !empty($groups) ): ?>
<?php foreach( $groups as $letter => $artists ) : ?>
<h3><?php echo $letter; ?></h3>
<?php foreach( $artists as $artist ): ?>
<p><?php echo $artist; ?></p>
<?php endforeach; ?>
<?php endforeach; ?>
<?php endif; ?>
<?php endwhile; ?>
</div>
</div>
<div class="clear"></div>
<?php wp_reset_postdata(); ?>
目前输出为:
如果您在下图中看到我只有print_r($ groups),则需要将所有数组添加到一个大数组中,然后对其进行排序。
所以,最终,我想结束类似的事情:
A
Joe Allan
Frank Aztec
B
Jane Bank
C
Mike Crichton
Mandy Curtz
更新
这是我没有的,需要按姓氏的字母顺序对艺术家进行排序。
<?php if( !empty($groups) ) : ?>
<?php ksort($groups); ?>
<?php foreach ( $groups as $letter => $artists ) : ?>
<div>
<h3><?php echo $letter; ?></h3>
<?php asort($artists); ?>
<?php foreach( $artists as $artist ) : ?>
<p><?php echo $artist; ?></p>
<?php endforeach; ?>
</div>
<?php endforeach; ?>
<?php endif; ?>
这给了我,以 B 为例:
B
Berendes, Eva
Bloor, Simon
Bloor, Tom
Burt, Theo
Barnes, Kristian
Bajo, Elena
答案 0 :(得分:2)
我想提出一个完全不同的问题方法。我知道这不是您发布的确切问题的答案,但我认为这是您问题的答案。我相信你的问题是让大家都有大写字母,所以,我不想做所有这些PHP噩梦,我想建议你从DDBB通过MySQL获得所有订购(因为通过PHP订购很难,很复杂(如你只是看着)和资源猪......)只打印结果:
<?php
// Get access to DDBB manager object
global $wpdb;
// Launch a query will give back all ordered, in rows with the capital letter
// and an array in JSON format with the group names
$query = $wpdb->get_results("
SELECT
GROUP_CONCAT('[', CONCAT(first_name, ', ',last_name), ']' SEPARATOR ', ') as name
, LEFT(last_name, 1) capLetter
FROM artists
GROUP BY capLetter
ORDER BY last_name, first_name;");
// Printing the results (they came ordered, ;D):
foreach ($query as $row) {
echo $row->capLetter;
$artists = json_decode($row->name);
foreach ($artists as $artist) {
echo $artist;
}
}
?>
我使用GROUP_CONCAT来构建一个JSON数组,我们在其中存储来自DDBB的值,并且来自MySQL引擎的组和顺序很容易实现我们想要的。我只测试了查询,我从http://www.makeuseof.com/tag/working-custom-database-tables-wordpress/
开始使用WordPress启动自定义查询答案 1 :(得分:1)
将$groups = array();
置于第一个while循环之上,并且还:
if( !empty($groups) ): ?>
<?php foreach( $groups as $letter => $artists ) : ?>
<h3><?php echo $letter; ?></h3>
<?php foreach( $artists as $artist ): ?>
<p><?php echo $artist; ?></p>
<?php endforeach; ?>
<?php endforeach; ?>
<?php endif; ?>
低于<?php endwhile; ?>
答案 2 :(得分:1)
与Chococroc提出的方法类似,使用数据库查询实现它将是有效的。
但是,如果您已经为某些其他目的运行查询并检索艺术家,那么您可以使用基于PHP的解决方案,而不是运行其他查询。
假设您可以获取所有名称,我正在考虑这样的输入数组:
$allNames =
array(
array( 'Kate' ,'Allen' )
,array( 'Simon' ,'Bloor' )
,array( 'Tom' ,'Bloor' )
,array( 'Theo' ,'Burt' )
,array( 'Ross' ,'Chisholm' )
,array( 'Eva' ,'Berendes' )
,array( 'Kristian' ,'Barnes' )
,array( 'Ashley' ,'Holmes' )
,array( 'Joe' ,'Allan' )
,array( 'Frank' ,'Aztec' )
,array( 'Jane' ,'Bank' )
,array( 'Kike' ,'Crichton' )
,array( 'Mandy' ,'Curtz' )
);
实际的解决方案从这里开始:
/*
* Put all the names in to $tempArray as lastName:firstName format
*/
$tempArray = array();
foreach( $allNames as $fullName )
{
list( $firstName ,$lastName ) = $fullName;
$tempArray[] = $lastName .':' .$firstName;
}
/*
* Use which ever sort suits your requirement and sort the $tempArray
*/
sort( $tempArray );
/*
* Now put the names into groups by the first letter of last name
*/
$groups = array( );
foreach( $tempArray as $value )
{
$firstLetter = substr( $value ,0 ,1 );
if( ! isset( $groups[$firstLetter] ) ) $groups[$firstLetter] = array( );
list( $lastName ,$firstName ) = explode( ":" ,$value );
$groups[$firstLetter][] = $firstName .' ' .$lastName;
}
结果:
var_dump( $groups );
给予:
'A' =>
'Joe Allan'
'Kate Allen'
'Frank Aztec'
'B' =>
'Jane Bank'
'Kristian Barnes'
'Eva Berendes'
'Simon Bloor'
'Tom Bloor'
'Theo Burt'
'C' =>
'Ross Chisholm'
'Kike Crichton'
'Mandy Curtz'
'H' =>
'Ashley Holmes'
如果我要将我的解决方案包含在您尝试过的代码中,可能会转到如下所示。这未经过测试,可能需要修改:
<?php query_posts ( array (
'post_type' => 'programme',
'category_name' => 'archive',
'order' => 'DESC' ) ); ?>
<div class="container_12">
<div class="prefix_1 grid_10 suffix_1">
<?php
/*
* Put all the names in to $tempArray as lastName:firstName format
*/
$tempArray = array();
?>
<?php while( have_posts() ) : the_post(); ?>
<?php
if( have_rows( 'artists' ) )
{
while( have_rows('artists') )
{
the_row();
$tempArray[] = get_sub_field( 'last_name' ) .':' .get_sub_field( 'first_name' );
}
}
?>
<?php endwhile; ?>
<?php
/*
* Use which ever sort suits your requirement and sort the $tempArray
*/
sort( $tempArray );
/*
* Now put the names into groups by the first letter of last name
*/
$groups = array( );
foreach( $tempArray as $value )
{
$firstLetter = substr( $value ,0 ,1 );
if( ! isset( $groups[$firstLetter] ) ) $groups[$firstLetter] = array( );
list( $lastName ,$firstName ) = explode( ":" ,$value );
$groups[$firstLetter][] = $firstName .' ' .$lastName;
}
?>
<?php if( ! empty($groups) ): ?>
<?php foreach( $groups as $letter => $artists ) : ?>
<h3><?php echo $letter; ?></h3>
<?php foreach( $artists as $artist ): ?>
<p><?php echo $artist; ?></p>
<?php endforeach; ?>
<?php endforeach; ?>
<?php endif; ?>
</div>
</div>
<div class="clear"></div>
<?php wp_reset_postdata(); ?>
答案 3 :(得分:0)
由于作者在排序数组时遇到了另一个问题,我将添加另一个答案,希望对此有所帮助。
如我的评论中所述,您应该使用ksort
按键对外部数组进行排序。
现在按姓氏排序内部数组有点棘手。
有一个名为uasort
的有用函数,它允许用户定义一个函数,通过该函数比较数组条目。
所以你想要做的是这样的事情:
ksort($groups);
foreach ($groups as &$group) {
uasort($group, 'sortLastName');
}
unset($group);
使用sortLastName
作为比较函数,其定义如下:
function sortLastName($a, $b) {
$aLast = end(explode(" ", $a));
$bLast = end(explode(" ", $b));
return strcasecmp($aLast, $bLast);
};
这是Ideone.com上的一个工作示例 http://ideone.com/vU8ukk
希望我能帮到你。