php recursive get parents - php

I am trying to get jquery.treeview.js to fold out to the just created folder. Think I need an array of "parents" to set class to "open".
Other suggestions works with me ;-) (find this a bit too much but cannot find another way to do it.
Having an array like this:
array(7) {
[126]=>
array(4) {
["folder_id"]=>
string(3) "126"
["folder_name"]=>
string(3) "555"
["folder_parent"]=>
string(3) "125"
}
[2]=>
array(4) {
["folder_id"]=>
string(1) "2"
["folder_name"]=>
string(14) "Administration"
["folder_parent"]=>
string(1) "1"
}
[7]=>
array(4) {
["folder_id"]=>
string(1) "7"
["folder_name"]=>
string(5) "Britt"
["folder_parent"]=>
string(1) "2"
}
[4]=>
array(4) {
["folder_id"]=>
string(1) "4"
["folder_name"]=>
string(9) "Documents"
["folder_parent"]=>
string(1) "3"
}
[3]=>
array(4) {
["folder_id"]=>
string(1) "3"
["folder_name"]=>
string(14) "Infrastructure"
["folder_parent"]=>
string(1) "1"
}
[1]=>
array(4) {
["folder_id"]=>
string(1) "1"
["folder_name"]=>
string(4) "root"
["folder_parent"]=>
string(1) "0"
}
[125]=>
array(4) {
["folder_id"]=>
string(3) "125"
["folder_name"]=>
string(13) "test-deleteme"
["folder_parent"]=>
string(1) "7"
}
}
I would like to get the parents from selected folder_id.
getting data for folder_id=126 should return an array with parents {1,2,7,122}
Anyone?

Well, here is mine with recursive:
function getParent($folder_id, $data, $parents=array()) {
$parent_id = isset($data[$folder_id]) ? $data[$folder_id]['folder_parent'] : 0;
if ($parent_id > 0) {
array_unshift($parents, $parent_id);
return getParent($parent_id, $data, $parents);
}
return $parents;
}
//Usage
print_r(getParent(126, $your_folders));
It seems like I had plagiarized mancuernita's solution I hereby apologize. It's just similar, but I'm not copying!

You could do something like:
function yourFunction(id, array) {
sol = array();
while (id) {
id = find_parent(id, array);
array_push(sol, id);
}
return sol;
}
function find_parent(id, array) {
return array[id]["folder_parent"];
}
I haven't tried the code, and probably it needs some work more.

Related

Sort multidimensional array by specific key value

I'm working on algorithm to display my events on the website.
I want to sort my multidimensional array by specific key value.
My array:
["2022-02-28"]=>
array(1) {
[0]=>
array(3) {
["post_id"]=>
string(4) "3656"
["time"]=>
string(5) "16:05"
["priority"]=>
string(1) "0"
}
}
["2022-03-01"]=>
array(2) {
[2]=>
array(3) {
["post_id"]=>
string(4) "3656"
["time"]=>
string(5) "16:05"
["priority"]=>
string(1) "0"
}
[3]=>
array(3) {
["post_id"]=>
string(4) "3784"
["time"]=>
string(5) "13:00"
["priority"]=>
string(1) "0"
}
}
["2022-03-03"]=>
array(1) {
[5]=>
array(3) {
["post_id"]=>
string(4) "3663"
["time"]=>
string(5) "13:06"
["priority"]=>
string(1) "1"
}
}
}
I want to sort the array by "time" key value. So for example at this index :
["2022-03-01"]=>
array(2) {
[2]=>
array(3) {
["post_id"]=>
string(4) "3656"
["time"]=>
string(5) "16:05"
["priority"]=>
string(1) "0"
}
[3]=>
array(3) {
["post_id"]=>
string(4) "3784"
["time"]=>
string(5) "13:00"
["priority"]=>
string(1) "0"
}
}
I want first 13:00 to appear then 16:05. Thank you for your help in advance! :)
Try with this:
<?php
$arr = array();
$arr["2022-02-28"] = [
array("post_id"=>"3656", "time"=>"16:05", "priority"=>"0"),
array("post_id"=>"4856", "time"=>"13:05", "priority"=>"3")];
$arr["2022-03-01"] = [
array("post_id"=>"3656", "time"=>"16:05", "priority"=>"0"),
array("post_id"=>"3636", "time"=>"13:05", "priority"=>"1")
];
foreach($arr as $key => $value){
usort($value, function($a,$b){
return strtotime($a["time"])>strtotime($b["time"]);
});
$arr[$key] = $value;
}
echo "<pre>";
var_dump($arr);
echo "</pre>";
Output:
array(2) {
["2022-02-28"]=>
array(2) {
[0]=>
array(3) {
["post_id"]=>
string(4) "4856"
["time"]=>
string(5) "13:05"
["priority"]=>
string(1) "3"
}
[1]=>
array(3) {
["post_id"]=>
string(4) "3656"
["time"]=>
string(5) "16:05"
["priority"]=>
string(1) "0"
}
}
["2022-03-01"]=>
array(2) {
[0]=>
array(3) {
["post_id"]=>
string(4) "3636"
["time"]=>
string(5) "13:05"
["priority"]=>
string(1) "1"
}
[1]=>
array(3) {
["post_id"]=>
string(4) "3656"
["time"]=>
string(5) "16:05"
["priority"]=>
string(1) "0"
}
}
Use usort for define custom sort.
function time_sort(array $arr){
usort($arr, function($a, $b){
return strcmp($a['time'], $b['time']);
});
}

two array foreach echo

I am having trouble with showing my data. i have 2 arrays, i want to check it if array 2 is equal to array 1. in the sample array below the output should be like this. Any answers would be appreciated. i am a newbie
100000, 010000, 010000, 000001, 000001, 100000, 100000
array 1 data
array(6) {
[0]=>
object(stdClass)#370 (1) {
["id"]=>
string(1) "1"
}
[1]=>
object(stdClass)#369 (1) {
["id"]=>
string(1) "2"
}
[2]=>
object(stdClass)#368 (1) {
["id"]=>
string(1) "3"
}
[3]=>
object(stdClass)#367 (1) {
["id"]=>
string(1) "4"
}
[4]=>
object(stdClass)#366 (1) {
["id"]=>
string(1) "5"
}
[5]=>
object(stdClass)#365 (1) {
["id"]=>
string(1) "6"
}
}
array 2 data
array(7) {
[0]=>
object(stdClass)#354 (1) {
["stage_id"]=>
string(1) "1"
}
[1]=>
object(stdClass)#355 (1) {
["stage_id"]=>
string(1) "2"
}
[2]=>
object(stdClass)#353 (1) {
["stage_id"]=>
string(1) "2"
}
[3]=>
object(stdClass)#352 (1) {
["stage_id"]=>
string(1) "6"
}
[4]=>
object(stdClass)#378 (1) {
["stage_id"]=>
string(1) "6"
}
[5]=>
object(stdClass)#377 (1) {
["stage_id"]=>
string(1) "1"
}
[6]=>
object(stdClass)#376 (1) {
["stage_id"]=>
string(1) "1"
}
}
This is what i have tried and doesnt work. it only shows 100000
foreach ($this->stage as $index => $object)
{
if (isset($this->items[$index]) && $object->id == $this->items[$index]->stage_id)
{
echo 1;
}
else
{
echo 0;
}
}

How to create new arrays from object array based on user_id?

I'm having trouble looping through an object array and displaying data from it under certain conditions.
I want the webpage to display the following:
<p>John Doe</p> <p>William Green</p> <p>Jane Smith</p>
I know how to do this by defining $user[0]->first_name . $user[0]->last_name specifically, but what I need is a way for the code to loop through and display the names dynamically using the user_id property as a unique identifier.
For all objects in the array with user_id 1, return "these values." For all the objects in the array with user_id 2, return "these values." And so on...looping through each unique user_id.
Here is a var_dump of the object array:
array(4)
{ [0]=> object(stdClass)#4336 (4)
{
["umeta_id"]=> string(1) "1"
["user_id"]=> string(1) "1"
["meta_key"]=> string(10) "first_name"
["meta_value"]=> string(4) "John"
}
[1]=> object(stdClass)#4333 (4)
{
["umeta_id"]=> string(1) "2"
["user_id"]=> string(1) "1"
["meta_key"]=> string(9) "last_name"
["meta_value"]=> string(3) "Doe"
}
[2]=> object(stdClass)#4334 (4)
{
["umeta_id"]=> string(1) "3"
["user_id"]=> string(1) "2"
["meta_key"]=> string(10) "first_name"
["meta_value"]=> string(4) "Jane"
}
[3]=> object(stdClass)#4334 (4)
{
["umeta_id"]=> string(1) "4"
["user_id"]=> string(1) "2"
["meta_key"]=> string(9) "last_name"
["meta_value"]=> string(5) "Smith"
}
[4]=> object(stdClass)#4334 (4)
{
["umeta_id"]=> string(1) "5"
["user_id"]=> string(1) "3"
["meta_key"]=> string(10) "first_name"
["meta_value"]=> string(7) "William"
}
[5]=> object(stdClass)#4334 (4)
{
["umeta_id"]=> string(1) "6"
["user_id"]=> string(1) "3"
["meta_key"]=> string(9) "last_name"
["meta_value"]=> string(5) "Green"
}
}
When I output the information on the webpage, I want it to look like this:
<p>John Doe</p> <p>William Green</p> <p>Jane Smith</p>
Maybe I need to loop through objects and build new arrays based on user_ids? Please do not provide code with echo or print_r. Only "return" can be used for this project.
So $your_object is already sorted by umeta_id and user_id fields?
If not, you can use usort:
function cmp1($a, $b)
{
return strcmp($a->umeta_id, $b->umeta_id);
}
function cmp2($a, $b)
{
return strcmp($a->user_id, $b->user_id);
}
usort($your_object, "cmp1");
usort($your_object, "cmp2");
Then simply:
$str = '';
for($i=0;$i<count($your_object);$i+=2){
$str .= "<p>".$your_object[$i]->meta_value." "$your_object[$i+1]->meta_value."</p> ";
}
return $str; // this var contain all your data in string
Hope this helps!
This will generate the first-name and last-name pairs :
$user_group = array();
ksort($user_group, SORT_NUMERIC);
foreach ($users as $key => $item) {
$name_meta = get_object_vars($item);
foreach ($name_meta as $meta_in => $names) {
if ($names == 'first_name') {
$user_group[$item->user_id]['first_name'] = $name_meta['meta_value'];
}
if ($names == 'last_name') {
$user_group[$item->user_id]['last_name'] = $name_meta['meta_value'];
}
}
}
Output :
array(3) {
[1]=>
array(2) {
["first_name"]=>
string(4) "John"
["last_name"]=>
string(3) "Doe"
}
[2]=>
array(2) {
["first_name"]=>
string(4) "Jane"
["last_name"]=>
string(5) "Smith"
}
[3]=>
array(2) {
["first_name"]=>
string(7) "William"
["last_name"]=>
string(5) "Green"
}
}
And to print the user name pairs :
foreach ($user_group as $name) {
echo "<p>" . $name['first_name'] . " " . $name['last_name'] . "</p>";
}

grouping array values like mysqli 'like' functionality

friends, i don't it's possible or not, anyway i want to group users who have same like preference names ( mysql 'like' functionality)
[0]=>
array(5) {
["id"]=>"21"
["user_id"]=>"58"
["preference_id"]=>"4"
["sub_preference_id"]=>"7"
["preference_name"]=>"stephens collage"
}
[1]=>
array(5) {
["id"]=>"22"
["user_id"]=>"52"
["preference_id"]=>"4"
["sub_preference_id"]=>"1"
["preference_name"]=>"st stephens"
}
[2]=>
array(5) {
["id"]=>"25"
["user_id"]=>"61"
["preference_id"]=>"4"
["sub_preference_id"]=>"9"
["preference_name"]=>"joseph"
}
means i want output as like this :
1]stephens collage, st stephens
2] joseph
i tried this code :
$school_preference ="SELECT * FROM gic_user_preference where preference_id='4'";
$result_school_preference = mysqli_query($createCon->connect(), $school_preference);
$school = array();
while ($show_school_preference = mysqli_fetch_assoc($result_school_preference)) {
$school_preference = $show_school_preference['sub_preference_id'];
$get_all_school_preference =mysqli_query($createCon->connect(),"SELECT gic_user_preference.id,gic_user_preference.user_id,gic_user_preference.preference_id,gic_user_preference.sub_preference_id,gic_user_wise_school_preference.preference_name FROM gic_user_preference INNER JOIN gic_user_wise_school_preference ON gic_user_preference.sub_preference_id = gic_user_wise_school_preference.id where gic_user_preference.sub_preference_id='$school_preference'");
while ($schools = mysqli_fetch_assoc($get_all_school_preference)) {
$school[$schools['preference_name']][] = $schools;
}
}
it's give the output
array(4) {
["stephens collage"]=>
array(1) {
[0]=>
array(5) {
["id"]=>
string(2) "21"
["user_id"]=>
string(2) "58"
["preference_id"]=>
string(1) "4"
["sub_preference_id"]=>
string(1) "7"
["preference_name"]=>
string(16) "stephens collage"
}
}
["st stephens"]=>
array(1) {
[0]=>
array(5) {
["id"]=>
string(2) "22"
["user_id"]=>
string(2) "52"
["preference_id"]=>
string(1) "4"
["sub_preference_id"]=>
string(1) "1"
["preference_name"]=>
string(11) "st stephens"
}
}
["joseph"]=>
array(1) {
[0]=>
array(5) {
["id"]=>
string(2) "25"
["user_id"]=>
string(2) "61"
["preference_id"]=>
string(1) "4"
["sub_preference_id"]=>
string(1) "9"
["preference_name"]=>
string(6) "joseph"
}
}
}
any idea how to grouping this data as mysqli like function works ...
I think you can use regular expression match for find same names!

how to return multidimensional array in codeigniter

guys i have multidimensional array which i got it from var_dump of $menu_order with this following array :
array(5) {
[0]=>
array(1) {
[0]=>
array(1) {
["variant_name"]=>
string(5) "Spicy"
}
}
[1]=>
array(2) {
[0]=>
array(1) {
["variant_name"]=>
string(5) "Spicy"
}
[1]=>
array(1) {
["variant_name"]=>
string(5) "small"
}
}
[2]=>
array(2) {
[0]=>
array(1) {
["variant_name"]=>
string(5) "Salty"
}
[1]=>
array(1) {
["variant_name"]=>
string(6) "medium"
}
}
[3]=>
array(2) {
[0]=>
array(1) {
["variant_name"]=>
string(12) "Mix of Herbs"
}
[1]=>
array(1) {
["variant_name"]=>
string(5) "large"
}
}
[4]=>
array(0) {
}
}
from that array, i need to get the variant_name become variant_menu_id with this following code :
foreach ($menu_order as $item) {
if (isset($item[0]["variant_name"])) {
foreach($item as $value) {
$variant_id[] = $this->Main_home_m->m_get_choice_id($value["variant_name"]);
}
} else {
$variant_id[] = array();
}
}
the model of m_get_choice_id have this following code :
Function m_get_choice_id($variant_name){
$this->db->select("variant_menu_id");
$this->db->from("uhd_variant_menu");
$this->db->where("variant_name",$variant_name);
$query = $this->db->get();
return $query->row_array();
}
the variant_id will be return to this multidimensional array :
array(8) {
[0]=>
array(1) {
["variant_menu_id"]=>
string(1) "3"
}
[1]=>
array(1) {
["variant_menu_id"]=>
string(1) "3"
}
[2]=>
array(1) {
["variant_menu_id"]=>
string(1) "6"
}
[3]=>
array(1) {
["variant_menu_id"]=>
string(1) "4"
}
[4]=>
array(1) {
["variant_menu_id"]=>
string(1) "7"
}
[5]=>
array(1) {
["variant_menu_id"]=>
string(1) "5"
}
[6]=>
array(1) {
["variant_menu_id"]=>
string(1) "8"
}
[7]=>
array(0) {
}
}
but i want the result variant_id become this multidimensional array :
array(5) {
[0]=>
array(1) {
[0]=>
string(1) "3"
}
[1]=>
array(2) {
[0]=>
string(1) "3"
[1]=>
string(1) "6"
}
[2]=>
array(2) {
[0]=>
string(1) "4"
[1]=>
string(1) "7"
}
[3]=>
array(2) {
[0]=>
string(1) "5"
[1]=>
string(1) "8"
}
[4]=>
array(0) {
}
}
guys can you help me how to get the multidimensional array?
thank you (:
Alternatively, you can create a temporary container holding the ids with an array. After getting them all as an array, push that whole batch inside a parent container:
$result = array();
foreach ($menu_order as $item) {
$temp = array(); // initialize temporary storage
if (isset($item[0]["variant_name"])) {
foreach($item as $value) {
$variant = $this->Main_home_m->m_get_choice_id($value["variant_name"]);
$temp[] = $variant['variant_menu_id']; // push single id into temporary storage
}
}
$result[] = $temp; // push ending batch
}

Categories