php - 在不使用 foreach 的情况下获取标准类值

标签 php arrays object

首先感谢所有阅读这篇文章的人。 这是问题。 我正在尝试从此类别数组下的这两个标准类中获取 category_id,并将它们添加到另一个数组中,例如

$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] => 
        )

)

最佳答案

您可以将 array_map 与回调函数一起使用。在不了解您的代码的情况下,我可以假设您可以尝试这样的事情:

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

如果您有一个数组数组要映射,您还可以研究 call_user_func_array。我自己仍在学习 PHP,但我认为这应该有所帮助

关于php - 在不使用 foreach 的情况下获取标准类值,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/47862086/

相关文章:

css - 将 CMS 链接添加到导航 - 强制激活状态

php - 找不到类 'Mpdf\Mpdf'

php - 如果键不存在/不真实,则通过键或假值从数组中获取值

javascript - 查找匿名 Javascript 对象

php - 更改日期格式-PHP/SQL

java - 通过 JSON 将 Android 应用程序连接到 PHP 服务器 : Socket Exception

java - 从用户那里获取输入并将其存储在 Java 中的字符串数组中

javascript - 如何按值合并对象并更改属性类型?

javascript - Java Nashorn 相当于在 Javascript 中将对象推送到数组

c# - 应用程序之间的对象共享?