Warning: file_get_contents(/data/phpspider/zhask/data//catemap/1/wordpress/11.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Php 显示一列的唯一数组数据,但显示另一列的多个项_Php_Wordpress - Fatal编程技术网

Php 显示一列的唯一数组数据,但显示另一列的多个项

Php 显示一列的唯一数组数据,但显示另一列的多个项,php,wordpress,Php,Wordpress,我有一个循环,显示合同和位置 它循环遍历并获取每行的合同名称和位置。行中有重复的合同,所以我只想显示一个,然后列出它下面的所有位置 我知道我可以使用类似于array_unique()的东西删除重复项,但不确定如何将其集成到我的循环中 如何显示所有唯一的合同以及与该合同相关的多个地点? <?php // Find all locations tied to the current logged in user. $args = array( 'posts_per_page

我有一个循环,显示合同和位置

它循环遍历并获取每行的合同名称和位置。行中有重复的合同,所以我只想显示一个,然后列出它下面的所有位置

我知道我可以使用类似于
array_unique()的东西
删除重复项,但不确定如何将其集成到我的循环中

如何显示所有唯一的合同以及与该合同相关的多个地点?

  <?php
  // Find all locations tied to the current logged in user.
  $args = array(
    'posts_per_page'    => -1,
    'post_type'     => 'locations',
    'meta_query'    => array(
        'relation'      => 'OR',
        array(
            'key'       => 'regional_operations_manager',
            'value'     => $user->id,
            'compare'   => 'LIKE'
        ),
    ),
  );
  // query
  $the_query = new WP_Query( $args ); ?>

  <?php if( $the_query->have_posts() ):
    while( $the_query->have_posts() ) : $the_query->the_post(); ?>
      <?php $clients[] = array('contract' => get_field('contract'), 'location' => get_the_title()); ?>
    <?php endwhile;
  endif;

  // Sort them alphabetically and only show unique locations.
  //$contracts = array_unique( $contracts );
  //sort($contracts);
  foreach ( $clients as $key => $row ) { ?>
    <?php echo $contract[$key] = $row['contract']; ?><br />
    <?php echo $location[$key] = $row['location']; ?><br />
  <?php } ?>



以下是一些数据:

  • 阿迪达斯 小瓶3小瓶业务

  • 阿尔迪 布里金德

  • 阿尔迪 彭布罗克码头

  • 阿尔迪 哈弗福德韦斯特

  • 第一个词是合同,后面的词是地点


    我只想显示一个“Aldi”,例如它下面的位置2、3和4。因此,一份独特的合同可以有多个地点。

    我对wordpress不太熟悉,但我相信您可以通过解决订单部分。无论哪种方式,您都可以像这样构建
    $clients
    阵列:

    <?php if( $the_query->have_posts() ):
        while( $the_query->have_posts() ) : $the_query->the_post(); ?>
          <?php $clients[get_field('contract')]['locations'] = []; ?>
          <?php array_push($clients[get_field('contract')]['locations'], get_the_title()); ?>
        <?php endwhile; ?>
      <?php endif; ?>
    
    结果是这样的:

    Contract: Adidas
     - Location: Viables 3 Viables Business
    Contract: Aldi
     - Location: Bridgend
     - Location: Haverfordwest
     - Location: Pembroke Dock
    

    我对wordpress不是很流利,但我相信你可以通过以下方式解决订单部分。无论哪种方式,您都可以像这样构建
    $clients
    阵列:

    <?php if( $the_query->have_posts() ):
        while( $the_query->have_posts() ) : $the_query->the_post(); ?>
          <?php $clients[get_field('contract')]['locations'] = []; ?>
          <?php array_push($clients[get_field('contract')]['locations'], get_the_title()); ?>
        <?php endwhile; ?>
      <?php endif; ?>
    
    结果是这样的:

    Contract: Adidas
     - Location: Viables 3 Viables Business
    Contract: Aldi
     - Location: Bridgend
     - Location: Haverfordwest
     - Location: Pembroke Dock
    

    看起来您正在使用wordpress。检查并搜索
    groupby
    orderby
    您想更改wp\u查询,以便按照您想要的顺序获得正确的结果。@caramba我已经在这段代码上面添加了它真正的功能。它查找当前登录的用户,然后查找与该用户相关的所有CPT
    位置。我不知道我是否还能做到,可以吗?@caramba刚刚更新。看起来你在用wordpress。检查并搜索
    groupby
    orderby
    您想更改wp\u查询,以便按照您想要的顺序获得正确的结果。@caramba我已经在这段代码上面添加了它真正的功能。它查找当前登录的用户,然后查找与该用户相关的所有CPT
    位置。我不知道我是否还能做到,可以吗?@caramba刚刚更新。@Rob检查while循环,那里没有硬编码的名字!这些只是举一个例子,说明数据看起来是什么样子的!!我的头在桌子上撞了好几个小时!!再次感谢。@Rob检查while循环,里面没有硬编码的名称!这些只是举一个例子,说明数据看起来是什么样子的!!我的头在桌子上撞了好几个小时!!再次感谢。