如何根据特定的列值制作不同的数组

时间:2018-04-20 11:18:29

标签: php arrays yii

我想基于列值制作不同的数组,例如我有2个数组

第一个就像 -

Array
(
    [1] => Products
    [3] => Services
    [4] => External
    [5] => Accessories
)

第二个就像

Array
(
    [0] => Array
        (
            [ad_id] => 1
            [u_id] => 2
            [country_id] => 1
            [ur_id] => 3
            [ad_type] => 2
            [path] => 1508927659.jpg
            [thumbnail_image] => 
            [ad_category_id] => 1
            [inserted_date] => 2017-10-25 16:04:19
            [expire_date] => 2017-12-08 06:58:10
            [is_approve] => 1
            [is_pay] => 0
            [payment_id] => 
            [payment_status] => 
            [amount] => 
            [ad_category_name] => Products
            [is_active] => 1
        )

    [1] => Array
        (
            [ad_id] => 2
            [u_id] => 2
            [country_id] => 1
            [ur_id] => 3
            [ad_type] => 2
            [path] => 1508933867.wmv
            [thumbnail_image] => 
            [ad_category_id] => 1
            [inserted_date] => 2017-10-25 17:47:47
            [expire_date] => 2017-11-01 11:31:26
            [is_approve] => 2
            [is_pay] => 0
            [payment_id] => 
            [payment_status] => 
            [amount] => 
            [ad_category_name] => Products
            [is_active] => 1
        )

    [2] => Array
        (
            [ad_id] => 15
            [u_id] => 2
            [country_id] => 1
            [ur_id] => 3
            [ad_type] => 2
            [path] => 1509688441.wmv
            [thumbnail_image] => 15096884411.jpg
            [ad_category_id] => 1
            [inserted_date] => 2017-11-03 11:24:01
            [expire_date] => 2017-11-04 11:24:01
            [is_approve] => 0
            [is_pay] => 0
            [payment_id] => 
            [payment_status] => 
            [amount] => 
            [ad_category_name] => Products
            [is_active] => 1
        )


.....
   )

我想像类别一样打印类似值的数据,如

产品是我的第一个类别,因此数据将是

**Product**
 - value one
 - value two
 - value three etc

**Services**
 - value one
 - value two
 - value 3

externalaccessories相同,我使用了以下代码,但我无法获得我想要的预期输出。

 <?php 
 $wb = new Webservice();
        $sql = 'SELECT * FROM `marketing_ads` as ma
                INNER JOIN ad_category as ac
                ON ma.`ad_category_id` = ac.ad_category_id';
        $final_data = $wb->getAllData($sql);


        $sqlCat = 'SELECT * FROM ad_category ';
        $adCatdata = $wb->getAllData($sqlCat);

         for($i=0; $i< count($adCatdata); $i++){


                $serCatArr[$adCatdata[$i]['ad_category_id']] = $adCatdata[$i]['ad_category_name'];


         }


         echo "<pre>";
        print_r ($final_data);
        echo "</pre>";

        foreach ($final_data as $key => $value) {
           echo $value['ad_category_id'].'<br/>';
           if(in_array($value['ad_category_id'], $serCatArr)){
            echo 'match';
           }
        }
?>

请给我一些建议或脚本。

3 个答案:

答案 0 :(得分:1)

我认为这应该有效:

transform

输出:

<?php
// The categories we wish to extract
$array1 = array(
    'Products',
    'Services',
    'External',
    'Accessories'
);

// The result set from the DB I assume?
$array2 = array(
    array( 'ad_id' => 1, 'ad_category_name' => 'Products'),
    array( 'ad_id' => 2, 'ad_category_name' => 'Products'),
    array( 'ad_id' => 3, 'ad_category_name' => 'Services'),
    array( 'ad_id' => 4, 'ad_category_name' => 'External'),
    array( 'ad_id' => 5, 'ad_category_name' => 'Accessories')
);

// The new array which groups the result sets
$array3 = array();

// Loop the desired categories and extract the corresponding DB results into a new array
foreach( $array1 as $v )
{
    // Use array_filter() with a callback to filter the DB results
    $array3[ $v ] = array_filter( $array2, function( $a ) use ( $v ){
        return ( $a[ 'ad_category_name' ] === $v );
    } );
}

print_r( $array3 );

注意:

这也可以在不需要Array ( [Products] => Array ( [0] => Array ( [ad_id] => 1 [ad_category_name] => Products ) [1] => Array ( [ad_id] => 2 [ad_category_name] => Products ) ) [Services] => Array ( [2] => Array ( [ad_id] => 3 [ad_category_name] => Services ) ) [External] => Array ( [3] => Array ( [ad_id] => 4 [ad_category_name] => External ) ) [Accessories] => Array ( [4] => Array ( [ad_id] => 5 [ad_category_name] => Accessories ) ) ) 的情况下实现,所以如果您希望看到,请告诉我。

答案 1 :(得分:1)

你可以在你的yii中应用以下代码吗?

/* QUERY FOR GET CATEGORIES */     
        $sqlCat = 'SELECT * FROM your_cate_table ';
        $adCatdata = $wb->getAllData($sqlCat);

         for($i=0; $i< count($adCatdata); $i++){  


            /* QUERY FOR market_data BY PARTICULAR CATEGORY */

            $sql = 'SELECT * FROM `marketing_ads` as ma
                INNER JOIN cate_table as ac
                ON ma.`ad_category_id` = ac.ad_category_id
                WHERE ac.ad_category_id ='.$adCatdata[$i]['ad_category_id'].'
                ';             

                 $marketinAddData = $wb->getAllData($sql);               

                 /* CHECK DATA IS EMPTY OR NOT*/
                 if(isset($marketinAddData) && !empty($marketinAddData)){
                    $serCatArr[$adCatdata[$i]['ad_category_name']] =$marketinAddData;   
                 }else{
                    $serCatArr[$adCatdata[$i]['ad_category_name']] =array();
                 }          
         }

         echo '<pre>';
         print_r($serCatArr);

答案 2 :(得分:0)

Yii有一个map()帮助:

ArrayHelper::map($array, 'ad_id', 'path', 'ad_category_name');

您可以使用闭包而不是列名来自定义输出。

或者,您可以尝试使用index(),具体取决于数组的值。

ArrayHelper::index($array, 'ad_category_name');