Click here to Skip to main content
15,923,168 members
Please Sign up or sign in to vote.
1.00/5 (1 vote)
See more:
Hi ,
I have this array after parsing the JSON , How to get the title of array item only if category is Accessories
(
    [0] => Array
        (
            [name] => Souq
            [title] => Upto 70% Off
            [category] => Accessories
        )

    [1] => Array
        (
            [name] => Beardo.
            [title] => 65% OFF 
            [category] => Personal Care
        )

    [2] => Array
        (
            [name] => Fnp
            [title] => Rs.100 off
            [category] => Gifts & Flowers
        )
     [3] => Array
        (
            [name] => Dreamz
            [title] => Rs.50 off
            [category] => Accessories
        )
)


Desired Output :
Souq
Dreamz

What I have tried:

I am not getting idea which function to use to get desired result , I have parsed the JSON and got this data but I need to categorise the data .
Posted
Updated 3-Aug-18 7:24am

1 solution

First. Filter the array by condition, this will return the item in index 0 and 3
PHP
$filterArr = array_filter($products, function ($var) {
    return ($var['category'] == 'Accessories');
});


Then write a loop to print out the desire properties value
PHP
foreach($filterArr as $v){
  echo $v['name'] . '<br/>';
}


Output:
Souq
Dreamz

php - How to filter a two dimensional array by value - Stack Overflow[^]
Print Single Value Of Multi Dimensional Array in PHP - Stack Overflow[^]
 
Share this answer
 

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900