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 fetch all the names belongs to that ...
i know it can be a really dumb question, but people sometime get stuck on a really basic problem.
Hope someone knows the answer.
<?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()));
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
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
Related
I have 2 associative arrays.
$a1 = array(
'blue' =>
array('id' => 2365,
'level' => 1,
'name' => 'blue'),
'black' =>
array('id' => 478,
'level' => 1,
'name' => 'black'),
'green' =>
array('id' => 698,
'level' => 1,
'name' => 'green'),
'red' =>
array('id' => 169,
'level' => 1,
'name' => 'red')
$a2= array(
'green' =>
array('id' => 452,
'level' => 1,
'name' => 'green'),
'black' =>
array('id' => 124,
'level' => 1,
'name' => 'black'),
'red' =>
array('id' => 124,
'level' => 1,
'name' => 'red'),
'blue' =>
array('id' => 145,
'level' => 1,
'name' => 'blue')
);
I want to sort my second array in the first array key order. That means I want to sorry my array of blue, black, green, red.
My desired result like:
$a2= array(
'blue' =>
array('id' => 145,
'level' => 1,
'name' => 'blue'),
'black' =>
array('id' => 124,
'level' => 1,
'name' => 'black'),
'green' =>
array('id' => 452,
'level' => 1,
'name' => 'green'),
'red' =>
array('id' => 124,
'level' => 1,
'name' => 'red'),
);
For this I am using the code :
uasort($a2, function($a, $b) use ($a1) { return array_search($a['name'], $a1) - array_search($b['name'], $a1); }
But I didn't get my desired result.
Also I tried with this:
uksort($a2, function($key1, $key2) use ($a1) {
return (array_search($key1, $a1) - array_search($key2, $a1));
});
Another method: But again, I didn't get my desired result.
$price = array();
foreach ($a2 as $key => $row) {
$price[$key] = $row['name'];
}
array_multisort($price, SORT_DESC, $a1);
I just use the following concept:
$arr2ordered = array() ;
foreach (array_keys($a1) as $key) {
$arr2ordered[$key] = $a2[$key] ;
}
After that, I got my desired result.
I would like to set level to each array that has children key in nested array. More deep that array then more big the level value.
Here's what I'm expected:
<?php
$array = array(
array(
'id' => 1,
'children' => array(
array(
'id' => 6,
'children' => array(
array(
'id' => 7,
),
array(
'id' => 8,
)
),
// Expected
'level' => '2',
),
array(
'id' => 9,
)
),
// Expected
'level' => '1'
),
array(
'id' => 2,
'children' => array(
array(
'id' => 10,
),
array(
'id' => 13,
'children' => array(
array(
'id' => 14,
),
array(
'id' => 19,
'children' => array(
array(
'id' => 20,
),
array(
'id' => 21,
),
array(
'id' => 22,
),
array(
'id' => 23,
),
)
// Expected
'level' => '3'
)
),
// Expected
'level' => '2'
),
),
// Expected
'level' => '1'
)
);
So far I make a function to set the level that has children key in nested array. But, the result is not what I'm expected.
<?php
$array = array(
array(
'id' => 1,
'children' => array(
array(
'id' => 6,
'children' => array(
array(
'id' => 7,
),
array(
'id' => 8,
)
),
// Expected
// 'level' => '2',
),
array(
'id' => 9,
)
),
// Expected
// 'level' => '1'
),
array(
'id' => 2,
'children' => array(
array(
'id' => 10,
),
array(
'id' => 13,
'children' => array(
array(
'id' => 14,
),
array(
'id' => 19,
'children' => array(
array(
'id' => 20,
),
array(
'id' => 21,
),
array(
'id' => 22,
),
array(
'id' => 23,
),
)
// Reality result
// 'level' => '4'
)
),
// Reality result
// 'level' => '3'
),
),
// Reality result
// 'level' => '2'
)
);
As you can see in Reality result comment is level 2, level 3, and level 4. What I'm expected the result is level 1, level 2, level 3, level (n+1). When there's not any children in the deeper array then the level value must be reset to level 1.
Here's my code that get the reality result:
<?php
function updateTreeArray(&$array, $level = 1) {
foreach ($array as $key => &$value) {
if (isset($value['children'])) {
$value['level'] = $level;
$level = setLevel($value['children'], $level);
}
if (is_array($value)) {
updateTreeArray($value, $level);
}
}
return $array;
}
function setLevel($array, $level) {
foreach ($array as $key => &$value) {
if (isset($value['children'])) {
$level += 1;
setLevel($value['children'], $level);
}
}
return $level;
}
print_r(updateTreeArray($array));
Could you please help me what should I do to achieve the expected result? Thanks.
One recursive function is enough.
function updateTreeArray(&$array, $level = 1)
{
foreach($array as &$item) {
if (array_key_exists('children', $item) && is_array($item['children'])) {
$item['level'] = $level;
updateTreeArray($item['children'], $level + 1);
}
}
}
updateTreeArray($array);
print_r($array);
fiddle
Here is my array:
$list = array(
array('id' => 3, 'name' => 'Phones', 'parent_id' => 0,),
array('id' => 256, 'name' => 'Accessories', 'parent_id' => 0,),
array('id' => 308, 'name' => 'Appliances', 'parent_id' => 0,),
array('id' => 1057, 'name' => 'Smart', 'parent_id' => 0,),
array('id' => 1065, 'name' => 'Smart Phones', 'parent_id' => 3,),
array('id' => 1066, 'name' => 'Feature Phones', 'parent_id' => 3,),
array('id' => 1069, 'name' => 'Samsung', 'parent_id' => 1065,),
array('id' => 1070, 'name' => 'Apple', 'parent_id' => 1065,),
array('id' => 1072, 'name' => 'Apple', 'parent_id' => 1066,),
array('id' => 1075, 'name' => 'Tablets', 'parent_id' => 0,),
array('id' => 1076, 'name' => 'Samsung', 'parent_id' => 1066,),
array('id' => 1077, 'name' => 'All Brands', 'parent_id' => 1075,),
array('id' => 1078, 'name' => 'Samsung', 'parent_id' => 1077,),
array('id' => 1079, 'name' => 'Protector', 'parent_id' => 256,),
array('id' => 1080, 'name' => 'Power', 'parent_id' => 256,),
array('id' => 1081, 'name' => 'Cable', 'parent_id' => 256,),
array('id' => 1082, 'name' => 'Memory', 'parent_id' => 256,),
);
This is the code I categorized:
function quote_make_tree($list,$deep=1, $root = 0)
{
$tree = $packData = [];
foreach ($list as $row) {
$packData[$row['id']] = $row;
}
foreach ($packData as $key => $val) {
if ($val['parent_id'] == $root) {
$tree[] = &$packData[$key];
} else {
$packData[$val['parent_id']]['children'][] = &$packData[$key];
}
}
return $tree;
}
I want to add a deep parameter $deep to quote_make_tree to control the depth of classification.
The result I want:
If $deep = 1 Get only data with parent_id = 0:
$one = array(
0 => array('id' => 3, 'name' => 'Phones', 'parent_id' => 0,),
1 => array('id' => 256, 'name' => 'Accessories', 'parent_id' => 0,),
2 => array('id' => 308, 'name' => 'Appliances', 'parent_id' => 0,),
3 => array('id' => 1057, 'name' => 'Smart', 'parent_id' => 0,),
4 => array('id' => 1075, 'name' => 'Tablets', 'parent_id' => 0,),
);
If $deep = 2,Get level 2 classification below level 1 classification,if $deep=3,Get all 1,2,3 classifications
I have tried get all the data first, then delete the data according to $deep.here is demo,thanks folks
This might be easier to solve using a recursive method. This allows you to check the depth and if you want more details then it will call itself to add in more layers. Each time calling itself you just take one off the depth...
function quote_make_tree($list, $deep=1, $root = 0) {
$output = [];
foreach ( $list as $listItem ) {
if ( $listItem['parent_id'] == $root ) {
if ( $deep > 1 ) {
$listItem['children'] = quote_make_tree($list, $deep-1, $listItem['id']);
}
$output[] = $listItem;
}
}
return $output;
}
I have the array of persons:
array(
array(
'name' => 'John',
'id' => 1,
'mother_id' => 2,
'father_id' => 3
),
array(
'name' => 'Lucy',
'id' => 2,
'mother_id' => 5,
'father_id' => 4
),
array(
'name' => 'Jim',
'id' => 3,
'mother_id' => 7,
'father_id' => 9
),
array(
'name' => 'Paul',
'id' => 4,
'mother_id' => 534,
'father_id' => 54
),
array(
'name' => 'Laura',
'id' => 5,
'mother_id' => 554,
'father_id' => 51
),
array(
'name' => 'Vanessa',
'id' => 7,
'mother_id' => 5354,
'father_id' => 514
),
array(
'name' => 'Adam',
'id' => 9,
'mother_id' => 245354,
'father_id' => 514234
),
);
And I want to get this:
array(
array(
'person' => array(
'name' => 'John',
'id' => 1,
'mother_id' => 2,
'father_id' => 3
),
'parents' => array(
'mother' => array(
'person' => array(
'name' => 'Lucy',
'id' => 2,
'mother_id' => 5,
'father_id' => 4
),
'parents' => array(
'mother' => array(
'person' => array(
'name' => 'Laura',
'id' => 5,
'mother_id' => 554,
'father_id' => 51
),
'parents' => array(...)
),
'father' => array(
'person' => array(
'name' => 'Paul',
'id' => 4,
'mother_id' => 534,
'father_id' => 54
),
'parents' => array(...)
),
)
),
'father' => ...
)
So John is a child that has mother and father, father has mother and father, mother has mother and father and etc, and write a script that do something but not what I want exactly
function parseTree(& $tree, $root = null) {
$return = null;
foreach ($tree as $key=> $item){
if ($item['id'] == $root){
$return = [
'person' => $item,
'parents' => [
'father' => parseTree($tree, $item['father_id'])
]
];
unset ($tree[$key]);
}elseif($item['id'] == $root){
$return = [
'person' => $item,
'parents' => [
'father' => parseTree($tree, $item['mother_id'])
]
];
unset ($tree[$key]);
}
elseif ($root == null) {
$return = [
'person' => $item,
'parents' => [
'father' => parseTree($tree, $item['father_id']),
'mother' => parseTree($tree, $item['mother_id'])
]
];
unset ($tree[$key]);
}
}
return $return;
}
How can I make it work? Or maybe there are some proper libraries for that?
You almost had it. I would do it in this manner:
function parseTree(&$tree, $root = null)
{
$return = null;
foreach ($tree as $key => $item) {
if ($root == null || $item['id'] == $root) {
$return = [
'person' => $item,
'parents' => [
'father' => parseTree($tree, $item['father_id']),
'mother' => parseTree($tree, $item['mother_id'])
]
];
unset ($tree[$key]);
}
}
return $return;
}
How can I sort an associative array by a weight AND type?
Input
array(
'a' => array( 'type' => 't1', 'weight' => 1, 'text' => 'text1' ),
'b' => array( 'type' => 't1', 'weight' => 3, 'text' => 'text2' ),
'c' => array( 'type' => 't2', 'weight' => 5, 'text' => 'text3' ),
'd' => array( 'type' => 't1', 'weight' => 2, 'text' => 'text4' ),
'e' => array( 'type' => 't2', 'weight' => 4, 'text' => 'text5' ),
'f' => array( 'type' => 't2', 'weight' => 4, 'text' => 'text6' )
);
Desired Output
array(
'a' => array( 'type' => 't1', 'weight' => 1, 'text' => 'text1' ),
'd' => array( 'type' => 't1', 'weight' => 2, 'text' => 'text4' ),
'b' => array( 'type' => 't1', 'weight' => 3, 'text' => 'text2' ),
'e' => array( 'type' => 't2', 'weight' => 1, 'text' => 'text5' ),
'f' => array( 'type' => 't2', 'weight' => 1, 'text' => 'text6' ),
'c' => array( 'type' => 't2', 'weight' => 5, 'text' => 'text3' )
);
Type "t2" must appear at end of array, all other types at start.
Weight must be sorted after type.
I am using uasort with a custom compare function, but am struggling. Here is what I have, but it doesn't work:
function my_comparer($a, $b) {
return ( $a['type'] !== 't2' && $b['type'] === 't2' )
? -1
: $a['weight'] - $b['weight'];
}
Your function doesn't take account of ($a['type']=='t2')
function my_comparer($a, $b) {
if ( ($a['type']==='t2') && ($b['type']!=='t2')) return -1;
if ( ($b['type']==='t2') && ($a['type']!=='t2')) return 1;
return ($a['weight'] - $b['weight']);
}
Try this:
function my_comparer($a, $b) {
if( $a['type'] == $b['type'] ){
return $a['weight'] - $b['weight'];
}else{
if( $a['type'] > $b['type'] ) return 1;
else return -1;
}
}
(warning, this is untested)
Simpler way would be array_multisort
$data = array(
'a' => array( 'type' => 't1', 'weight' => 1, 'text' => 'text1' ),
'b' => array( 'type' => 't1', 'weight' => 3, 'text' => 'text2' ),
'c' => array( 'type' => 't2', 'weight' => 5, 'text' => 'text3' ),
'd' => array( 'type' => 't1', 'weight' => 2, 'text' => 'text4' ),
'e' => array( 'type' => 't2', 'weight' => 4, 'text' => 'text5' ),
'f' => array( 'type' => 't2', 'weight' => 4, 'text' => 'text6' )
);
// Obtain a list of columns
foreach ($data as $key => $row) {
$type[$key] = $row['type'];
$weight[$key] = $row['weight'];
}
array_multisort($type, SORT_ASC, $weight, SORT_ASC, $data);
Plus this works if you add any number of types you want sorted in the same way.
Helper function:
function arrayColumnSort(&$array, $directions) {
// collect columns
$columnNames = array_keys($directions);
$columns = array();
foreach ($array as $row) {
foreach ($columnNames as $columnName) {
if (!isset($columns[$columnName])) {
$columns[$columnName] = array();
}
$columns[$columnName][] = isset($row[$columnName]) ? $row[$columnName] : null;
}
}
// build array_multisort params
$params = array();
foreach ($directions as $columnName => $direction) {
$params = array_merge(
$params,
array($columns[$columnName]),
is_array($direction) ? $direction : array($direction)
);
}
$params[] =& $array;
// sort
call_user_func_array('array_multisort', $params);
}
Call:
arrayColumnSort($data, array(
'type' => SORT_ASC,
'weight' => SORT_ASC,
));