search within array PHP -
i have array contains data. 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 write function ---
if ($post ='category 1') { // filter name category => category1 } expected output -- 'john', 'jack', 'jame';
i have tried ---
$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 returns me null, how can search within array condition category category1 fetch names belongs ...
i know can dumb question, people sometime stuck on basic problem. hope knows answer.
bellow code 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 $ele) { $names[]=$ele['name']; } print_r($names);
click here realtime output
Comments
Post a Comment