dth8312
2018-06-17 08:32
浏览 43
已采纳

多维数组 - 仅打印特定键值

Here is the output of my program var_dump($myvar):

f_shopping array(2) {
["product-size"]=> array(3) {
    [۰]=> object(WP_Term)#20766 (11) {
        ["term_id"]=> int(45) 
        ["name"]=> string(8) "Large" 
        ["slug"]=> string(5) "l" 
        ["term_group"]=> int(0) 
        ["term_taxonomy_id"]=> int(45) 
        ["taxonomy"]=> string(13) "pa_product-size" 
        ["description"]=> string(0) "" 
        ["parent"]=> int(0) 
        ["count"]=> int(2) 
        ["filter"]=> string(3) "raw" 
        ["meta_value"]=> string(1) "0" 
    } 
    [۱]=> object(WP_Term)#20791 (11) {
        ["term_id"]=> int(47) 
        ["name"]=> string(8) "Small" 
        ["slug"]=> string(5) "s" 
        ["term_group"]=> int(0) 
        ["term_taxonomy_id"]=> int(47) 
        ["taxonomy"]=> string(13) "pa_product-size" 
        ["description"]=> string(0) "" 
        ["parent"]=> int(0) 
        ["count"]=> int(2) 
        ["filter"]=> string(3) "raw" 
        ["meta_value"]=> string(1) "0" 
    } 
    [۲]=> object(WP_Term)#20780 (11) { 
        ["term_id"]=> int(46) 
        ["name"]=> string(10) "Medium" 
        ["slug"]=> string(6) "m" 
        ["term_group"]=> int(0) 
        ["term_taxonomy_id"]=> int(46) 
        ["taxonomy"]=> string(13) "pa_product-size" 
        ["description"]=> string(0) "" 
        ["parent"]=> int(0) 
        ["count"]=> int(2) 
        ["filter"]=> string(3) "raw" 
        ["meta_value"]=> string(1) "0" 
    } 
} 
["shopping-pack"]=> array(4) {
    [۰]=> object(WP_Term)#20751 (11) {
        ["term_id"]=> int(26) 
        ["name"]=> string(13) "Box" 
        ["slug"]=> string(3) "b" 
        ["term_group"]=> int(0) ["term_taxonomy_id"]=> int(26) 
        ["taxonomy"]=> string(16) "pa_shopping-pack" 
        ["description"]=> string(0) "" 
        ["parent"]=> int(0) 
        ["count"]=> int(1) 
        ["filter"]=> string(3) "raw" 
        ["meta_value"]=> string(1) "1" 
    }
    [۱]=> object(WP_Term)#20750 (11) {
        ["term_id"]=> int(25) 
        ["name"]=> string(12) "Kilos" 
        ["slug"]=> string(4) "k" 
        ["term_group"]=> int(0) 
        ["term_taxonomy_id"]=> int(25) 
        ["taxonomy"]=> string(16) "pa_shopping-pack" 
        ["description"]=> string(0) "" 
        ["parent"]=> int(0) ["count"]=> int(1) 
        ["filter"]=> string(3) "raw" 
        ["meta_value"]=> string(1) "2" 
    } 
    [۲]=> object(WP_Term)#20749 (11) {
        ["term_id"]=> int(24) 
        ["name"]=> string(13) "Single" 
        ["slug"]=> string(6) "s" 
        ["term_group"]=> int(0) 
        ["term_taxonomy_id"]=> int(24) 
        ["taxonomy"]=> string(16) "pa_shopping-pack" 
        ["description"]=> string(0) "" 
        ["parent"]=> int(0) 
        ["count"]=> int(2) 
        ["filter"]=> string(3) "raw" 
        ["meta_value"]=> string(1) "3" 
    }
    [۳]=> object(WP_Term)#20748 (11) { 
        ["term_id"]=> int(73) 
        ["name"]=> string(13) "Packed" 
        ["slug"]=> string(6) "p" 
        ["term_group"]=> int(0) 
        ["term_taxonomy_id"]=> int(73) 
        ["taxonomy"]=> string(16) "pa_shopping-pack" 
        ["description"]=> string(0) "" 
        ["parent"]=> int(0) 
        ["count"]=> int(1) 
        ["filter"]=> string(3) "raw" 
        ["meta_value"]=> string(1) "4" 
    }
}

How can i store all ["name"]s inside of ["product-size"] and ["shopping-pack"] in two separate simple arrays with the values of "slug" as key and "name" as value?

I've searched before and tried. the solutions didn't work.

  • 写回答
  • 关注问题
  • 收藏
  • 邀请回答

2条回答 默认 最新

  • du9843 2018-06-17 08:45
    已采纳

    You can use array_column

    $product = array_column($myVar['product-size'],'name','slug');
    print_r($product);
    
    $shopping = array_column($myVar['shopping-pack'],'name','slug');
    print_r($shopping);
    

    http://php.net/manual/en/function.array-column.php

    已采纳该答案
    打赏 评论
  • douzong5057 2018-06-17 08:54

    Maybe this help to you:

    $arr = "your array output";
    $count_prod = count($arr['f_shopping']['product-size']);
    $count_shopp = count($arr['f_shopping']['shopping-pack']);
    $newArray = array();
    
    for ($i = 0; $i < $count_prod; $i++) {
        $slug = $arr['f_shopping']['product-size'][$i]['slug'];
        $name = $arr['f_shopping']['product-size'][$i]['name'];
        $newArray[]($slug -> $name);
    }
    for ($i = 0; $i < $count_shopp; $i++) {
        $slug = $arr['f_shopping']['shopping-pack'][$i]['slug'];
        $name = $arr['f_shopping']['shopping-pack'][$i]['name'];
        $newArray[]($slug -> $name);
    }
    
    print_r($newArray);

    </div>
    
    打赏 评论

相关推荐 更多相似问题