0

I have an array which contains some data. for example:

$data = array(
    'CATEGORY 1' => array(
        array('id'=> 0, 'name' => 'John', 'category' => 'CATEGORY1'),
        array('id'=> 1, 'name' => 'Jack', 'category' => 'CATEGORY1'),
        array('id'=> 2, 'name' => 'Jame', 'category' => 'CATEGORY1'),
    ),
    'CATEGORY 2' => array(
        array('id'=> 0, 'name' => 'Mile', 'category' => 'CATEGORY2'),
        array('id'=> 1, 'name' => 'Mike', 'category' => 'CATEGORY2'),
        array('id'=> 2, 'name' => 'Matt', 'category' => 'CATEGORY2'),
    )
);

Now how can I write a function like this:

if ($post ='CATEGORY 1') {
    // filter all the name where category => CATEGORY1
}

Expected output:

['John', 'Jack', 'Jame']

I have tried like so:

$filters = array(
    "id" => array(
        "filter" => FILTER_VALIDATE_INT,
        "flags" => FILTER_FORCE_ARRAY,
        "options" => array(
            "min_range" => 1,
            "max_range" => 120
        )
    ),
    "name" => array(
        "filter" => FILTER_CALLBACK,
        "options" => "ucwords"                   
    ),
    "category" => array(
        "filter" => FILTER_CALLBACK,
        "options" => "ucwords"
    )
);

print_r(filter_var_array($data, $filters));

But that returns me null. How can I search within the array with the condition where category is category1 and fetch all the names which belongs to that?

3
  • Simply use foreach loop Commented Oct 10, 2015 at 9:11
  • @Uchiha thanks i have actually tried so but do not know where to put the condition in foreach where category is category1 ... Commented Oct 10, 2015 at 9:14
  • This isn't a filtration task, this is an array access task. In other words, you don't need to remove anything, just access the isolated data set. $data['CATEGORY 1'] targets the sub array. array_column() can isolate the name values. Commented Nov 22, 2024 at 23:24

3 Answers 3

2
<?php
$data = array(

    'CATEGORY 1' => array(
        array('id'=> 0, 'name' => 'John', 'category' => 'CATEGORY1'),
        array('id'=> 1, 'name' => 'Jack', 'category' => 'CATEGORY1'),
        array('id'=> 2, 'name' => 'Jame', 'category' => 'CATEGORY1'),
    ),
    'CATEGORY 2' => array(
        array('id'=> 0, 'name' => 'Mile', 'category' => 'CATEGORY2'),
        array('id'=> 1, 'name' => 'Mike', 'category' => 'CATEGORY2'),
        array('id'=> 2, 'name' => 'Matt', 'category' => 'CATEGORY2'),
    )

);

$post = 'CATEGORY 1';
$pluck = 'name';

var_dump( array_reduce($data['CATEGORY 1'], function($result, $array) use ( $pluck ) {
    isset($array[$pluck]) && $result[] = $array[$pluck];
    return $result;
}, array()));
Sign up to request clarification or add additional context in comments.

Comments

2

Simply use foreach like as

$result = [];
foreach($data as $value){
  foreach($value as $k => $v){
      if($v['category'] == 'CATEGORY1')
        $result[] = $v['name'];
  }
}
print_r($result);

Demo

Comments

1

Bellow code will help you out

<?php
$data = array(

    'CATEGORY 1' => array(
        array('id'=> 0, 'name' => 'John', 'category' => 'CATEGORY1'),
        array('id'=> 1, 'name' => 'Jack', 'category' => 'CATEGORY1'),
        array('id'=> 2, 'name' => 'Jame', 'category' => 'CATEGORY1'),
    ),
    'CATEGORY 2' => array(
        array('id'=> 0, 'name' => 'Mile', 'category' => 'CATEGORY2'),
        array('id'=> 1, 'name' => 'Mike', 'category' => 'CATEGORY2'),
        array('id'=> 2, 'name' => 'Matt', 'category' => 'CATEGORY2'),
    )

);

$post='CATEGORY 1';
$elements=$data[$post];
$names=array();
foreach ($elements as $ele) {
$names[]=$ele['name'];
}

print_r($names);

click here get realtime output

Comments

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.