String together values of parent nodes from a multidimensional PHP array - php

I have a table of workplaces and their parent ids. Each parent could have any number of levels.
id workplace parent
1 WCHN 0
2 Acute Services 1
3 Paediatric Medicine 2
4 Surgical Services 2
5 Nursing and Midwifery 1
6 Casual Pool 5
7 Clinical Practice 5
I need to create a single select input that lists the workplaces with all their parent workplaces, a bit like this:
<select>
<option>WCHN > Acute Services > Paediatric Medicine</option>
<option>WCHN > Acute Services > Surgical Services</option>
<option>WCHN > Nursing and Midwifery > Casual Pool</option>
<option>WCHN > Nursing and Midwifery > Clinical Practice</option>
</select>
By modifying this solution I've been able to turn my flat list into a multidimensional array and output pairs of values, but no the full paths.
<?php
public function list_all_workplaces()
{
$temp = $result = array();
$list = array();
// Get the data from the DB
$table = mysql_query("SELECT * FROM workplaces");
// Put it into one dimensional array with the row id as the index
while ($row = mysql_fetch_assoc($table)) {
$temp[$row['workplace_id']] = $row;
}
// Loop the 1D array and create the multi-dimensional array
for ($i = 1; isset($temp[$i]); $i++)
{
if ($temp[$i]['parent'] > 0)
{
$tmpstring = ($temp[$i]['workplace']); // workplace title
// This row has a parent
if (isset($temp[$temp[$i]['parent']])) {
$list[$i] = $temp[$temp[$i]['parent']]['workplace']." > ".$tmpstring;
//example output: Acute Services > Paediatric Medicine
// The parent row exists, add this row to the 'children' key of the parent
$temp[$temp[$i]['parent']]['children'][] =& $temp[$i];
} else {
// The parent row doesn't exist - handle that case here
// For the purposes of this example, we'll treat it as a root node
$result[] =& $temp[$i];
}
} else {
// This row is a root node
$result[] =& $temp[$i];
}
}
// unset the 1D array
unset($temp);
//Here is the result
print_r($result);
}
Example print_r() output:
[1] => WCHN > Acute Services
[2] => Acute Services > Paediatric Medicine
Where do I go from here to get all of the parent workplaces into the select options?

I can't see how your code will produce the print_r output you say you're seeing, as you're not ever using the string you create. But something along these lines should get you on your way - it generates the strings you need.
Replace your for loop with the following:
foreach ($temp as $i => $details)
{
$parentID = $details['parent'];
$tmpstring = ($details['workplace']);
if ($parentID > 0 && isset($temp[$parentID]))
{
$temp[$parentID]['children'][] =& $temp[$i];
while ($parentID > 0 && isset($temp[$parentID]))
{
$tmpstring = $temp[$parentID]['workplace']." > ".$tmpstring;
$parentID = $temp[$parentID]['parent'];
}
}
$result[] = $tmpstring;
}
As #Syx said, you'll need to return something from your function too, probably $result, and then use that to generate your <select>.

I don't understand how you even use your function if there is no return value.
If your array is built correctly and the way you want it and your asking about getting your results into an html select field then that's the easy part.
Let's get back to your function real quick
.
You don't have return $result listed in your function so it's not going to return anything when called.
You'll want to add that to the end of the function to start.
You'll then loop through the array to start your html processing.
$data = list_all_workplaces()
foreach( $data as $key => $value )
{
$options = "<option value='{$key}'>{$value}</option>";
}
echo "<select>{$options}</select>";

Related

Multilevel tree with PHP

I have one table of folders
We dont know the depth of array and first I am trying to take all folder with parentid 0 and then am again calling the same function in foreach so that iterates each folder. Below is the dummy table
Id parentid label
-- -------- ------
1 0 ABC
2 1 XYZ
3 2 ABC2
4 0 DUMMY
I am trying to make tree with below code:
public function getFolder($parentId = null)
{
if(IS_NULL($parentId)){
$parentId = 0;
}
$folders = Connection::queryBuilder()->select('f.id,f.parentid,f.label')
->from('folders','f')
->where('parentid='.$parentId)->execute()->fetchAll(PDO::FETCH_OBJ);
$temp = array();
foreach($folders as $folder){
$subfolders = self::getFolder($folder->id,$temp);
array_push($temp,$folder,$subfolders);
}
return $temp;
}
But I am not getting the array the way I want. Can anyone help me?
Thanks in advance
There was a very close question: How to created nested multi dimensional array from database result array with parent id
#!/usr/bin/env php
<?php
// ordered by parent
$src = array(
['cid'=>'1','name'=>'Rootcat','parent'=>'0'],
['cid'=>'3','name'=>'dddd','parent'=>'1'],
['cid'=>'4','name'=>'test1','parent'=>'3'],
['cid'=>'5','name'=>'Test123 rec','parent'=>'3'],
['cid'=>'6','name'=>'abceg','parent'=>'3'],
['cid'=>'7','name'=>'JHGSGF','parent'=>'5'],
);
// inverse array, better take it sorted by parent_id desc
$data = array_reverse($src,false);
$for_parent = [];
foreach($data as $rec) {
if (isset($for_parent[$rec['cid']])) {
$rec['sub'] = $for_parent[$rec['cid']];
unset($for_parent[$rec['cid']]);
}
$for_parent[$rec['parent']] [$rec['cid']]= $rec;
}
print_r($for_parent);
So you can create array without recursive function.

fetch data from model that is called in loop

I have a controller function in CodeIgniter that looks like this:
$perm = $this->job_m->getIdByGroup();
foreach($perm as $pe=>$p)
{
$pId = $p['id'];
$result = $this->job_m->getDatapermission($pId);
}
$data['permission'] = $result;
What I need to do is list the data in the result in the view, but I get only the last value while using this method. How can I pass all the results to the view?
Store it in an array. Like this:
foreach($perm as $pe=>$p){
$result[] = $this->job_m->getDatapermission($p['id']);
}
Because $result is not an array...
try this:
$result=array();
foreach($perm as $pe=>$p)
{
$pId = $p['id'];
$result[] = $this->job_m->getDatapermission($pId);
}
$data['permission'] = $result;
Note:
My answer uses a counter to enable the display of a single group result when needed.
Guessing from your need to loop and display the value of $result, possibly, it is an array or object returned by $query->result(). Things could be a bit complex.
Example: if $perm is an array of 5 items( or groups), the counter assigns keys 1 - 5 instead of 0 - 4 as would [] which could be misleading. Using the first view example, you could choose to display a single group value if you wants by passing it via a url segment. Making the code more flexible and reusable. E.g. You want to show just returns for group 2, in my example, $result[2] would do just that else next code runs. See my comments in the code.
$perm = $this->job_m->getIdByGroup();
$counter = 1;
foreach($perm as $pe=>$p)
{
$pId = $p['id'];
$result[$counter] = $this->job_m->getDatapermission($pId);
$counter++;
}
$data['permission'] = $result;
As mentioned above Note:
I Added a Counter or Key so you target specific level. If the groups are:
Men, Women, Boys, Girls, Children; you'd know women is group two(2) If you desire to display values for just that group, you don't need to rewrite the code below. Just pass the group key would be as easy as telling it by their sequence. To display all the loop without restrictions, use the second view example. To use both, use an if statement for that.
###To access it you could target a specific level like
if(isset($permission)){
foreach($permission[2] as $key => $value){
echo $value->columnname;
}
###To get all results:
foreach($permission as $array){
foreach($array as $key => $value){
echo $value->columnname;
}
}
}

Recursive Array Exploding

I have string structured like
(cat,dog,fish) && (drinks) && (milk,water)
I need to convert to an array list like
cat drinks milk
cat drinks water
dog drinks milk
dog drinks water
fish drinks milk
fish drinks water
I've thought about doing it with a loop that takes each group and inserts them into an array like
0th pass:
fill the array with the first row
(cat,dog,fish) && (drinks) && (milk,water)
1st pass:
detect the first group and split it while removing the source
cat && (drinks) && (milk,water)
dog && (drinks) && (milk,water)
fish && (drinks) && (milk,water)
2nd pass
....
then loop this each time take the line split it add it to the end and remove the original.
Do you have a better idea? and in PHP?
For those who wonder It is part of sentence parsing code I'm writing.
Thanks
use normal string parsing to get 3 arrays which correspond to their groupings. I think you can figure it out with explode()
Then generate the "cartesian product" of the 3 arrays
$iterators = array(
new ArrayIterator(array('cat', 'dog', 'fish'))
, new ArrayIterator(array('drinks'))
, new ArrayIterator(array('milk', 'water'))
);
$citer = new CartesianProductIterator($iterators);
foreach ($citer as $combo) {
printf("[%s]\n", join(',', $combo));
}
Using
class CartesianProductIterator implements Iterator {
protected $iterators;
function __construct(array $iters) {
$this->iterators = $iters;
}
function rewind() {
foreach ($this->iterators as $it) {
$it->rewind();
}
}
function current() {
$values = array();
foreach ($this->iterators as $it) {
$values[] = $it->current();
}
return $values;
}
function key() {
return null;
}
function next() {
/*
loop them in reverse, but exclude first
why? example, odometer: 55199
you always check the rightmost digit first to see if incrementing it would roll it over and need to be "rewound" to 0,
which causes the digit to the left to increase as well, which may also cause it to roll over as well, and so on...
looping in reverse operates from right column to the left.
we dont rewind the first column because if the leftmost column is on its last element and needs to roll over
then this iterator has reached its end, and so rewind() needs to be explicitly called
*/
for ($i = count($this->iterators) - 1; $i > 0; --$i) {
$it = $this->iterators[$i];
$it->next();
if ($it->valid()) {
// were done advancing because we found a column that didnt roll over
return;
} else {
$it->rewind();
}
}
//if execution reached here, then all of the columns have rolled over, so we must attempt to roll over the left most column
$this->iterators[0]->next();
}
function valid() {
return $this->iterators[0]->valid();
}
}
Once i needed to make every combination of similar sets. I had a recursive function which was actually very resource-intensive on big array (9 parts containing 5 items each) but i can try to adjust it for you:
$input=array(array("cat","dog","fish"),array("drinks"),array("milk","water"));
$output=array();
function combination($string,$level)
{
global $output;
global $input;
if (isset($input[$level]))
{
$item=$input[$level];
if (is_array($item))
{
foreach ($item as $i)
combination($string." ".$i,$level+1);
}
else
combination($string." ".$item,$level+1);
}
else
$output[]=$string;
}
combination("",0);
var_export($output);
However converting your string into input array is different problem, which i am not sure how to solve so i will keep it up to you.

shift array without indexOfOutBounds

I have 2 array's with the same length. array $gPositionStudents and array $gPositionInternships. Each student is assigned to a different internship, That part works.
Now I want the first element (index 0) of $gPositionStudent refer to the second (index 1) element of array $gPositionInternship. That implicitly means that the last element of $gPositionStudents refer to the first element of $gPositionInternship. (I included a picture of my explanation).
My Code is:
// Make table
$header = array();
$header[] = array('data' => 'UGentID');
$header[] = array('data' => 'Internships');
// this big array will contains all rows
// global variables.
global $gStartPositionStudents;
global $gStartPositionInternships;
//var_dump($gStartPositionInternships);
$rows = array();
$i = 0;
foreach($gStartPositionStudents as $value) {
foreach($gStartPositionInternships as $value2) {
// each loop will add a row here.
$row = array();
// build the row
$row[] = array('data' => $value[0]['value']);
//if($value[0] != 0 || $value[0] == 0) {
$row[] = array('data' => $gStartPositionInternships[$i]);
}
$i++;
// add the row to the "big row data (contains all rows)
$rows[] = array('data' => $row);
}
$output = theme('table', $header, $rows);
return $output;
Now I want that I can choose how many times, we can shift. 1 shift or 2 or more shifts. What I want exists in PHP?
Something like this:
//get the array keys for the interns and students...
$intern_keys = array_keys($gStartPositionInternships);
$student_keys = array_keys($gStartPositionStudents);
//drop the last intern key off the end and pin it to the front.
array_unshift($intern_keys, array_pop($intern_keys));
//create a mapping array to join the two arrays together.
$student_to_intern_mapping = array();
foreach($student_keys as $key=>$value) {
$student_to_intern_mapping[$value] = $intern_keys[$key];
}
You'll need to modify it to suit the rest of your code, but hopefully this will demonstrate a technique you could use. Note the key line here is the one which does array_unshift() with array_pop(). The comment in the code should explain what it's doing.
I think you want to do array_slice($gPositionsStudents, 0, X) where X is the number of moves to shift. This slices of a number of array elements. Then do array_merge($gPositionsStudents, $arrayOfSlicedOfPositions); to append these to the end of the original array.
Then you can do an array_combine to create one array with key=>value pairs from both arrays.

Keeping top 5 values in PHP efficently

I'm writing a small algorithm in PHP that goes through n number of movies with ratings, and will store the top 5. I'm not reading from a datafile, but from a stream so I cannot simply order the movies by rating.
My question is what is the most efficent way to keep track of the top 5 rated movies as I read the stream? Currently I do the following:
Read in 5 movies (into an array called movies[]), with two keys movies[][name] and movies[][rating]
Order the array by movies[rating] using array_multisort() (highest rating now sits at movies[4])
Read in the next movie
If this new movie rating > movies[0][rating] then replace movies[0] with this new movie
Re-order the list
Repeat 3-5 until finished
My method works, but requires a sort on the list after every read. I believe this to be an expensive method mostly due to the fact that every time I use array_multisort() I must do a for loop on 5 movies just to build the index to sort on. Can anyone suggest a better way to approach this?
Linked lists would work here.
Build a linked list that chains the first 5 movies in the correct order. For each new movie, just start at the the end of the chain and walk it until your movie is between one with a higher rating and one with a lower rating. Then insert your link into the list here. If the movie was better than the worst (and thus your list is now 6 long), just remove the last link in the chain, and you are back to 5.
No sorting, no indexing.
Your algorithm looks fine. I am not sure how the arrays are implemented in PHP. From an algorithm point of view: use a heap instead of an array.
No point in re-sorting after every read since you really only need to insert a new entry. Use the following algorithm, it's likely to get you the best speed. It's basically an unrolled loop, not the most beautiful code.
set movies[0..4].rating to -1.
while more movies in stream:
read in next movie.
if movie.rating < movies[0].rating:
next while
if movie.rating < movies[1].rating:
movies[0] = movie
next while
if movie.rating < movies[2].rating:
movies[0] = movies[1]
movies[1] = movie
next while
if movie.rating < movies[3].rating:
movies[0] = movies[1]
movies[1] = movies[2]
movies[2] = movie
next while
if movie.rating < movies[4].rating:
movies[0] = movies[1]
movies[1] = movies[2]
movies[2] = movies[3]
movies[3] = movie
next while
movies[0] = movies[1]
movies[1] = movies[2]
movies[2] = movies[3]
movies[3] = movies[4]
movies[4] = movie
At the end, you have your sorted list of movies. If there's less than 5, those others will have a rating of -1 so you'll know they're invalid. This is assuming that the rating on a real movie is zero or greater but you can adjust the values if they're not.
If you need to adjust it for more than 5 movies, you can. The best bet would be to roll up the loop again. At some point, however, it's going to become more efficient to sort it than use this method. This method's only really good for a small data set.
My method works, but requires a sort on the list after every read.
No it doesn't, it only requires a sort after you find a new movie whos rating is > movies[0][rating].
This method seems efficient to me. You only sort occasionally when there's a new entry for the top 5, which will happen less the more movies you process.
How big is the list? I'm guessing it's not an option to keep the entire list in memory, and sort it at the end?
there is no need for two keys in array. array with name as key, and rating as value will do. Sort it with arsort();
the algorithm is not perfect, you can do it optimally with linked list. Although I think linked list implemented in PHP will be actually slower that function call to asort() for 6 elements. For big O estimation, you can assume that sorting 6 elements has constant time.
You'll only sort when you encounter movie rated higher then the actual, so in average case you'll do it less an less often, while progressing. You'll sort on every movie only in worst case scenario of having initial list sorted from lowest rated.
Here’s what I would do:
// let’s say get_next_movie () returns array with 'rating' and 'name' keys
while ($m = get_next_movie ()) {
$ratings[$m['rating']][] = $m['movie'];
$temp_ratings = $ratings;
$top5 = array ();
$rating = 5;
while (1) {
if (count ($temp_ratings[$rating])) {
$top5[] = array_shift ($temp_ratings[$rating]);
} elseif ($rating > 0) {
--$rating;
} else {
break;
}
}
// $top5 has current top 5 :-)
}
$ratings array looks like this, each rating has array of movies inside:
Array
(
[5] => Array
(
[0] => Five!
)
[3] => Array
(
[0] => Three
[1] => Threeeeee
[2] => Thr-eee-eee
)
[4] => Array
(
[0] => FOR
)
)
Maybe this can be of help.
class TopList {
private $items = array();
private $indexes = array();
private $count = 0;
private $total = 5;
private $lowest;
private $sorted = false;
public function __construct($total = null) {
if (is_int($total))
$this->total = $total;
$this->lowest = -1 * (PHP_INT_MAX - 1);
}
public function addItem($index, $item) {
if ($index <= $this->lowest)
return;
$setLowest = $this->count === $this->total;
if ($setLowest) {
/* //remove first added
$lowestIndex = array_search($this->lowest, $this->indexes);
/*/ //remove last added
$lowestIndex = end(array_keys($this->indexes, $this->lowest));
//*/
unset($this->indexes[$lowestIndex], $this->items[$lowestIndex]);
} else {
++$this->count;
$setLowest = $this->count === $this->total;
}
$this->indexes[] = $index;
$this->items[] = $item;
$this->sorted = false;
if ($setLowest)
$this->lowest = min($this->indexes);
}
public function getItems() {
if (!$this->sorted) {
array_multisort($this->indexes, SORT_DESC, $this->items);
$this->sorted = true;
}
return $this->items;
}
}
$top5 = new TopList(5);
foreach ($movies as $movie) {
$top5->addItem($movie['rating'], $movie);
}
var_dump($top5->getItems());

Categories