猿问

PHP-获取在多个索引中具有值的键

您可以像这样使用自定义查询


<?php


$authorID = get_the_author_meta('ID');


$args = array(

    'post_type' => 'product',

    'post_status' => 'publish'

    'posts_per_page' => 12,

    'product_cat' => 'pants'

    'author'    => $authorID

);


$loop = new WP_Query( $args );


?>        

<div class="author_products">

    <?php if ( $loop->have_posts() ) { ?>

        <ul class="author_pubproducts">

        <?php while ( $loop->have_posts() ) : $loop->the_post();

            woocommerce_get_template_part( 'content', 'product' );

        endwhile; ?>

        </ul>

        <?php

        } else {

            echo __( 'No products found', 'textdomain' );

        }

        wp_reset_postdata();

    ?>

希望能成功


绝地无双
浏览 88回答 2
2回答

狐的传说

这就是我要做的:// I take first element of array as a source for indexesforeach ($myArray[0] as $index => $item) {&nbsp; &nbsp; // next I extract all elements from all subarrays under current `$index`&nbsp; &nbsp; $values = array_column($myArray, $index);&nbsp; &nbsp; // then I filter values to remove nulls.&nbsp;&nbsp; &nbsp; // This also removes 0, empty arrays, false,&nbsp;&nbsp; &nbsp; // so maybe you should change filter process&nbsp; &nbsp; $values_filtered = array_filter($values);&nbsp; &nbsp; // if number of filtered items is same as in original array - no nulls found&nbsp; &nbsp; if (count($values_filtered) === count($values)) {&nbsp; &nbsp; &nbsp; &nbsp; echo $index;&nbsp; &nbsp; &nbsp; &nbsp; // optionally&nbsp; &nbsp; &nbsp; &nbsp; // break;&nbsp;&nbsp; &nbsp; }}

一只名叫tom的猫

尽管有一个公认的答案,但我想我会分享一种使用 Laravel 集合来做到这一点的方法。&nbsp;$uniqueKeysWithValues = collect($myArray)->map(function($item){&nbsp; &nbsp; return array_keys( collect($item)->filter()->toArray() ); //filter will remove all null&nbsp;})->flatten()->unique();这种方法将为您提供所有包含值的键,即使两个键中都有值。
随时随地看视频慕课网APP
我要回答