PHP: array_filter for an object?











up vote
1
down vote

favorite












I have an array that I need to filter for certain things, for example, I might only want records that have the day of the week as Friday. As far as I'm aware this has never worked but it's taking an object and using array_filter on it. Can this work? Is there a better way or a way to do this on with object?



public function filterByDow($object)
{
$current_dow=5;
return array_values(array_filter($object, function ($array) use ($current_dow) {
$array = (array) $array;
if(!empty($array['day_id']) && $array['day_id'] > -1){
if($array['day_id'] != $current_dow){
return false;
}
}
return true;
}));
}

$object = $this->filterByDow($object);


Sample data might be like:



$object = (object) array(['id' => '1', 'day_id' => 3], ['id' => '2', 'day_id' => 4]);









share|improve this question
























  • can u provide a sample data, so that i can simulate it in fiddler?
    – Borhan
    Nov 9 at 15:52










  • Sure. I've edited the question. thank you.
    – sdexp
    Nov 9 at 15:58






  • 1




    What exactly does not work with the given code? Why do you need to cast the inner array into an object?
    – Nico Haase
    Nov 9 at 15:59






  • 1




    If you have a laravel collection why the hell do you not use $filtered = $collection->filter(function ($value, $key) { return $value == 5; });
    – Blackbam
    Nov 9 at 16:16






  • 1




    laravel.com/docs/5.7/collections#method-filter :-) ?
    – Blackbam
    Nov 9 at 16:16

















up vote
1
down vote

favorite












I have an array that I need to filter for certain things, for example, I might only want records that have the day of the week as Friday. As far as I'm aware this has never worked but it's taking an object and using array_filter on it. Can this work? Is there a better way or a way to do this on with object?



public function filterByDow($object)
{
$current_dow=5;
return array_values(array_filter($object, function ($array) use ($current_dow) {
$array = (array) $array;
if(!empty($array['day_id']) && $array['day_id'] > -1){
if($array['day_id'] != $current_dow){
return false;
}
}
return true;
}));
}

$object = $this->filterByDow($object);


Sample data might be like:



$object = (object) array(['id' => '1', 'day_id' => 3], ['id' => '2', 'day_id' => 4]);









share|improve this question
























  • can u provide a sample data, so that i can simulate it in fiddler?
    – Borhan
    Nov 9 at 15:52










  • Sure. I've edited the question. thank you.
    – sdexp
    Nov 9 at 15:58






  • 1




    What exactly does not work with the given code? Why do you need to cast the inner array into an object?
    – Nico Haase
    Nov 9 at 15:59






  • 1




    If you have a laravel collection why the hell do you not use $filtered = $collection->filter(function ($value, $key) { return $value == 5; });
    – Blackbam
    Nov 9 at 16:16






  • 1




    laravel.com/docs/5.7/collections#method-filter :-) ?
    – Blackbam
    Nov 9 at 16:16















up vote
1
down vote

favorite









up vote
1
down vote

favorite











I have an array that I need to filter for certain things, for example, I might only want records that have the day of the week as Friday. As far as I'm aware this has never worked but it's taking an object and using array_filter on it. Can this work? Is there a better way or a way to do this on with object?



public function filterByDow($object)
{
$current_dow=5;
return array_values(array_filter($object, function ($array) use ($current_dow) {
$array = (array) $array;
if(!empty($array['day_id']) && $array['day_id'] > -1){
if($array['day_id'] != $current_dow){
return false;
}
}
return true;
}));
}

$object = $this->filterByDow($object);


Sample data might be like:



$object = (object) array(['id' => '1', 'day_id' => 3], ['id' => '2', 'day_id' => 4]);









share|improve this question















I have an array that I need to filter for certain things, for example, I might only want records that have the day of the week as Friday. As far as I'm aware this has never worked but it's taking an object and using array_filter on it. Can this work? Is there a better way or a way to do this on with object?



public function filterByDow($object)
{
$current_dow=5;
return array_values(array_filter($object, function ($array) use ($current_dow) {
$array = (array) $array;
if(!empty($array['day_id']) && $array['day_id'] > -1){
if($array['day_id'] != $current_dow){
return false;
}
}
return true;
}));
}

$object = $this->filterByDow($object);


Sample data might be like:



$object = (object) array(['id' => '1', 'day_id' => 3], ['id' => '2', 'day_id' => 4]);






php object laravel-5 array-filter






share|improve this question















share|improve this question













share|improve this question




share|improve this question








edited Nov 9 at 15:58

























asked Nov 9 at 15:44









sdexp

4092616




4092616












  • can u provide a sample data, so that i can simulate it in fiddler?
    – Borhan
    Nov 9 at 15:52










  • Sure. I've edited the question. thank you.
    – sdexp
    Nov 9 at 15:58






  • 1




    What exactly does not work with the given code? Why do you need to cast the inner array into an object?
    – Nico Haase
    Nov 9 at 15:59






  • 1




    If you have a laravel collection why the hell do you not use $filtered = $collection->filter(function ($value, $key) { return $value == 5; });
    – Blackbam
    Nov 9 at 16:16






  • 1




    laravel.com/docs/5.7/collections#method-filter :-) ?
    – Blackbam
    Nov 9 at 16:16




















  • can u provide a sample data, so that i can simulate it in fiddler?
    – Borhan
    Nov 9 at 15:52










  • Sure. I've edited the question. thank you.
    – sdexp
    Nov 9 at 15:58






  • 1




    What exactly does not work with the given code? Why do you need to cast the inner array into an object?
    – Nico Haase
    Nov 9 at 15:59






  • 1




    If you have a laravel collection why the hell do you not use $filtered = $collection->filter(function ($value, $key) { return $value == 5; });
    – Blackbam
    Nov 9 at 16:16






  • 1




    laravel.com/docs/5.7/collections#method-filter :-) ?
    – Blackbam
    Nov 9 at 16:16


















can u provide a sample data, so that i can simulate it in fiddler?
– Borhan
Nov 9 at 15:52




can u provide a sample data, so that i can simulate it in fiddler?
– Borhan
Nov 9 at 15:52












Sure. I've edited the question. thank you.
– sdexp
Nov 9 at 15:58




Sure. I've edited the question. thank you.
– sdexp
Nov 9 at 15:58




1




1




What exactly does not work with the given code? Why do you need to cast the inner array into an object?
– Nico Haase
Nov 9 at 15:59




What exactly does not work with the given code? Why do you need to cast the inner array into an object?
– Nico Haase
Nov 9 at 15:59




1




1




If you have a laravel collection why the hell do you not use $filtered = $collection->filter(function ($value, $key) { return $value == 5; });
– Blackbam
Nov 9 at 16:16




If you have a laravel collection why the hell do you not use $filtered = $collection->filter(function ($value, $key) { return $value == 5; });
– Blackbam
Nov 9 at 16:16




1




1




laravel.com/docs/5.7/collections#method-filter :-) ?
– Blackbam
Nov 9 at 16:16






laravel.com/docs/5.7/collections#method-filter :-) ?
– Blackbam
Nov 9 at 16:16














3 Answers
3






active

oldest

votes

















up vote
1
down vote



accepted










As from the comments the array is a laravel collection I guess the answer is:



$filtered = $collection->filter(function ($value, $key) { 
return $value['day_id'] == 5;
});


https://laravel.com/docs/5.7/collections#method-filter






share|improve this answer





















  • This is exactly what I was looking for. Thanks.
    – sdexp
    Nov 9 at 16:20


















up vote
1
down vote













try this



    <?php
$items = array(['id' => '1', 'day_id' => 3], ['id' => '2', 'day_id' => 5]);
function filterByDow($items, $dow = 5){
return array_filter($items, function($item) use ($dow) {
if($item['day_id'] == $dow){
return true;
}
});

}

$resultArr = filterByDow($items);
print_r($resultArr);
?>





share|improve this answer





















  • An object is passed to filterByDow therfore I do not know if this change in the code is allowed?
    – Blackbam
    Nov 9 at 16:14


















up vote
1
down vote













Try to create a collection and implement methods for filtering in it.



class Offer
{
private $dayId;

public function getDayId()
{
return $this->dayId;
}

public function setDayId($dayId)
{
return $this->dayId = $dayId;
}
}

class OfferCollection
{
const FRIDAY = 5;

static $dayIds = [
self::FRIDAY => 'Friday'
];

private $offers = ;

public function addOffer(Offer $offer)
{
$this->offers = $offer;
}

public function getOffersByDay($dayId)
{
$offers = ;

if (in_array($dayId, self::$dayIds)) {
foreach ($this->offers as $offer) {
if ($offer->getDayId == $dayId) $offers = $offer;
}
}

return $offers;
}
}





share|improve this answer





















  • Nice solution if it is not already existing :-)
    – Blackbam
    Nov 9 at 16:21










  • Already exists:)
    – Yaroslaw
    Nov 9 at 16:23











Your Answer






StackExchange.ifUsing("editor", function () {
StackExchange.using("externalEditor", function () {
StackExchange.using("snippets", function () {
StackExchange.snippets.init();
});
});
}, "code-snippets");

StackExchange.ready(function() {
var channelOptions = {
tags: "".split(" "),
id: "1"
};
initTagRenderer("".split(" "), "".split(" "), channelOptions);

StackExchange.using("externalEditor", function() {
// Have to fire editor after snippets, if snippets enabled
if (StackExchange.settings.snippets.snippetsEnabled) {
StackExchange.using("snippets", function() {
createEditor();
});
}
else {
createEditor();
}
});

function createEditor() {
StackExchange.prepareEditor({
heartbeatType: 'answer',
convertImagesToLinks: true,
noModals: true,
showLowRepImageUploadWarning: true,
reputationToPostImages: 10,
bindNavPrevention: true,
postfix: "",
imageUploader: {
brandingHtml: "Powered by u003ca class="icon-imgur-white" href="https://imgur.com/"u003eu003c/au003e",
contentPolicyHtml: "User contributions licensed under u003ca href="https://creativecommons.org/licenses/by-sa/3.0/"u003ecc by-sa 3.0 with attribution requiredu003c/au003e u003ca href="https://stackoverflow.com/legal/content-policy"u003e(content policy)u003c/au003e",
allowUrls: true
},
onDemand: true,
discardSelector: ".discard-answer"
,immediatelyShowMarkdownHelp:true
});


}
});














 

draft saved


draft discarded


















StackExchange.ready(
function () {
StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2fstackoverflow.com%2fquestions%2f53228919%2fphp-array-filter-for-an-object%23new-answer', 'question_page');
}
);

Post as a guest















Required, but never shown

























3 Answers
3






active

oldest

votes








3 Answers
3






active

oldest

votes









active

oldest

votes






active

oldest

votes








up vote
1
down vote



accepted










As from the comments the array is a laravel collection I guess the answer is:



$filtered = $collection->filter(function ($value, $key) { 
return $value['day_id'] == 5;
});


https://laravel.com/docs/5.7/collections#method-filter






share|improve this answer





















  • This is exactly what I was looking for. Thanks.
    – sdexp
    Nov 9 at 16:20















up vote
1
down vote



accepted










As from the comments the array is a laravel collection I guess the answer is:



$filtered = $collection->filter(function ($value, $key) { 
return $value['day_id'] == 5;
});


https://laravel.com/docs/5.7/collections#method-filter






share|improve this answer





















  • This is exactly what I was looking for. Thanks.
    – sdexp
    Nov 9 at 16:20













up vote
1
down vote



accepted







up vote
1
down vote



accepted






As from the comments the array is a laravel collection I guess the answer is:



$filtered = $collection->filter(function ($value, $key) { 
return $value['day_id'] == 5;
});


https://laravel.com/docs/5.7/collections#method-filter






share|improve this answer












As from the comments the array is a laravel collection I guess the answer is:



$filtered = $collection->filter(function ($value, $key) { 
return $value['day_id'] == 5;
});


https://laravel.com/docs/5.7/collections#method-filter







share|improve this answer












share|improve this answer



share|improve this answer










answered Nov 9 at 16:19









Blackbam

4,696113871




4,696113871












  • This is exactly what I was looking for. Thanks.
    – sdexp
    Nov 9 at 16:20


















  • This is exactly what I was looking for. Thanks.
    – sdexp
    Nov 9 at 16:20
















This is exactly what I was looking for. Thanks.
– sdexp
Nov 9 at 16:20




This is exactly what I was looking for. Thanks.
– sdexp
Nov 9 at 16:20












up vote
1
down vote













try this



    <?php
$items = array(['id' => '1', 'day_id' => 3], ['id' => '2', 'day_id' => 5]);
function filterByDow($items, $dow = 5){
return array_filter($items, function($item) use ($dow) {
if($item['day_id'] == $dow){
return true;
}
});

}

$resultArr = filterByDow($items);
print_r($resultArr);
?>





share|improve this answer





















  • An object is passed to filterByDow therfore I do not know if this change in the code is allowed?
    – Blackbam
    Nov 9 at 16:14















up vote
1
down vote













try this



    <?php
$items = array(['id' => '1', 'day_id' => 3], ['id' => '2', 'day_id' => 5]);
function filterByDow($items, $dow = 5){
return array_filter($items, function($item) use ($dow) {
if($item['day_id'] == $dow){
return true;
}
});

}

$resultArr = filterByDow($items);
print_r($resultArr);
?>





share|improve this answer





















  • An object is passed to filterByDow therfore I do not know if this change in the code is allowed?
    – Blackbam
    Nov 9 at 16:14













up vote
1
down vote










up vote
1
down vote









try this



    <?php
$items = array(['id' => '1', 'day_id' => 3], ['id' => '2', 'day_id' => 5]);
function filterByDow($items, $dow = 5){
return array_filter($items, function($item) use ($dow) {
if($item['day_id'] == $dow){
return true;
}
});

}

$resultArr = filterByDow($items);
print_r($resultArr);
?>





share|improve this answer












try this



    <?php
$items = array(['id' => '1', 'day_id' => 3], ['id' => '2', 'day_id' => 5]);
function filterByDow($items, $dow = 5){
return array_filter($items, function($item) use ($dow) {
if($item['day_id'] == $dow){
return true;
}
});

}

$resultArr = filterByDow($items);
print_r($resultArr);
?>






share|improve this answer












share|improve this answer



share|improve this answer










answered Nov 9 at 16:12









Borhan

1381212




1381212












  • An object is passed to filterByDow therfore I do not know if this change in the code is allowed?
    – Blackbam
    Nov 9 at 16:14


















  • An object is passed to filterByDow therfore I do not know if this change in the code is allowed?
    – Blackbam
    Nov 9 at 16:14
















An object is passed to filterByDow therfore I do not know if this change in the code is allowed?
– Blackbam
Nov 9 at 16:14




An object is passed to filterByDow therfore I do not know if this change in the code is allowed?
– Blackbam
Nov 9 at 16:14










up vote
1
down vote













Try to create a collection and implement methods for filtering in it.



class Offer
{
private $dayId;

public function getDayId()
{
return $this->dayId;
}

public function setDayId($dayId)
{
return $this->dayId = $dayId;
}
}

class OfferCollection
{
const FRIDAY = 5;

static $dayIds = [
self::FRIDAY => 'Friday'
];

private $offers = ;

public function addOffer(Offer $offer)
{
$this->offers = $offer;
}

public function getOffersByDay($dayId)
{
$offers = ;

if (in_array($dayId, self::$dayIds)) {
foreach ($this->offers as $offer) {
if ($offer->getDayId == $dayId) $offers = $offer;
}
}

return $offers;
}
}





share|improve this answer





















  • Nice solution if it is not already existing :-)
    – Blackbam
    Nov 9 at 16:21










  • Already exists:)
    – Yaroslaw
    Nov 9 at 16:23















up vote
1
down vote













Try to create a collection and implement methods for filtering in it.



class Offer
{
private $dayId;

public function getDayId()
{
return $this->dayId;
}

public function setDayId($dayId)
{
return $this->dayId = $dayId;
}
}

class OfferCollection
{
const FRIDAY = 5;

static $dayIds = [
self::FRIDAY => 'Friday'
];

private $offers = ;

public function addOffer(Offer $offer)
{
$this->offers = $offer;
}

public function getOffersByDay($dayId)
{
$offers = ;

if (in_array($dayId, self::$dayIds)) {
foreach ($this->offers as $offer) {
if ($offer->getDayId == $dayId) $offers = $offer;
}
}

return $offers;
}
}





share|improve this answer





















  • Nice solution if it is not already existing :-)
    – Blackbam
    Nov 9 at 16:21










  • Already exists:)
    – Yaroslaw
    Nov 9 at 16:23













up vote
1
down vote










up vote
1
down vote









Try to create a collection and implement methods for filtering in it.



class Offer
{
private $dayId;

public function getDayId()
{
return $this->dayId;
}

public function setDayId($dayId)
{
return $this->dayId = $dayId;
}
}

class OfferCollection
{
const FRIDAY = 5;

static $dayIds = [
self::FRIDAY => 'Friday'
];

private $offers = ;

public function addOffer(Offer $offer)
{
$this->offers = $offer;
}

public function getOffersByDay($dayId)
{
$offers = ;

if (in_array($dayId, self::$dayIds)) {
foreach ($this->offers as $offer) {
if ($offer->getDayId == $dayId) $offers = $offer;
}
}

return $offers;
}
}





share|improve this answer












Try to create a collection and implement methods for filtering in it.



class Offer
{
private $dayId;

public function getDayId()
{
return $this->dayId;
}

public function setDayId($dayId)
{
return $this->dayId = $dayId;
}
}

class OfferCollection
{
const FRIDAY = 5;

static $dayIds = [
self::FRIDAY => 'Friday'
];

private $offers = ;

public function addOffer(Offer $offer)
{
$this->offers = $offer;
}

public function getOffersByDay($dayId)
{
$offers = ;

if (in_array($dayId, self::$dayIds)) {
foreach ($this->offers as $offer) {
if ($offer->getDayId == $dayId) $offers = $offer;
}
}

return $offers;
}
}






share|improve this answer












share|improve this answer



share|improve this answer










answered Nov 9 at 16:18









Yaroslaw

684




684












  • Nice solution if it is not already existing :-)
    – Blackbam
    Nov 9 at 16:21










  • Already exists:)
    – Yaroslaw
    Nov 9 at 16:23


















  • Nice solution if it is not already existing :-)
    – Blackbam
    Nov 9 at 16:21










  • Already exists:)
    – Yaroslaw
    Nov 9 at 16:23
















Nice solution if it is not already existing :-)
– Blackbam
Nov 9 at 16:21




Nice solution if it is not already existing :-)
– Blackbam
Nov 9 at 16:21












Already exists:)
– Yaroslaw
Nov 9 at 16:23




Already exists:)
– Yaroslaw
Nov 9 at 16:23


















 

draft saved


draft discarded



















































 


draft saved


draft discarded














StackExchange.ready(
function () {
StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2fstackoverflow.com%2fquestions%2f53228919%2fphp-array-filter-for-an-object%23new-answer', 'question_page');
}
);

Post as a guest















Required, but never shown





















































Required, but never shown














Required, but never shown












Required, but never shown







Required, but never shown

































Required, but never shown














Required, but never shown












Required, but never shown







Required, but never shown







Popular posts from this blog

Schultheiß

Liste der Kulturdenkmale in Wilsdruff

Android Play Services Check