2017-12-18 212 views
3

首先,我要感谢所有阅读此帖的人。 这里是问题。 我想从这两性病类,它是根据本类别阵列得到CATEGORY_ID并将它们添加到另一个数组一样在不使用foreach的情况下获取std类值

$categories = array(75,65); 

反正是有,我可以让他们在不使用的foreach? 它已经在foreach中,我需要这个类别来继续下一步。

[categories] => Array 
(
    [0] => stdClass Object 
     (
      [product_category_id] => 83125 
      [category_id] => 75 
      [product_id] => 10836 
      [ordering] => 2 
      [category_parent_id] => 51 
      [category_type] => product 
      [category_name] => Men boxer 
      [category_description] => 
      [category_published] => 1 
      [category_ordering] => 3 
      [category_left] => 34 
      [category_right] => 35 
      [category_depth] => 5 
      [category_namekey] => product_1494167920_122781373 
      [category_created] => 1494167920 
      [category_modified] => 1502169524 
      [category_access] => all 
      [category_menu] => 0 
      [category_keywords] => men swimsuit, men swimming pants, swimsuit, men swimming suit, long sleeve swimming suit 
      [category_meta_description] => Buy men swimming suit at xxxxx, men long sleeve swimsuit and swimming boxer available, Buy 2 FREE shipping 
      [category_layout] => 
      [category_page_title] => Men Swim Boxer - xxxxx 
      [category_alias] => men-boxer 
      [category_site_id] => 
      [category_canonical] => 
      [category_quantity_layout] => 
      [hasbought] => 
     ) 

    [1] => stdClass Object 
     (
      [product_category_id] => 83124 
      [category_id] => 65 
      [product_id] => 10836 
      [ordering] => 43 
      [category_parent_id] => 43 
      [category_type] => product 
      [category_name] => Accessory 
      [category_description] => 
      [category_published] => 1 
      [category_ordering] => 5 
      [category_left] => 87 
      [category_right] => 114 
      [category_depth] => 4 
      [category_namekey] => product_1476761078_750345878 
      [category_created] => 1476761078 
      [category_modified] => 1502169605 
      [category_access] => all 
      [category_menu] => 0 
      [category_keywords] => accessory, beach accessory, swimming accessory, xxxx 
      [category_meta_description] => Shop accessory at xxxx, beach accessory, fashion accessory, gym accessory and many more, Free shipping & speed delivery 
      [category_layout] => 
      [category_page_title] => Accessory - xxxxxxx 
      [category_alias] => accessory 
      [category_site_id] => 
      [category_canonical] => 
      [category_quantity_layout] => 
      [hasbought] => 
     ) 

) 
+0

为什么你不想在foreach中使用另一个foreach? – Geshode

+0

@Geshode我担心它会拖动处理速度。 –

+0

也许试试看,如果它大大降低速度或不。 – Geshode

回答

0

您可以使用带有回调函数的array_map。不知道更多关于你的代码,我想你可以尝试这样的:

function get_category_id($category) { 
    return $category['category_id']; 
    } 
    $category_id = array_map('get_category_id', $categories); 

您可能还调查call_user_func_array,如果你有一个数组的数组映射通过。我仍然学习PHP自己,但我认为这应该有帮助

0

在我看来,这是一个array_reduce的工作。每次迭代都会添加到阵列中。

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

<?php 
$data = makeSomeData(); 

$categories = array_reduce($data, function($carry, $item){ 
    $carry[] = $item->category_id; 
    return $carry; 
}, []); 

print_r($categories); 
echo "\n"; 


function makeSomeData() { 
    $input = [65, 165, 265, 365]; 
    $data = []; 
    foreach($input as $id) { 
     $a = new stdClass; 
     $a->category_id = $id; 
     $data[] = $a;   
    } 
    return $data; 
} 
0

不知道关于你提到的数组或对象在foreach,但对你的情况下,只要做到这一点

$catesTmp[] = array_map(function($item){ 
    return !empty($item->category_id) ? $item->category_id : null; 
}, $categories); 

它会返回你需要的结果。

相关问题