I need help with my array. For several hours trying to find a solution but I can not cope. I tried to do it in such a way, however, the code turned out to be completely useless.
private function findKey($array, $keySearch,$app,$klucz='')
{
foreach ($array as $key => $item) {
if ($key == $keySearch) {
$c2 = [];
$cache = [
'nazwa'=>$app->nazwa,
'link'=>$app->link,
'child'=>[]
];
// echo $klucz; echo '<br />';
if($klucz && $klucz != '/'){
// echo $klucz;
// $k = explode('/',$klucz);
// $url = "/2/22/";
// debug($klucz);
$parts = explode('/',$klucz);
debug($klucz);
debug($parts);
$x = 0;
$last = $parts[count($parts)-1];
$arr = array();
while ($bottom = array_pop($parts)) {
if($bottom != $last){
$arr = array($bottom => $arr);
$x++;
}else{
array_push($arr, $cache);
}
}
// $arr['child'] = $cache;
debug($arr);
// exit;
// print_r($output);
// exit;
// self::buildKey($k);
// print_r($c2);
echo "<br />";
}
}
else {
$klucz = $klucz.'/'.$key;
if (is_array($item) && self::findKey($item, $keySearch,$app,$klucz)) {
// return true;
}
}
}
// return false;
}
I need to create array from loop. From mysql I get data as object, and this data view like this:
|id|nazwa|link|parent|
|1|abc|abcc|0|
|2|aaa|bbb|1|
|3|aas|bbc|2|
|4|asdasd|adsasd|2|
|5|asdasd|serae|4|
|6|rywer|twet|0|
And now I need array whose use data from mysql and display it at array like this:
array(
[1]=>array(
id=>1,
nazwa=>abc
link=>abcc
child=>array(
[2]=>array(
id=>2,
nazwa=>aaa
link=>bbb
child=>array(
[3]=>array(
id=>3,
nazwa=>aas
link=>bbc
child=>array(
)
),
[4]=>array(
id=>4,
nazwa=>asdasd
link=>adsasd
child=>array(
[5]=>array(
id=>5,
nazwa=>asdasd
link=>serae
child=>array(
)
),
)
),
)
)
)
),
[6]=>array(
id=>6,
nazwa=>rywer
link=>twet
child=>array(
)
),
)
I think just a simple, good loop or function but I can not deal with it.