I need a little help. My knowledge of algorithms are weak. I can not write a recursive function in PHP that returns all the latest children.
Suppose our array looks like this:
Array
(
[0] => Array
(
[id_category] => 1
[name] => Accueil
[id_parent] => 0
)
[1] => Array
(
[id_category] => 2
[name] => Accessoires
[id_parent] => 1
)
[2] => Array
(
[id_category] => 3
[name] => Merchandising
[id_parent] => 1
)
[3] => Array
(
[id_category] => 4
[name] => Pièces détachées
[id_parent] => 1
)
[4] => Array
(
[id_category] => 5
[name] => Excavateur
[id_parent] => 4
)
[5] => Array
(
[id_category] => 6
[name] => série 100
[id_parent] => 5
)
[6] => Array
(
[id_category] => 7
[name] => above
[id_parent] => 6
)
[7] => Array
(
[id_category] => 8
[name] => système hydraulique
[id_parent] => 7
)
[8] => Array
(
[id_category] => 9
[name] => série 200
[id_parent] => 5
)
[9] => Array
(
[id_category] => 10
[name] => thru
[id_parent] => 6
)
[10] => Array
(
[id_category] => 11
[name] => Compaction
[id_parent] => 4
)
[11] => Array
(
[id_category] => 12
[name] => système électrique
[id_parent] => 7
)
)
I would like getLastChildren (5) or getLastChildren (6) or getLastChildren (7), the function returns me an answer array ("8", "12")
I will try to give an example.
If I take the category 5: = 6 and 9 are the children.
I look through children. The child 6 has two children, 7 and 10, Child 9: no children.
I put 9 in the list of children.
The child 7 has two children, 8 and 12. 8 has no children. 12 has no children. I add 8 and 12.
So we return (9,8,12) 10 has no children. I also added.
In the end I (9,8,12,10)
So what I would do, if I search "all the last children in category 7" => 8, and 12. I hope my explanation is "a little clearer."
I would do something like that:
$array = array(
array (
'id_category' => 1,
'name' => 'Accueil',
'id_parent' => 0,
),
array (
'id_category' => 2,
'name' => 'Accessoires',
'id_parent' => 1,
),
array (
'id_category' => 3,
'name' => 'Merchandising',
'id_parent' => 1,
),
array (
'id_category' => 4,
'name' => 'Pièces détachées',
'id_parent' => 1,
),
array (
'id_category' => 5,
'name' => 'Excavateur',
'id_parent' => 4,
),
array (
'id_category' => 6,
'name' => 'série 100',
'id_parent' => 5,
),
array (
'id_category' => 7,
'name' => 'above',
'id_parent' => 6,
),
array (
'id_category' => 8,
'name' => 'système hydraulique',
'id_parent' => 7,
),
array (
'id_category' => 9,
'name' => 'série 200',
'id_parent' => 5,
),
array (
'id_category' => 10,
'name' => 'thru',
'id_parent' => 6,
),
array (
'id_category' => 11,
'name' => 'Compaction',
'id_parent' => 4,
),
array (
'id_category' => 12,
'name' => 'système électrique',
'id_parent' => 7,
),
);
I split data and code, here is the code:
$children = array();
function getLastChildren($array, $parent) {
global $children;
foreach ($array as $key => $value) {
if ($value['id_parent'] == $parent) {
if (hasChild($array, $value['id_category'])) {
getLastChildren($array, $value['id_category']);
} else {
$children[] = $value['id_category'];
}
}
}
}
function hasChild($array, $parent) {
foreach ($array as $key => $value) {
if ($value['id_parent'] == $parent) {
return true;
}
}
return false;
}
getLastChildren($array, 5);
print_r($children);
output:
Array
(
[0] => 8
[1] => 12
[2] => 10
[3] => 9
)
Related
I have an array
$arr = [
[
'id' => 10,
'name' => 'John',
'occupation' => 'engineer',
'points' => 10
],
[
'id' => 10,
'name' => 'John',
'occupation' => 'librarian',
'points' => 14
],
[
'id' => 7,
'name' => 'Sarah',
'occupation' => 'artist',
'points' => 21
],
[
'id' => 7,
'name' => 'Sarah',
'occupation' => 'teacher',
'points' => 17
],
[
'id' => 10,
'name' => 'John',
'occupation' => 'butcher',
'points' => 7
],
[
'id' => 7,
'name' => 'Sarah',
'occupation' => 'engineer',
'points' => 9
],
[
'id' => 25,
'name' => 'Andreea',
'occupation' => 'judge',
'points' => 11
]
];
And I use this built in functions to get unique ids:
$people = array_column($arr, 'id', 'id');
And then I use a foreach to get every occurrence of each user in the main array $arr:
foreach($people as $id){
$keys = array_keys(array_column($arr, 'id'), $id);
}
This is the return:
Array
(
[0] => 0
[1] => 1
[2] => 4
)
Array
(
[0] => 2
[1] => 3
[2] => 5
)
Array
(
[0] => 6
)
Now in order to build a small array for each person I could loop trough this small arrays that contain the keys from the main array and get the values and ending up with small slices.
But, how can I get the actual $arr values for each person instead of getting just the keys? (using as little resources as possible)
I need the result to be like this:
Array
(
[10] => Array
(
[0] => Array
(
[id] => 10
[name] => John
[occupation] => engineer
[points] => 10
)
[1] => Array
(
[id] => 10
[name] => John
[occupation] => librarian
[points] => 14
)
[2] => Array
(
[id] => 10
[name] => John
[occupation] => butcher
[points] => 7
)
)
[7] => Array
(
[0] => Array
(
[id] => 7
[name] => Sarah
[occupation] => artist
[points] => 21
)
[1] => Array
(
[id] => 7
[name] => Sarah
[occupation] => teacher
[points] => 17
)
[2] => Array
(
[id] => 7
[name] => Sarah
[occupation] => engineer
[points] => 9
)
)
[25] => Array
(
[0] => Array
(
[id] => 25
[name] => Andreea
[occupation] => judge
[points] => 11
)
)
)
P.S: I don't need to keep the key index the same.
You can do it in a more efficient way,Demo
$result = [];
foreach($arr as $v){
$result[$v["id"]][] = $v;
}
print_r($result);
use a function of group by like:
$byGroup = group_by("id", $arr);
function group_by($key, $data) {
$result = array();
foreach($array as $val) {
if(array_key_exists($key, $val)){
$result[$val[$key]][] = $val;
}else{
$result[""][] = $val;
}
}
return $result;
}
I have 2 arrays category and product_to_category (Structure shown below)
[category] => Array
(
[0] => 299
[2] => 342
[3] => 134
[4] => 333
[5] => 347
[9] => 296
)
And
[product_to_category] => Array
(
[0] => Array
(
[product_id] => 23895
[category_id] => 296
[relevance] => 77.73432159423828
)
[1] => Array
(
[product_id] => 17218
[category_id] => 296
[relevance] => 77.73432159423828
)
[2] => Array
(
[product_id] => 23896
[category_id] => 347
[relevance] => 77.73432159423828
)
....So On (5000+ records)
i have almost 5000+ records in product_to_category array, But i want to pick only 3 products from P2C array per Category. For Example category_id=299 so 3 products of 299 will be added to my new array and same for other categories.
Is there any possible way to do that .
Try this query to fetch record you wanted,
select p2c.product_id, p2c.category_id, p2c.relevance
from category c
LEFT JOIN product_to_category p2c ON p2c.category_id = category.category_id
GROUP BY p2c.product_id, p2c.category_id
HAVING count(*) <= 3
ORDER BY category_id, product_id
You can refer table category to category table and product_to_category to your table name in database.
$category = [0 => 299, 2 => 342, 3 => 134, 4 => 333, 5 => 347, 9 => 296];
$product_to_category = Array
(
0 => Array
(
'product_id' => 23895,
'category_id' => 296,
'relevance' => 77.73432159423828
),
1 => Array
(
'product_id' => 17218,
'category_id' => 296,
'relevance' => 77.73432159423828
),
2 => Array
(
'product_id' => 23896,
'category_id' => 347,
'relevance' => 77.73432159423828
),
3 => Array
(
'product_id' => 23897,
'category_id' => 296,
'relevance' => 77.73432159423828
),
4 => Array
(
'product_id' => 23899,
'category_id' => 296,
'relevance' => 77.73432159423828
),
5 => Array
(
'product_id' => 23894,
'category_id' => 347,
'relevance' => 77.73432159423828
),
6 => Array
(
'product_id' => 23892,
'category_id' => 347,
'relevance' => 77.73432159423828
),
7 => Array
(
'product_id' => 23833,
'category_id' => 347,
'relevance' => 77.73432159423828
)
);
$new_product_to_category = [];
foreach ($category as $categories) {
$count_category_ids = [];
$i=0;
foreach ($product_to_category as $product_to_categories){
if ($categories == $product_to_categories['category_id']){
$count_category_ids[] = $product_to_categories['category_id'];
if (count($count_category_ids) < 4) {
$new_product_to_category[$i]['product_id'] = $product_to_categories['product_id'];
$new_product_to_category[$i]['category_id'] = $product_to_categories['category_id'];
$new_product_to_category[$i]['relevance'] = $product_to_categories['relevance'];
}
}
$i++;
}
}
echo "<pre>";
print_r($new_product_to_category);
Output shows like this :
Array
(
[2] => Array
(
[product_id] => 23896
[category_id] => 347
[relevance] => 77.734321594238
)
[5] => Array
(
[product_id] => 23894
[category_id] => 347
[relevance] => 77.734321594238
)
[6] => Array
(
[product_id] => 23892
[category_id] => 347
[relevance] => 77.734321594238
)
[0] => Array
(
[product_id] => 23895
[category_id] => 296
[relevance] => 77.734321594238
)
[1] => Array
(
[product_id] => 17218
[category_id] => 296
[relevance] => 77.734321594238
)
[3] => Array
(
[product_id] => 23897
[category_id] => 296
[relevance] => 77.734321594238
)
)
$categories = array(6, 5, 3, 4, 2, 1);
$products = array(
array('product_id' => 231, 'category_id' => 1, 'relevance' => 321),
array('product_id' => 232, 'category_id' => 4, 'relevance' => 322),
array('product_id' => 233, 'category_id' => 2, 'relevance' => 323),
array('product_id' => 234, 'category_id' => 4, 'relevance' => 324),
array('product_id' => 235, 'category_id' => 4, 'relevance' => 325),
array('product_id' => 236, 'category_id' => 2, 'relevance' => 326),
array('product_id' => 237, 'category_id' => 1, 'relevance' => 327),
array('product_id' => 238, 'category_id' => 4, 'relevance' => 328),
array('product_id' => 239, 'category_id' => 1, 'relevance' => 329),
array('product_id' => 240, 'category_id' => 1, 'relevance' => 330)
);
$categoryWiseProduct = array();
foreach ($products as $p) {
foreach ($categories as $c) {
if ($c == $p['category_id']) {
if (isset($categoryWiseProduct[$c]) && count($categoryWiseProduct[$c]) > 2) {
break;
}
$categoryWiseProduct[$c][] = $p;
}
}
}
echo'<pre>';print_r($categoryWiseProduct);die;
Output will be:
Array
(
[1] => Array
(
[0] => Array
(
[product_id] => 231
[category_id] => 1
[relevance] => 321
)
[1] => Array
(
[product_id] => 237
[category_id] => 1
[relevance] => 327
)
[2] => Array
(
[product_id] => 239
[category_id] => 1
[relevance] => 329
)
)
[4] => Array
(
[0] => Array
(
[product_id] => 232
[category_id] => 4
[relevance] => 322
)
[1] => Array
(
[product_id] => 234
[category_id] => 4
[relevance] => 324
)
[2] => Array
(
[product_id] => 235
[category_id] => 4
[relevance] => 325
)
)
[2] => Array
(
[0] => Array
(
[product_id] => 233
[category_id] => 2
[relevance] => 323
)
[1] => Array
(
[product_id] => 236
[category_id] => 2
[relevance] => 326
)
)
)
try this code
$collection = array();
foreach($arr as $key => $value){
if(
isset($collection[$value['category_id']])
&&
count($collection[$value['category_id']]) >= 3
) continue;
$collection[$value['product_id']][] = $value['product_id'];
}
In this way, you will get 3 products from each category.
You can add more condition if you need, like check the product id you can use inside if condition
&& in_array($product_id, $collection[$value['category_id']])
I need help organizing my inventory array. The structure is one big inventory array, with array of items inside. Per item array consists of the following:
item, item_group, item_no and array sold. sold consists of inner arrays with dates and quantity. Now, I'm having trouble organizing it for my needed output. I'll give you guys sample of input and output. So please do check and it's very much appreciated.
Sample part of my $inventory array
Array
(
[0] => Array
(
[item] => NK
[item_group] => 5
[sold] => Array
(
[0] => Array
(
[quantity] => 11
[date] => 2017-10-28
)
[1] => Array
(
[quantity] => 1
[date] => 2017-10-29
)
)
[item_no] => 1
)
[1] => Array
(
[item] => FL
[item_group] => 5
[sold] => Array
(
[0] => Array
(
[quantity] => 7
[date] => 2017-10-28
)
[1] => Array
(
[quantity] => 2
[date] => 2017-10-29
)
)
[item_no] => 2
)
[2] => Array
(
[item] => AD
[item_group] => 5
[sold] => Array
(
[0] => Array
(
[quantity] => 5
[date] => 2017-10-28
)
[1] => Array
(
[quantity] => 3
[date] => 2017-10-29
)
)
[item_no] => 3
)
[3] => Array
(
[item] => CV
[item_group] => 5
[sold] => Array
(
[0] => Array
(
[quantity] => 4
[date] => 2017-10-28
)
[1] => Array
(
[quantity] => 6
[date] => 2017-10-29
)
)
[item_no] => 4
)
[4] => Array
(
[item] => NB
[item_group] => 5
[sold] => Array
(
[0] => Array
(
[quantity] => 12
[date] => 2017-10-28
)
[1] => Array
(
[quantity] => 4
[date] => 2017-10-29
)
)
[item_no] => 5
)
[5] => Array
(
[item] => SP
[item_group] => 5
[sold] => Array
(
[0] => Array
(
[quantity] => 4
[date] => 2017-10-28
)
[1] => Array
(
[quantity] => 6
[date] => 2017-10-29
)
)
[item_no] => 6
)
[6] => Array
(
[item] => WB
[item_group] => 5
[sold] => Array
(
[0] => Array
(
[quantity] => 5
[date] => 2017-10-28
)
[1] => Array
(
[quantity] => 2
[date] => 2017-10-29
)
)
[item_no] => 7
)
[7] => Array
(
[item] => wny
[item_group] => 12
[sold] => Array
(
[0] => Array
(
[quantity] => 4
[date] => 2017-10-28
)
[1] => Array
(
[quantity] => 6
[date] => 2017-10-29
)
)
[item_no] => 8
)
[8] => Array
(
[item] => bs
[item_group] => 12
[sold] => Array
(
[0] => Array
(
[quantity] => 15
[date] => 2017-10-28
)
[1] => Array
(
[quantity] => 2
[date] => 2017-10-29
)
)
[item_no] => 9
)
[9] => Array
(
[item] => st
[item_group] => 12
[sold] => Array
(
[0] => Array
(
[quantity] => 16
[date] => 2017-10-29
)
)
[item_no] => 10
)
[10] => Array
(
[item] => ayhtdws
[item_group] => 12
[sold] => Array
(
[0] => Array
(
[quantity] => 2
[date] => 2017-10-29
)
)
[item_no] => 11
)
[11] => Array
(
[item] => sif
[item_group] => 12
[sold] => Array
(
[0] => Array
(
[quantity] => 3
[date] => 2017-10-29
)
)
[item_no] => 12
)
[12] => Array
(
[item] => bb
[item_group] => 12
[sold] => Array
(
[0] => Array
(
[quantity] => 6
[date] => 2017-10-29
)
)
[item_no] => 13
)
)
From there, what I want to display is like this. Grouped by date ascending. And each item => quantity sold
Array
(
[0] => Array
(
[date] => 2017-10-28
[NK] => 11
[FL] => 7
[AD] => 5
[CV] => 4
[NB] => 12
[SP] => 4
[WB] => 5
[wny] => 4
[bs] => 15
)
[1] => Array
(
[date] => 2017-10-29
[NK] => 1
[FL] => 2
[AD] => 3
[CV] => 6
[NB] => 4
[SP] => 6
[WB] => 2
[wny] => 6
[bs] => 2
[st] => 16
[ayhtdws] => 2
[sif] => 3
[bb] => 6
)
)
I've spent almost 3 days figuring this out and up to this writing, I was only able to make it this far
$result = array();
$dates = array();
foreach ($inventory as $key => $item) {
foreach ($item['sold'] as $k => $v) {
array_push($dates, $v['date']);
}
}
$dates = array_unique($dates);
foreach($dates as $key => $value) {
array_push($result, array('date' => $value));
}
foreach ($dates as $rkey => $rvalue) {
foreach ($inventory as $key => $item) {
foreach ($item['sold'] as $k => $v) {
if ($v['date'] = $result[$key]['date']) {
array_push($result[$key][$item['item']] = $v['quantity']);
}
}
}
}
return $result;
Which of course gives me this sad result
Array
(
[0] => Array
(
[date] => 2017-10-28
[NK] => 1
)
[1] => Array
(
[date] => 2017-10-29
[FL] => 2
)
)
And to make things worse, we have this rule about cyclomatic complexities that we should only have at most 3 loop/conditions and up to 3 nesting levels per loop/conditions. And the whole organizing should not have any user created functions.
Even if not following the rules, I wasn't able to figure it out for days. Sorry if problem is long. Please help :(
Update: var_export($inventory) output
array (
0 =>
array (
'item' => 'NK',
'item_group' => '5',
'sold' =>
array (
0 =>
array (
'quantity' => '11',
'date' => '2017-10-28',
),
1 =>
array (
'quantity' => '1',
'date' => '2017-10-29',
),
),
'item_no' => '1',
),
1 =>
array (
'item' => 'FL',
'item_group' => '5',
'sold' =>
array (
0 =>
array (
'quantity' => '7',
'date' => '2017-10-28',
),
1 =>
array (
'quantity' => '2',
'date' => '2017-10-29',
),
),
'item_no' => '2',
),
2 =>
array (
'item' => 'AD',
'item_group' => '5',
'sold' =>
array (
0 =>
array (
'quantity' => '5',
'date' => '2017-10-28',
),
1 =>
array (
'quantity' => '3',
'date' => '2017-10-29',
),
),
'item_no' => '3',
),
3 =>
array (
'item' => 'CV',
'item_group' => '5',
'sold' =>
array (
0 =>
array (
'quantity' => '4',
'date' => '2017-10-28',
),
1 =>
array (
'quantity' => '6',
'date' => '2017-10-29',
),
),
'item_no' => '4',
),
4 =>
array (
'item' => 'NB',
'item_group' => '5',
'sold' =>
array (
0 =>
array (
'quantity' => '12',
'date' => '2017-10-28',
),
1 =>
array (
'quantity' => '4',
'date' => '2017-10-29',
),
),
'item_no' => '5',
),
5 =>
array (
'item' => 'SP',
'item_group' => '5',
'sold' =>
array (
0 =>
array (
'quantity' => '4',
'date' => '2017-10-28',
),
1 =>
array (
'quantity' => '6',
'date' => '2017-10-29',
),
),
'item_no' => '6',
),
6 =>
array (
'item' => 'WB',
'item_group' => '5',
'sold' =>
array (
0 =>
array (
'quantity' => '5',
'date' => '2017-10-28',
),
1 =>
array (
'quantity' => '2',
'date' => '2017-10-29',
),
),
'item_no' => '7',
),
7 =>
array (
'item' => 'wny',
'item_group' => '12',
'sold' =>
array (
0 =>
array (
'quantity' => '4',
'date' => '2017-10-28',
),
1 =>
array (
'quantity' => '6',
'date' => '2017-10-29',
),
),
'item_no' => '8',
),
8 =>
array (
'item' => 'bs',
'item_group' => '12',
'sold' =>
array (
0 =>
array (
'quantity' => '15',
'date' => '2017-10-28',
),
1 =>
array (
'quantity' => '2',
'date' => '2017-10-29',
),
),
'item_no' => '9',
),
9 =>
array (
'item' => 'st',
'item_group' => '12',
'sold' =>
array (
0 =>
array (
'quantity' => '16',
'date' => '2017-10-29',
),
),
'item_no' => '10',
),
10 =>
array (
'item' => 'ayhtdws',
'item_group' => '12',
'sold' =>
array (
0 =>
array (
'quantity' => '2',
'date' => '2017-10-29',
),
),
'item_no' => '11',
),
11 =>
array (
'item' => 'sif',
'item_group' => '12',
'sold' =>
array (
0 =>
array (
'quantity' => '3',
'date' => '2017-10-29',
),
),
'item_no' => '12',
),
12 =>
array (
'item' => 'bb',
'item_group' => '12',
'sold' =>
array (
0 =>
array (
'quantity' => '6',
'date' => '2017-10-29',
),
),
'item_no' => '13',
),
)
I don't know if this is more or less the same as Erwin's code, but I wrote this code 13 hours ago and had to wait for the array.
Edit; I have tested Erwin's code and it seems we have close to matching code.
He makes one loop more to get the date in there but it's more or less the same.
I loop the array and the subarray sold.
I make the new array key the date example:
Echo $new['2017-10-28']['nk']; // 11
And if the date key is not set already I create it.
Once the loop is done I use array_values to remove the date keys making the array look like:
Echo $new[0]['nk']; // 11
The code:
$new =[];
Foreach($inventory as $sub){
Foreach($sub["sold"] as $sold){
If (!isset($new[$sold["date"]]["date"])) $new[$sold["date"]]["date"] = $sold["date"];
$new[$sold["date"]][$sub["item"]] = $sold["quantity"];
}
}
$new = array_values($new);
Var_dump($new);
https://3v4l.org/mGJSX
I've run through some tests and workarounds and come up with this solution. I've managed to do it by using ksort(), array_values()and array_key_exists(). I used the date first as an array key to contain all items sold on that date. Then, move it inside the array after gathering all items, then renumbered the array. Further explanation is given through code comments
// result array
$result = array();
// Loop for each item on inventory
foreach ($inventory as $inv) {
// Loop for each sold array
foreach ($inv['sold'] as $item) {
// date sold
$date = $item['date'];
// Check if date already exist in result array as key
// If not, Create new array with key equal to date and push to result array
if (!array_key_exists($date, $result)) {
$result[$date] = array();
}
// Add array of item => quantity to corresponding date array inside result array
$result[$date][$inv['item']] = $item['quantity'];
}
}
// From here you already have each item => quantity sold inside each date array
// e.g. $result = array( '2017-10-28' => array('NK' => '11', 'FL' => '7', ...) );
// Sort keys which are dates ascending
ksort($result);
// Loop to results and set
foreach ($result as $key => $value) {
$result[$key]['date'] = $key;
}
// Renumber Keys
$result = array_values($result);
// Print result
echo '<pre>';
print_r($result);
Output
Array
(
[0] => Array
(
[NK] => 11
[FL] => 7
[AD] => 5
[CV] => 4
[NB] => 12
[SP] => 4
[WB] => 5
[wny] => 4
[bs] => 15
[date] => 2017-10-28
)
[1] => Array
(
[NK] => 1
[FL] => 2
[AD] => 3
[CV] => 6
[NB] => 4
[SP] => 6
[WB] => 2
[wny] => 6
[bs] => 2
[st] => 16
[ayhtdws] => 2
[sif] => 3
[bb] => 6
[date] => 2017-10-29
)
)
I have two array as below
Array 1
Array
(
[0] => Array
(
[ps_id] => 5
[product_id] => 2
[supplier_id] => 25
[cost] => 789.00
[name] => Mahesh
)
[1] => Array
(
[ps_id] => 6
[product_id] => 2
[supplier_id] => 2
[cost] => 12345.00
[name] => mayank
)
[2] => Array
(
[ps_id] => 7
[product_id] => 2
[supplier_id] => 1
[cost] => 123456.00
[name] => abc
)
[3] => Array
(
[ps_id] => 10
[product_id] => 2
[supplier_id] => 8
[cost] => 12000.00
[name] => mayank1
)
)
Array 2
Array
(
[0] => Array
(
[suppliers] => Mahesh
[suppliers_cost] => 789.00
)
[1] => Array
(
[suppliers] => mayank
[suppliers_cost] => 12345.00
)
[2] => Array
(
[suppliers] => mayank1
[suppliers_cost] => 12000.00
)
[3] => Array
(
[suppliers] => testtetstet
[suppliers_cost] => 123123
)
)
I want to compare above array by their suppliers and name key,
Means if this both key have same values than it will store into one new array and if those key are not match then they will store new different array.
Or might be it possible that both array will have different number of keys
I had tried like below
foreach ($existsProductSupplier as $key => $value) {
if (isset($supplier_data[$key])) {
}else{
$supplier_data[$key]['suppliers']='';
$supplier_data[$key]['suppliers_cost']='';
}
}
foreach ($supplier_data as $key => $value) {
if(in_array($value['suppliers_cost'],$existsProductSupplier[$key])){
//echo "string";
// print_r($value);
}else{
echo "string";
//print_r($value);
}
}
try this code it will help you
<?php
$arr1=Array
(
0 => Array
(
"ps_id" => 5,
"product_id" => 2,
"supplier_id" => 25,
"cost" => 789.00,
"name" => "Mahesh"
),
1 => Array
(
"ps_id" => 6,
"product_id" => 2,
"supplier_id" => 2,
"cost" => 12345.00,
"name" => "mayank"
),
2 => Array
(
"ps_id" => 7,
"product_id" => 2,
"supplier_id" => 1,
"cost" => 123456.00,
"name" => "abc"
),
3 => Array
(
"ps_id" => 10,
"product_id" => 2,
"supplier_id" => 8,
"cost" => 12000.00,
"name" => "mayank1"
),
4 => Array
(
"ps_id" => 10,
"product_id" => 2,
"supplier_id" => 8,
"cost" => 12000.00,
"name" => "mayank2"
)
);
$arr2=Array
(
0 => Array
(
"suppliers" => "Mahesh",
"suppliers_cost" => 789.00
),
1 => Array
(
"suppliers" => "mayank",
"suppliers_cost" => 12345.00
),
2 => Array
(
"suppliers" => "mayank1",
"suppliers_cost" => 12000.00
),
3 => Array
(
"suppliers" => "testtetstet",
"suppliers_cost" => 123123
)
);
foreach($arr1 as $key=>$value){
if(isset($arr2[$key])){
if($value['name']==$arr2[$key]['suppliers']){
$arrnew1[]=$value['name'];
}else{
$arrnew2[]=$value['name'];
}
}else{
$arrnew2[]=$value['name'];
}
}
print_r($arrnew1);
print_r($arrnew2);
I have a tree like below, which contains the nth level permission.
Landing Page Footer Archive
|| || ||
|| || ||
Current ========== Pipeline=== BHI Phonebook==Password Keeper Edit==View==Delete
|| ||
Open Forms Open Forms
|| ||
Blog Request Blog Request
|| ||
Add == Edit Add == Edit
Sample Array
$list = Array
(
[0] => Array
(
[parent] => 0
[item_level] => 1
[id] => 1
[display_name] => Landing Page
)
[1] => Array
(
[parent] => 1
[item_level] => 2
[id] => 2
[display_name] => Current
)
[2] => Array
(
[parent] => 2
[item_level] => 3
[id] => 5
[display_name] => Open Forms
)
[3] => Array
(
[parent] => 5
[item_level] => 4
[id] => 9
[display_name] => Blog Request
)
[4] => Array
(
[parent] => 9
[item_level] => 5
[id] => 10
[display_name] => Add
)
[5] => Array
(
[parent] => 9
[item_level] => 5
[id] => 11
[display_name] => Update
)
[6] => Array
(
[parent] => 1
[item_level] => 2
[id] => 3
[display_name] => Pipeline
)
[7] => Array
(
[parent] => 3
[item_level] => 3
[id] => 6
[display_name] => Open Forms
)
[8] => Array
(
[parent] => 6
[item_level] => 4
[id] => 12
[display_name] => Blog Request
)
[9] => Array
(
[parent] => 12
[item_level] => 5
[id] => 13
[display_name] => Add
)
[10] => Array
(
[parent] => 12
[item_level] => 5
[id] => 14
[display_name] => Update
)
[11] => Array
(
[parent] => 1
[item_level] => 2
[id] => 4
[display_name] => BHI
)
[12] => Array
(
[parent] => 1
[item_level] => 2
[id] => 7
[display_name] => Add Property
)
[13] => Array
(
[parent] => 1
[item_level] => 2
[id] => 8
[display_name] => Show Child Properties
)
[14] => Array
(
[parent] => 0
[item_level] => 1
[id] => 15
[display_name] => Footer
)
[15] => Array
(
[parent] => 15
[item_level] => 2
[id] => 16
[display_name] => Phonebook
)
[16] => Array
(
[parent] => 15
[item_level] => 2
[id] => 17
[display_name] => Password Keeper
)
[17] => Array
(
[parent] => 0
[item_level] => 1
[id] => 18
[display_name] => Archive
)
[18] => Array
(
[parent] => 18
[item_level] => 2
[id] => 19
[display_name] => Edit
)
[19] => Array
(
[parent] => 18
[item_level] => 2
[id] => 20
[display_name] => View
)
[20] => Array
(
[parent] => 18
[item_level] => 2
[id] => 21
[display_name] => Delete
)
)
Expected Output
//Here Key is the value of "id" attribute
$final_output = array(
1 => array(
2=>array(5=>array(9=>array(10,11))),
3=>array(6=>array(12=>array(13,14))),
4,//As it doesnt have any child
7,//As it doesnt have any child
8 //As it doesnt have any child
),
15 => array(
16, //As it doesnt have any child
17 ////As it doesnt have any child
),
18 => array(
19, //As it doesnt have any child
20, ////As it doesnt have any child
21 ////As it doesnt have any child
),
);
here, 1: Landing Page ,15 : Footer ,18 : Archive (its "Id" attribute) etc.
After above array i will have to do a nth level loop.
like:
foreach($final_output as $res)
{
if(is_array($res))
{
//Then do something, i am not sure how this achivable
}
}
What i have done so far, but no success.
//To find out the how manhy levels are in array
$unique_level = array_unique(array_map(function ($i) { return $i['item_level']; }, $list));
$parent_data = $level_data = array();
foreach($list as $key=>$res)
{
if($res['parent']==0)
{
if(!array_key_exists($res['id'],$parent_data)) // First insert all unique parent at level 1
{
$parent_data[$res['id']] = $res;
}
}
$level_data[$res['parent']][] = array(
'display_name' => $res['display_name'],
'id' => $res['id'],
'parent' => $res['parent'],
);
}
Ready to use json
[{"parent":"0","item_level":"1","id":"1","display_name":"Landing Page"},{"parent":"1","item_level":"2","id":"2","display_name":"Current"},{"parent":"2","item_level":"3","id":"5","display_name":"Open Forms"},{"parent":"5","item_level":"4","id":"9","display_name":"Blog Request"},{"parent":"9","item_level":"5","id":"10","display_name":"Add"},{"parent":"9","item_level":"5","id":"11","display_name":"Update"},{"parent":"1","item_level":"2","id":"3","display_name":"Pipeline"},{"parent":"3","item_level":"3","id":"6","display_name":"Open Forms"},{"parent":"6","item_level":"4","id":"12","display_name":"Blog Request"},{"parent":"12","item_level":"5","id":"13","display_name":"Add"},{"parent":"12","item_level":"5","id":"14","display_name":"Update"},{"parent":"1","item_level":"2","id":"4","display_name":"BHI"},{"parent":"1","item_level":"2","id":"7","display_name":"Add Property"},{"parent":"1","item_level":"2","id":"8","display_name":"Show Child Properties"},{"parent":"0","item_level":"1","id":"15","display_name":"Footer"},{"parent":"15","item_level":"2","id":"16","display_name":"Phonebook"},{"parent":"15","item_level":"2","id":"17","display_name":"Password Keeper"},{"parent":"0","item_level":"1","id":"18","display_name":"Archive"},{"parent":"18","item_level":"2","id":"19","display_name":"Edit"},{"parent":"18","item_level":"2","id":"20","display_name":"View"},{"parent":"18","item_level":"2","id":"21","display_name":"Delete"}]
The nearest I've got is...
$list = json_decode($data, true);
$baseList = array_combine(array_column($list,'id'), $list);
$hierarchy = [];
foreach ( $baseList as $newItem ) {
$parent = $newItem['parent'];
$hierarchy[$parent][] = $newItem['id'];
}
function buildTree ( $startNode, $hierarchy) {
$newLevel = [];
foreach ( $startNode as $children ) {
if ( isset($hierarchy[$children])) {
$newLevel[$children] = buildTree($hierarchy[$children], $hierarchy);
}
else {
$newLevel[$children] = $children;
}
}
return $newLevel;
}
$finalOutput = buildTree ( $hierarchy[0], $hierarchy );
print_r($finalOutput);
The problem you have is where you have elements like...
1 => array(
2=>array(5=>array(9=>array(10,11))),
3=>array(6=>array(12=>array(13,14))),
4,//As it doesnt have any child
7,//As it doesnt have any child
8 //As it doesnt have any child
),
As you almost have elements 4, 7 and 8 which don't have a key - all elements in an array have a key, this isn't going to work.
The output which I've done is...
Array
(
[1] => Array
(
[2] => Array
(
[5] => Array
(
[9] => Array
(
[10] => 10
[11] => 11
)
)
)
[3] => Array
(
[6] => Array
(
[12] => Array
(
[13] => 13
[14] => 14
)
)
)
[4] => 4
[7] => 7
[8] => 8
)
[15] => Array
(
[16] => 16
[17] => 17
)
[18] => Array
[19] => 19
[20] => 20
[21] => 21
)
) [19] => 19
[20] => 20
[21] => 21
)
)
Which as you can see, has element 4 having a value of 4.
$list = Array
(
0 => Array
(
'parent' => 0,
'item_level' => 1,
'id' => 1,
'display_name' => 'Landing Page'
),
1 => Array
(
'parent' => 1,
'item_level' => 2,
'id' => 2,
'display_name' => 'Current'
),
2 => Array
(
'parent' => 2,
'item_level' => 3,
'id' => 5,
'display_name' => 'Open Forms'
),
3 => Array
(
'parent' => 5,
'item_level' => 4,
'id' => 9,
'display_name' => 'Blog Request'
),
4 => Array
(
'parent' => 9,
'item_level' => 5,
'id' => 10,
'display_name' => 'Add'
),
5 => Array
(
'parent' => 9,
'item_level' => 5,
'id' => 11,
'display_name' => 'Update'
),
6 => Array
(
'parent' => 1,
'item_level' => 2,
'id' => 3,
'display_name' => 'Pipeline'
),
7 => Array
(
'parent' => 3,
'item_level' => 3,
'id' => 6,
'display_name' => 'Open Forms'
),
8 => Array
(
'parent' => 6,
'item_level' => 4,
'id' => 12,
'display_name' => 'Blog Request'
),
9 => Array
(
'parent' => 12,
'item_level' => 5,
'id' => 13,
'display_name' => 'Add'
),
10 => Array
(
'parent' => 12,
'item_level' => 5,
'id' => 14,
'display_name' => 'Update'
),
11 => Array
(
'parent' => 1,
'item_level' => 2,
'id' => 4,
'display_name' => 'BHI'
),
12 => Array
(
'parent' => 1,
'item_level' => 2,
'id' => 7,
'display_name' => 'Add Property'
),
13 => Array
(
'parent' => 1,
'item_level' => 2,
'id' => 8,
'display_name' => 'Show Child Properties'
),
14 => Array
(
'parent' => 0,
'item_level' => 1,
'id' => 15,
'display_name' => 'Footer'
),
15 => Array
(
'parent' => 15,
'item_level' => 2,
'id' => 16,
'display_name' => 'Phonebook'
),
16 => Array
(
'parent' => 15,
'item_level' => 2,
'id' => 17,
'display_name' => 'Password Keeper'
),
17 => Array
(
'parent' => 0,
'item_level' => 1,
'id' => 18,
'display_name' => 'Archive'
),
18 => Array
(
'parent' => 18,
'item_level' => 2,
'id' => 19,
'display_name' => 'Edit'
),
19 => Array
(
'parent' => 18,
'item_level' => 2,
'id' => 20,
'display_name' => 'View'
),
20 => Array
(
'parent' => 18,
'item_level' => 2,
'id' => 21,
'display_name' => 'Delete'
),
);
Hi, try this code:
$results=array();
$parent1='';$parent=$parent2='';
foreach ($list as $key => $value) {
if($value['parent']==0)
{
$results[$value['id']]=$value;
$id=$value["id"];
}
if($id==$value['parent']&&$id!="")
{
$results[$id][$value['id']]=$value;
$parent=$value['id'];
}
if($parent==$value['parent']&&$parent!="")
{
$results[$id][$parent][$value['id']]=$value;
$parent1=$value['id'];
}
if($parent1==$value['parent']&&$parent1!="")
{
$results[$id][$parent][$parent1][$value['id']]=$value;
$parent2=$value['id'];
}
if($parent2==$value['parent']&&$parent2!="")
{
$results[$id][$parent][$parent1][$parent2][$value['id']]=$value;
$parent3=$value['id'];
}
}
echo '<pre>';
print_r($results);
echo '</pre>';