can
can

Reputation: 33

Remove multidimension array elements which are bigger than X

I have array like this;

 Array
(
    [0] => stdClass Object
        (
            [selling] => 4.0107
            [update_date] => 1522271815
        )

    [1] => stdClass Object
        (
            [selling] => 4.0124
            [update_date] => 1522271876
        )

I want to print only selling elements which elements update_date bigger than 1522271876

I can get only selling elements with this $datay1 = array_column($results, 'selling'); but how can i get only SELLING elements update_date bigger than 1522271876 ?

Thanks

EDIT: I used these codes

$contents = file_get_contents('https://.com/api/v1/currencies/USD/daily'); 
$contents = utf8_encode($contents); 
$results = json_decode($contents, true); 

$filtered_array = array_filter($results, function($obj){
    if (isset($obj->update_date)) {
        if ($obj->update_date > 1522271876) return true;
    }
    return false;
});
print '<pre>';
print_r($filtered_array);
print '</pre>';

Upvotes: 0

Views: 41

Answers (2)

A l w a y s S u n n y
A l w a y s S u n n y

Reputation: 38542

You need to try something like this with array_filter() function. I've created the whole array of object for you and applied array_filter() with the update_date > 1522271876 condition.

<?php
$array = [];
$obj1 = new stdClass;
$obj1->selling = 4.0107;
$obj1->update_date = 1522271815;

$obj2 = new stdClass;
$obj2->selling = 4.0124;
$obj2->update_date = 1522271876;

$obj3 = new stdClass;
$obj3->selling = 4.0129;
$obj3->update_date = 1522271980;
$array = [$obj1,$obj2,$obj3];
print_r($array);
$filtered_array = array_filter($array, function($obj){
    if (isset($obj->update_date)) {
        if ($obj->update_date > 1522271876) return true;
    }
    return false;
});
print '<pre>';
print_r($filtered_array);
print '</pre>';
?>

INITIAL ARRAY OF OBJECT

Array
(
    [0] => stdClass Object
        (
            [selling] => 4.0107
            [update_date] => 1522271815
        )

    [1] => stdClass Object
        (
            [selling] => 4.0124
            [update_date] => 1522271876
        )

    [2] => stdClass Object
        (
            [selling] => 4.0129
            [update_date] => 1522271980
        )

)

OUTPUT AFTER FILTER:

Array
(
    [2] => stdClass Object
        (
            [selling] => 4.0129
            [update_date] => 1522271980
        )

)

DEMO: https://eval.in/980839

EDIT: It is just multidimensional array i.e array of array but on your question you put array of object, So now the code will be like this as I seen your API response. So dont use -> , instead use [ ]

$contents=file_get_contents('https://doviz.com/ap i/v1/curren cies/USD/daily'); $results = json_decode($contents, true);

$filtered_array = array_filter($results, 
function($arr){
if (isset($arr['update_date'])) {
    if ($arr['update_date'] > 1522271876)
     return  true;
}
     return false;
});

print '<pre>';
print_r($filtered_array);
print '</pre>';

Upvotes: 0

Abhinav Bajpai
Abhinav Bajpai

Reputation: 26

You can use array_filter to apply a callback that filters down your array.

You can look at the docs here: http://php.net/manual/en/function.array-filter.php

Upvotes: 1

Related Questions