简体   繁体   English

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

[英]Show unique array data for one column but multiple items for other column

I have a loop that's showing a contract and a location. 我有一个循环,显示合同和位置。

It's looping through and getting the contract name and the location per row. 它遍历并获取合同名称和每行的位置。 There are duplicate contracts in the rows so I only want to show one then list all the locations underneath it. 行中有重复的合同,因此我只想显示一个合同,然后列出其下方的所有位置。

I know I can use something like array_unique(); 我知道我可以使用诸如array_unique();类的东西array_unique(); to strip out the duplicates but not sure how to integrate that into my loop. 删除重复项,但不确定如何将其集成到我的循环中。

How can I show all the unique contracts and show multiple locations tied to that contract? 如何显示所有唯一合同并显示与该合同相关的多个位置?

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

This is some of the data: 这是一些数据:

  1. Adidas Viables 3 Viables Business Adidas Viables 3 Viables业务

  2. Aldi Bridgend 阿尔迪·布里真德

  3. Aldi Pembroke Dock 阿尔迪彭布罗克码头

  4. Aldi Haverfordwest Aldi Haverfordwest

The first word is the contract, the rest after that is the location. 第一个词是合同,其余的是位置。

I want to only show one "Aldi" with locations 2, 3 and 4 underneath it for example. 例如,我只想显示一个在其下方带有位置2、3和4的“ Aldi”。 So one unique contract can have multiple locations. 因此,一个唯一的合同可以有多个位置。

I'm not so fluent in wordpress but sure you could solve the order part via the wp_query . 我对Wordpress不太熟练,但可以确定可以通过wp_query解决订单部分。 Either way, you could build your $clients array like so: 无论哪种方式,您都可以像这样构建$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; ?>

That will create the $clients array looking something like this where you can foreach over it... 这将创建$clients数组,看起来像这样,您可以在其上进行遍历...

<?php

$clients['Adidas']['locations'] = [];
array_push($clients['Adidas']['locations'], 'Viables 3 Viables Business');

$clients['Aldi']['locations'] = [];
array_push($clients['Aldi']['locations'], 'Bridgend');
array_push($clients['Aldi']['locations'], 'Pembroke Dock');
array_push($clients['Aldi']['locations'], 'Haverfordwest');

foreach ($clients as $contract => $locations ) {
    echo 'Contract: ' . $contract . "\n";
    sort($locations['locations']); // this will sort the locations alphabetically
    foreach ($locations['locations'] as $location ) {
        echo ' - Location: ' . $location . "\n";
    }
}

Result looks like so: 结果看起来像这样:

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

You can play with it here 你可以在这里玩

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM