-1

Having the following array:

var products = [
    { title: "Product One", categories: ["one", "two"] },
    { title: "Product Two", categories: ["two", "three"] },
    { title: "Product Three", categories: ["three", "four"] }
];

How can I filter the array (I only want products which have category "two") so I get the desired result:

[
    { title: "Product One", categories: ["one", "two"] },
    { title: "Product Two", categories: ["two", "three"] }
]
3
  • php arrays look a bit different. Commented Aug 25, 2015 at 11:46
  • Try using - array_filter php.net/manual/en/function.array-filter.php Commented Aug 25, 2015 at 11:46
  • What have you tried? Add some examples of what you have tried before looking for someone to code for you. Commented Aug 25, 2015 at 11:46

4 Answers 4

1
$product = '[
    { title: "Product One", categories: ["one", "two"] },
    { title: "Product Two", categories: ["two", "three"] },
    { title: "Product Three", categories: ["three", "four"] }
]';

$productArray = json_decode($product);

foreach($productArray as $key => $value)
{
     if(!in_array('two', $value['categories']))
         unset($productArray[$key]);
}

$productJson = json_encode($productArray);

echo $productJson;
Sign up to request clarification or add additional context in comments.

2 Comments

Thanks, I used this solution! I know that it wasn't correct JSON, but I only wanted to clear which data I use. Furthermore I tried array_filter but couldn't get it to work.. Thanks again! :)
Even after correcting the malformed json string, this answer does not work: 3v4l.org/k84BM
1

Javascript:

products = products.filter(function(item){
               return item.categories.indexOf('two') > -1;
           });

PHP:

$products = array_filter(
    json_decode($products),
    function($val) {
        return in_array('two', $val->categories);
    }
);

But, first you need to correct your json input by double quoting the keys. Below is corrected:

[
    {
        "title": "Product One",
        "categories": [
            "one",
            "two"
        ]
    },
    {
        "title": "Product Two",
        "categories": [
            "two",
            "three"
        ]
    },
    {
        "title": "Product Three",
        "categories": [
            "three",
            "four"
        ]
    }
]

Comments

0
  1. Decode the payload into an array of objects
  2. Make iterated checks with in_array() and push qualifying rows into a new result array
  3. After looping, re-encode the data to be in JSON format.

Code: (Demo)

$products = json_decode('[
    { "title": "Product One", "categories": ["one", "two"] },
    { "title": "Product Two", "categories": ["two", "three"] },
    { "title": "Product Three", "categories": ["three", "four"] }
]');

$result = [];
foreach ($products as $row) {
    if (in_array('two', $row->categories)) {
        $result[] = $row;
    }
}
echo json_encode($result, JSON_PRETTY_PRINT);

Output:

[
    {
        "title": "Product One",
        "categories": [
            "one",
            "two"
        ]
    },
    {
        "title": "Product Two",
        "categories": [
            "two",
            "three"
        ]
    }
]

Comments

-1

Try to use array_filter http://php.net/manual/en/function.array-filter.php :

print_r(array_filter($array, "value"));

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.