PHP: Building a hierarchistic array structure - php

I'm having some problems building an hierarchistic array structure - I have almost got it done, but there's something unexplained that makes my array look weird which I hoped you could help me with.
The tree structure is:
root
|data
|-children
|--data
|--children
|---data
|---children
Any child can have any number of children and each child can have any number of parents.
I have a function that builds the tree:
private function build_tree($rows,$parent) {
$i = 0;
$response -> result = array();
$result = array();
$leaf = false;
foreach($rows as $row) {
if($row['parent_id'] == $parent) {
$leaf = is_null($row['treeDef']) ? false : true;
$workingSet = array_merge($result,array(
'id' => (int)$i,
'parent_id' => (int)$row['parent_id'],
'child_id' => (int)$row['id'],
'name' => (string)$row['resourceName'],
'updated' => strtotime($row['updated']),
'purchasedUnit' => (string)$row['purchasingUnit'],
'purchasedCost' => (double)$row['purchasingCosts'],
'purchasedDiscount' => (double)$row['discount'],
'estimateUnit' => (string)$row['estimatingUnit'],
'profitAddOn' => (string)$row['profitAddOn'],
'landedCost' => (double)$row['landedCost'],
'unitCorrelation' => (double)$row['unitCorrelation'],
'leadTime' => (string)$row['leadTime'],
'ResourceClassShortname' => (string)$row['ResourceClassShortname'],
'supplierName' => (string)$row['nameSupplier'],
'iconCls' => (string)$row['typeIcon'],
'leaf' => $leaf
));
$hasChildren = $this->Resource_model->has_children($rows,$row['id']);
if ($hasChildren->check) {
if (!$leaf) {
for($j=0; $j <= ($hasChildren -> rows); $j++) {
$parentArray = $workingSet;
$childArray = $this -> Resource_model -> build_tree($rows,$row['id']);
$workingSet = array_merge($parentArray,array('children' => $childArray -> result));
}
}
}
$result[$i] = $workingSet;
$i++;
}
}
$response -> result = $result;
$response -> rows = $i;
return $response;
}
Which produces this JSON:
Big picture
Every item that has 2 children (or more? - no test values) gets the first item like it should be, but the second item contains the first item as well - duplicating all the results.
Any help appreciated.

Instead of array_merge use array_push - this will add the children subarray instead of trying merging it with the one already existing...
It's in this part of code (already edited):
$hasChildren = $this->Resource_model->has_children($rows,$row['id']);
if ($hasChildren->check) {
if (!$leaf) {
for($j=0; $j <= ($hasChildren->rows); $j++) {
$parentArray = $workingSet;
$childArray = $this->Resource_model->build_tree($rows,$row['id']);
$workingSet = array_push($parentArray,array('children' => $childArray->result));
}
}
}

Made it work, here's the final code:
private function build_tree($rows,$parent) {
$i = 0;
$response -> result = array();
$result = array();
$leaf = false;
$newArray = array();
foreach($rows as $row) {
if($row['parent_id'] == $parent) {
$leaf = is_null($row['treeDef']) ? false : true;
$newArray = array(
'id' => (int)$i,
'parent_id' => (int)$row['parent_id'],
'child_id' => (int)$row['id'],
'name' => (string)$row['resourceName'],
'updated' => strtotime($row['updated']),
'purchasedUnit' => (string)$row['purchasingUnit'],
'purchasedCost' => (double)$row['purchasingCosts'],
'purchasedDiscount' => (double)$row['discount'],
'estimateUnit' => (string)$row['estimatingUnit'],
'profitAddOn' => (string)$row['profitAddOn'],
'landedCost' => (double)$row['landedCost'],
'unitCorrelation' => (double)$row['unitCorrelation'],
'leadTime' => (string)$row['leadTime'],
'ResourceClassShortname' => (string)$row['ResourceClassShortname'],
'supplierName' => (string)$row['nameSupplier'],
'iconCls' => (string)$row['typeIcon'],
'leaf' => $leaf
);
$hasChildren = $this -> Resource_model -> has_children($rows,$row['id']);
if ($hasChildren->check) {
for($j=0; $j <= ($hasChildren -> rows); $j++) {
$childArray = $this -> Resource_model -> build_tree($rows,$row['id']);
$newArray = array_merge($newArray, array('children' => $childArray -> result));
}
}
$result[$i] = $newArray;
$i++;
}
}
$response -> result = $result;
$response -> rows = $i;
return $response;
}

Related

which algorithm php to create a secret Santa program

I'm trying to make a draw for a secret Santa.
I collect in a table the information of the person and the name of the person on whom it should not fall (to avoid couples).
However during my PHP loop I can't take into account my exclusions
foreach ($supplier as $sup){
$exclude = $sup['blacklist'];
$data = $recipient;
$temp = array_diff($data[], array($exclude));
echo $temp[rand(0, sizeOf($temp))];
foreach ($recipient as $key=>$recip){
if ($sup['surname'] !== $recip['surname']){
$result[] = ['recipient' => $recip, 'supplier' => $sup];
unset($recipient[$key]);
}
}
}
How can I take into account this blacklist please?
shuffle($supplier);
shuffle($recipient);
// dump($supplier, $recipient);
$result = [];
foreach ($supplier as $sup){
$assign = false;
dump($sup);
foreach ($recipient as $key=>$recip){
dump($recip['surname']);
if ($sup['surname'] !== $recip['surname'] && $sup['blacklist'] !== $recip['surname'] && $sup['surname'] !== $recip['blacklist']){
$result[] = ['recipient' => $recip, 'supplier' => $sup];
dump($sup['surname']);
unset($recipient[$key]);
$assign = true;
}
if ($assign === true){
break;
}
}
}
return $result;
}
this is my code with shuffle
Your solution is fine, but with that nested loop it's going to start behaving poorly as the list of participants grows. You can accomplish the task with a single array and two linear passes over it:
$people = [
[ 'name' => 'alice', 'blacklist' => ['bob'] ],
[ 'name' => 'bob', 'blacklist' => ['alice'] ],
[ 'name' => 'carl', 'blacklist' => ['david'] ],
[ 'name' => 'david', 'blacklist' => ['carl'] ],
[ 'name' => 'elise', 'blacklist' => ['frank'] ],
[ 'name' => 'frank', 'blacklist' => ['elise'] ],
[ 'name' => 'georg', 'blacklist' => ['herb'] ],
[ 'name' => 'herb', 'blacklist' => ['george'] ]
];
function wrap_index($count, $index) {
$cur = $index;
if( $index == 0 ) {
$prev = $count - 1;
$next = $index + 1;
} else if( $index == $count - 1) {
$prev = $index - 1;
$next = 0;
} else {
$prev = $index - 1;
$next = $index + 1;
}
return [$prev, $cur, $next];
}
function array_swap(&$array, $a, $b) {
$tmp = $array[$a];
$array[$a] = $array[$b];
$array[$b] = $tmp;
}
function santify($people) {
shuffle($people);
$count = count($people);
for( $i=0; $i<$count; ++$i ) {
list($prev, $cur, $next) = wrap_index($count, $i);
if( in_array($people[$cur]['name'], $people[$next]['blacklist']) ) {
printf("%s in blacklist for %s\n", $people[$cur]['name'], $people[$next]['name']);
array_swap($people, $cur, $prev);
}
}
$pairs = [];
for( $i=0; $i<$count; ++$i ) {
list($prev, $cur, $next) = wrap_index($count, $i);
$pairs[] = [ $people[$cur]['name'], $people[$next]['name'] ];
}
return $pairs;
}
foreach( santify($people) as $pair ) {
printf("%s\n", json_encode($pair));
}
Output:
david in blacklist for carl
elise in blacklist for frank
["david","georg"]
["georg","carl"]
["carl","bob"]
["bob","herb"]
["herb","elise"]
["elise","alice"]
["alice","frank"]
["frank","david"]
There is a caveat to this approach, though. It will only work with strict 1:1 blacklist arrangements. Once there is a love triangle [or quardrangle or above] or any polyamory, neither of our solutions is equipped for these extra dimensions.

php - how to convert 'tree-array' to array

I have gotten the tree-array like that:
Now I want to convert it to an array like this:
array(
1 => array('id'=>'1','parentid'=>0),
2 => array('id'=>'2','parentid'=>0),
3 => array('id'=>'3','parentid'=>1),
4 => array('id'=>'4','parentid'=>1),
5 => array('id'=>'5','parentid'=>2),
6 => array('id'=>'6','parentid'=>3),
7 => array('id'=>'7','parentid'=>3)
);
I had already coded something like this:
private function _getOrderData($datas)
{
$_data = [];
static $i = 0;
foreach ($datas as $data) {
$i++;
$rows = ['id' => $data['id'], 'pid' => isset($data['children']) ? $data['id'] : 0, 'menu_order' => $i];
if(isset($data['children'])) {
$this->_getOrderData($data['children']);
}
$_data[] = $rows;
}
return $_data;
}
But it didn't work
Cry~~
How can I fix my code to get the array? Thanks~
BTW, my English is pool.
So much as I don't know you can read my description of the problem or not.
I had already solved this problem, Thx~
private function _getOrderData($datas, $parentid = 0)
{
$array = [];
foreach ($datas as $val) {
$indata = array("id" => $val["id"], "parentid" => $parentid);
$array[] = $indata;
if (isset($val["children"])) {
$children = $this->_getOrderData($val["children"], $val["id"]);
if ($children) {
$array = array_merge($array, $children);
}
}
}
return $array;
}
The array look like:
array

Make separate php arrays from one json file

I have this json source file:
{
"results":
[
{
"movie_title":"A Monster Calls",
"cinema":"downtown"
},
{
"movie_title":"A Monster Calls",
"cinema":"uptown"
},
{
"movie_title":"A Monster Calls",
"cinema":"downtown"
},
{
"movie_title":"A Monster Calls",
"cinema":"downtown"
}
]
}
and I am writing my array like this (simplified for clarity):
$json_data = json_decode($html, true);
for($i = 0; $i < count($json_data['results']); $i++) {
$movieTitle = $json_data['results'][$i]["movie_title"];
$cinema = $json_data['results'][$i]["cinema"];
$moviesList[]= array(
"movieTitle" => $movieTitle,
"cinema" => $cinema
);
}
But what I want to do is output 2 separate arrays. One is all films showing in "downtown" cinema, and the other array all films showing in "uptown". The order in the json file will change, so I have to do it by name.
What's the best way to do this?
$downtownArray = array();
$uptownArray = array();
$json_data = json_decode($html, true);
for($i = 0; $i < count($json_data['results']); $i++) {
$movieTitle = $json_data['results'][$i]["movie_title"];
$cinema = $json_data['results'][$i]["cinema"];
if ($cinema == 'uptown') {
$uptownArray[]= array(
"movieTitle" => $movieTitle,
"cinema" => $cinema
);
} else {
$downtownArray[]= array(
"movieTitle" => $movieTitle,
"cinema" => $cinema
);
}
}
foreach ($json_data['results'] as $result) {
$cinema = $result['cinema'];
$moviesList[$cinema] []= [
"movieTitle" => $result['movie_title'],
// ...
];
}
The code classifies the results by the cinema field and stores them into $moviesList array. So, for example, the uptown results will be stored into $moviesList['uptown'].
You may try something like this
$json_data = json_decode($html, true);
$moviesDwn=array();
$moviesUp=array();
for($i = 0; $i < count($json_data['results']); $i++) {
$movieTitle = $json_data['results'][$i]["movie_title"];
$cinema = $json_data['results'][$i]["cinema"];
if ($json_data['results'][$i]["cinema"]='uptown')
$moviesUp[]= array(
"movieTitle" => $movieTitle,
"cinema" => $cinema
);
else if ($json_data['results'][$i]["cinema"]='updown')
$moviesDwn[]= array(
"movieTitle" => $movieTitle,
"cinema" => $cinema
);
}
First of all please , FIXyou JSON ,as it has some extra comma and then try with below codes
If you want to just separate the two array than use foreach(){};
foreach ($json_data['results'] as $result) {
$DownTown_List[$result['cinema']] []= $result['movie_title'];
}
OR
if you want to do other operation with the Indexes then use for(){};
for($i = 0; $i < count($json_data['results']); $i++) {
if($json_data['results'][$i]["cinema"] === "downtown"){
$DownTown_List["downtown"][] = $json_data['results'][$i]["movie_title"];
}
if($json_data['results'][$i]["cinema"] === "uptown"){
$DownTown_List["uptown"][] = $json_data['results'][$i]["movie_title"];
}
}
echo "<pre>";print_r($DownTown_List);exit;
OUTPUT
Array
(
[downtown] => Array
(
[0] => A Monster Calls
[1] => A Monster Calls
[2] => A Monster Calls
)
[uptown] => Array
(
[0] => A Monster Calls
)
)

Symfony2 undefined offset: 0 error with lottery project

In my lottery project I have 5 tickets, in which you select numbers and buy. The thing is, you can only buy the tickets if you buy them in order... For example:
Ticket 1 Ticket 2 Ticket 3 Ticket 4 Ticket 5
If you add numbers to the ticket 1 and then the others it works... If you skip the ticket 1 and add numbers to the other ones, when you try to buy you get this error:
ContextErrorException: Notice: Undefined offset: 0 in C:\wamp\www\Digidis\front\src\MediaparkLt\UserBundle\Service\MoneyManager.php line 313
The full stack:
array('cartProduct' => array('title' => 'EUROMILLONES', 'price' => '2.35', 'product' => '2', 'ticket_id' => '1433921783_19792', 'numbers' => '8,13,14,17,37', 'stars' => '4,7', 'betslip' => '{"duration":"1","subscription":"false","jsPrice":"235","type":"simple","numbers1":"0,0,0,0,0","numbers2":"8,13,14,17,37","numbers3":"0,0,0,0,0","numbers4":"0,0,0,0,0","numbers5":"0,0,0,0,0","stars1":"0,0","stars2":"4,7","stars3":"0,0","stars4":"0,0","stars5":"0,0","dayOfWeek":"3"}', 'is_syndicate' => false, 'draw' => object(DateTime)), 'product' => object(Product), 'user' => object(User), 'reference' => null, 'paymentResult' => 'Authorised', 'bets' => object(stdClass), 'individualBets' => array(), 'tickets' => array(array('numbers' => '8,13,14,17,37', 'stars' => '4,7')), 'k' => '0', 't' => array('numbers' => '0,0,0,0,0', 'stars' => '0,0'), 'is_ticket_filled' => false, 'week_id' => array(array('ticketId' => '7005')), 'g' => '0', 'lastId' => '7005', 'purchase' => object(Purchase), 'price' => '2.35', 'bet' => object(Bet), 'euromillonesBet' => object(EuromillonesBet), 'drawDate' => array(object(DrawDate)), 'j' => '0')) in C:\wamp\www\Digidis\front\src\MediaparkLt\UserBundle\Service\MoneyManager.php line 313
As you can see first it gets the ticket 1, which is empty(or 0) and thats why it causes the error... How can I make it so that it skips the empty tickets?
Here is the controller where the error occurs:
$bets = json_decode($cartProduct['betslip']);
$individualBets = array();
$tickets = array(
array('numbers' => $bets->numbers1, 'stars' => $bets->stars1),
array('numbers' => $bets->numbers2, 'stars' => $bets->stars2),
array('numbers' => $bets->numbers3, 'stars' => $bets->stars3),
array('numbers' => $bets->numbers4, 'stars' => $bets->stars4),
array('numbers' => $bets->numbers5, 'stars' => $bets->stars5)
);
if ($bets->type === 'simple') {
foreach ($tickets as $k => $t) {
$is_ticket_filled = ((int) str_replace(',', '', $t['numbers'])) > 0;
if (!$is_ticket_filled) {
unset($tickets[$k]);
}
}
} else if ($bets->type === 'multiple') {
$tickets = array(array('numbers' => $bets->numbers1, 'stars' => $bets->stars1));
}
$week_id = null;
for ($k = 0; $k < (count($tickets)); $k++) {
for ($g = 0; $g < $bets->duration; $g++) {
if (!isset($week_id[$g])) {
$week_id[$g] = $this->entityManager->getRepository('MediaparkLtLotteryBundle:Bet')->getLastTicketId();
if ($week_id[$g]) {
$week_id[$g]['ticketId'] ++;
} else {
$week_id[$g]['ticketId'] = 0;
}
}
$lastId = $week_id[$g]['ticketId'];
$purchase = new Purchase();
$purchase->setUser($user);
$purchase->setDrawDate($cartProduct['draw']);
$purchase->setProduct($product);
$purchase->setReference($reference);
$price = $cartProduct['price'];
$bet = new Bet();
if ('eurojackpot' == $product->getAlias()) {
$euromillonesBet = new EurojackpotBet();
} else {
$euromillonesBet = new EuromillonesBet();
}
$drawDate = $this->entityManager->getRepository('MediaparkLtLotteryBundle:DrawDate')->findByDrawDate($cartProduct['draw']);
if (!$drawDate)
die('no draw date found ' . $cartProduct['draw']->format('Y-m-d H:i:s'));
$bet->setDrawDate($drawDate[0]);
$bet->setTicketId($lastId);
if (strtoupper($paymentResult) === 'AUTHORISED') {
$bet->setStatus(BetStatus::AUTHORISED);
} else {
$bet->setStatus(BetStatus::FAILED);
}
$bet->setWinnings(0);
$euromillonesBet->setBet($bet);
/// LINE 313 ABOVE!!!!!!!
$numbers = $this->getNumbersArray($tickets[$k]['numbers']);
$j = 0;
foreach ($numbers as $number) {
$j++;
$name = 'setN' . $j;
$euromillonesBet->$name($number);
}
$numbers = $this->getNumbersArray($tickets[$k]['stars']);
$euromillonesBet->setS1($numbers[0]);
$euromillonesBet->setS2($numbers[1]);
$euromillonesBet->setAmountOfStars(Bet::NUMBER_OF_STARS);
$purchase->addBet($bet);
$purchase->setPricePaid($price);
if (strtoupper($paymentResult) === 'AUTHORISED') {
$purchase->setStatus(PaymentStatus::AUTHORISED);
} else {
$purchase->setStatus(PaymentStatus::FAILED);
}
if ($bets->subscription === "true") {
$contract = new PurchaseContract();
$contract->setAccumulatedWinnings(0);
$contract->setCancellationDate(null);
$contract->setFirstDrawDate($purchase->getDrawDate());
$contract->setLastRenewedDate($purchase->getDrawDate());
$contract->setNextRenewalFirstDrawDate($purchase->getDrawDate());
// $contract->setPurchase($purchase);
$contract->setStatusPurchaseContract(1);
$contract->setWeeks(1);
$purchase->setPurchaseContract($contract);
$this->entityManager->persist($contract);
}
if ($g == 0)
$individualBets[] = $euromillonesBet;
$this->entityManager->persist($bet);
$this->entityManager->persist($euromillonesBet);
$this->entityManager->persist($purchase);
$this->entityManager->flush();
}
}
return $individualBets;
}
From what I see the bet type in your object is set to "type":"simple" and numbers1":"0,0,0,0,0"
$is_ticket_filled = ((int) str_replace(',', '', $t['numbers'])) > 0;
//(int) 00000 = 0
if (!$is_ticket_filled) {
unset($tickets[$k]);
}
Is causing the issue since unset does not reset the array indexes.
http://ideone.com/5q74Wv
Then later you iterate using for($k=0; $k < count($tickets); $k++)
You should instead rebase the array after using unset($tickets[$k])
if ($bets->type === 'simple') {
//...
$tickets = array_values($tickets);
}
or check the existence of the ticket when iterating over indexes
$ticketCount = count($tickets);
for ($k=0; $k < $ticketCount; $k++) {
if (false === isset($tickets[$k]) {
continue;
}
//...
}
or easier still, iterate over the existing tickets array using foreach instead of for.
foreach ($tickets as $k => $ticket) {
//...
}
Then change $tickets[$k] with just $ticket since $k is not used anywhere else.

Convert multidimensional array to nested set array

im having a little problem i need some help with.
Im trying to convert a multidimensional array into a flatten array with nested set values right and left like so:
$array = {
'id' => 1
'name' => 'john'
'childs' => array(
array(
'id' => 1
'name' => 'jane'
)
)
}
to
$array = {
array(
'id' => 1,
'name' => 'john'
'left' => '1'
'right' => '4'
),
array(
'id' => 1,
'name' => 'jane'
'left' => '2'
'right' => '3'
)
}
Any help is appreciated!
I asked a very similar question and got a result, so thought I'd send it on for you. I realise this is quite an old topic, but still worth getting an answer. I've included my data, but would be easily adapted for yours.
$JSON = '[{"id":1,"children":[{"id":2,"children":[{"id":3},{"id":4}]},{"id":5}]}]';
$cleanJSON = json_decode($JSON,true);
$a_newTree = array();
function recurseTree($structure,$previousLeft)
{
global $a_newTree; // Get global Variable to store results in.
$indexed = array(); // Bucket of results.
$indexed['id'] = $structure['id']; // Set ID
$indexed['left'] = $previousLeft + 1; // Set Left
$lastRight = $indexed['left'];
$i_count = 0;
if ($structure['children'])
{
foreach ($structure['children'] as $a_child)
{
$lastRight = recurseTree($structure['children'][$i_count],$lastRight);
$i_count++;
}
}
$indexed['right'] = $lastRight + 1; // Set Right
array_push($a_newTree,$indexed); // Push onto stack
return $indexed['right'];
}
recurseTree($cleanJSON[0],0);
print_r($a_newTree);
function restructRecursive($array, $left = 1) {
if (isset($array['childs'])) {
$result = array();
foreach ($array['childs'] as $child) {
$result = array_merge($result, restructRecursive($child, $left+1));
}
unset($array['childs']);
}
$array['left'] = $left;
return array_merge(array($array), $result);
}
$newStruct = restructRecursive($oldStruct);
For anyone coming here and looking for a solution, loneTraceur's solution works fine. However, I needed one in OOP, so here is my version of it.
<?php
class NestedSet
{
protected $tree = [];
public function deconstruct($tree, $left = 0)
{
$this->flattenTree($tree, $left);
return $this->tree;
}
protected function flattenTree($tree, $left)
{
$indexed = [];
$indexed['id'] = $tree['id'];
$indexed['_lft'] = $left + 1;
$right = $indexed['_lft'];
if (isset($tree['children']) && count($tree['children'])) {
foreach ($tree['children'] as $child) {
$right = $this->flattenTree($child, $right);
}
}
$indexed['_rgt'] = $right + 1;
$this->tree[] = $indexed;
return $indexed['_rgt'];
}
}
You would run it like this:
$NestedSet = new NestedSet;
$flat = $NestedSet->deconstruct($tree):
This code is based on the other answer.

Categories