-2
    $arr = [
        (Object)[
            'interval' => "2018-08-26",
            'product_id' => 12 ,
 'size' => 10 
        ],
        (Object)[
            'interval' => "2018-09-26",
            'product_id' => 26 ,
'size' => 25 
        ],
        (Object)[
            'interval' => "2018-07-26",
            'product_id' => 50 ,
'size' => 28 
        ],(Object)[
            'interval' => "2018-08-26",
            'product_id' => 60,
'size' => 89 
        ],
        (Object)[
            'interval' => "2018-12-26",
            'product_id' => 75,
'size' => 89 
        ],
        (Object)[
            'interval' => "2018-09-26",
            'product_id' => 98 ,
'size' => 89 
        ] 
    ];

I need only 12 and 60 product id. How can filter from this array (as only they has interval of Aug (08)).

dWinder
  • 11,597
  • 3
  • 24
  • 39
Pritesh Mahajan
  • 4,974
  • 8
  • 39
  • 64

2 Answers2

2

If your input are IDs and you want to get dates then try

$ids = [12,60];

$result = array_filter($arr, function($e) use($ids) {
    return in_array($e->product_id, $ids);
});

$dates = array_map(function($e) { return $e->interval; }, $result );

working example here

UPDATE

If your input is month number e.g. 08 and you wana to get IDs then try this

$result = array_filter($arr, function($e) {
    return substr($e->interval,5,2) == '08';
});

$ids = array_map(function($e) { return $e->product_id; }, $result );

working example here. The $result contains full objects so you can read project_is as well as size.

Kamil Kiełczewski
  • 85,173
  • 29
  • 368
  • 345
1

try this:

$arrays = array_filter($arrays,function($arr){
  return substr($arr->interval,5,2) == '08';
});

array_walk($arrays,function($obj){
  unset($obj->interval);
});
davidloper
  • 181
  • 1
  • 6