【问题标题】:PHP: Filtering multidimensional arraysPHP:过滤多维数组
【发布时间】:2015-03-29 11:11:00
【问题描述】:

我正在尝试过滤以下多维数组:

Array ( 
        [0] => stdClass Object ( [term_id] => 5 [name] => Freelance [slug] => freelance-category1 [term_group] => 0 [term_taxonomy_id] => 5 [taxonomy] => job_listing_type [description] => [parent] => 0 [count] => 0 ) 

        [1] => stdClass Object ( [term_id] => 2 [name] => Full Time [slug] => full-time-category2 [term_group] => 0 [term_taxonomy_id] => 2 [taxonomy] => job_listing_type [description] => [parent] => 0 [count] => 0 ) 

        [2] => stdClass Object ( [term_id] => 6 [name] => Internship [slug] => internship-category1 [term_group] => 0 [term_taxonomy_id] => 6 [taxonomy] => job_listing_type [description] => [parent] => 0 [count] => 0 ) 

        [3] => stdClass Object ( [term_id] => 3 [name] => Parta Time [slug] => part-time-category2 [term_group] => 0 [term_taxonomy_id] => 3 [taxonomy] => job_listing_type [description] => [parent] => 0 [count] => 0 ) 

        [4] => stdClass Object ( [term_id] => 4 [name] => Temporary [slug] => temporary-category1 [term_group] => 0 [term_taxonomy_id] => 4 [taxonomy] => job_listing_type [description] => [parent] => 0 [count] => 0 ) )

我想将 [slug] => something-category1 的每个项目过滤到一个不同的数组中,并将 [slug] => something-category2 到一个不同的数组中。过滤的标准是hip-fun之后的结尾部分。

任何帮助将不胜感激。

【问题讨论】:

  • 试试foreachexplode

标签: php arrays multidimensional-array


【解决方案1】:

这里没有什么好建议的。只需迭代对象数组,在 slug 值中隔离目标子字符串,然后将任何符合条件的对象路由到它们各自的新组。

代码:(Demo)

$cat1 = [];
$cat2 = [];

foreach ($objects as $object) {
    if ($dash = strrpos($object->slug, '-')) {  // don't need to check for false-vs-0 due to task logic
        $cat = substr($object->slug, ++$dash);  // or $dash + 1 ...same thing
        if ($cat == 'category1') {
            $cat1[] = $object;
        } elseif ($cat == 'category2') {
            $cat2[] = $object;
        }
    }
}
echo "Category1 : ";
var_export($cat1);
echo "\n---\n";
echo "Category2 : ";
var_export($cat2);

输出:

Category1 : array (
  0 => 
  (object) array(
     'term_id' => 5,
     'name' => 'Freelance',
     'slug' => 'freelance-category1',
     'term_group' => 0,
     'term_taxonomy_id' => 5,
     'taxonomy' => 'job_listing_type',
     'description' => NULL,
     'parent' => 0,
     'count' => 0,
  ),
  1 => 
  (object) array(
     'term_id' => 6,
     'name' => 'Internship',
     'slug' => 'internship-category1',
     'term_group' => 0,
     'term_taxonomy_id' => 6,
     'taxonomy' => 'job_listing_type',
     'description' => NULL,
     'parent' => 0,
     'count' => 0,
  ),
  2 => 
  (object) array(
     'term_id' => 4,
     'name' => 'Temporary',
     'slug' => 'temporary-category1',
     'term_group' => 0,
     'term_taxonomy_id' => 4,
     'taxonomy' => 'job_listing_type',
     'description' => NULL,
     'parent' => 0,
     'count' => 0,
  ),
)
---
Category2 : array (
  0 => 
  (object) array(
     'term_id' => 2,
     'name' => 'Full Time',
     'slug' => 'full-time-category2',
     'term_group' => 0,
     'term_taxonomy_id' => 2,
     'taxonomy' => 'job_listing_type',
     'description' => NULL,
     'parent' => 0,
     'count' => 0,
  ),
  1 => 
  (object) array(
     'term_id' => 3,
     'name' => 'Parta Time',
     'slug' => 'part-time-category2',
     'term_group' => 0,
     'term_taxonomy_id' => 3,
     'taxonomy' => 'job_listing_type',
     'description' => NULL,
     'parent' => 0,
     'count' => 0,
  ),
)

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 2012-12-04
    • 2018-07-07
    • 2019-11-20
    • 1970-01-01
    • 2020-03-27
    • 2021-07-14
    • 1970-01-01
    相关资源
    最近更新 更多