HiveBrain v1.2.0
Get Started
← Back to all entries
patternphpMinor

Adjacency list with array data

Submitted by: @import:stackexchange-codereview··
0
Viewed 0 times
witharrayadjacencylistdata

Problem

I came through a question on Stack Overflow, which shows this array structure:

array (size=4)
  0 => 
    array (size=3)
      'id' => int 1
      'name' => string 'Anniversary' (length=11)
      'parent' => int 0
  1 => 
    array (size=3)
      'id' => int 12
      'name' => string 'New arrives' (length=11)
      'parent' => int 1
  2 => 
    array (size=3)
      'id' => int 13
      'name' => string 'Discount' (length=8)
      'parent' => int 12
  3 => 
    array (size=3)
      'id' => int 6
      'name' => string 'Birthday' (length=8)
      'parent' => int 0


and the required outcome needs to be the path for that list like shown below, implementation of adjacency list:

13 - Anniversary->New arrives->Discount
12 - Anniversary->New arrives
1 - Anniversary
6 - Birthday


I came to this solution:

$in = [
['id'=>1, 'name'=>'Anniversary', 'parent'=>0],
['id'=>12, 'name'=>'New arrives', 'parent'=>1],
['id'=>13, 'name'=>'Discount', 'parent'=>12 ],
['id'=>6, 'name'=>'Birthday', 'parent'=>0 ]
];

function path($element, $input) {
$return = $input[ array_search($element, array_column($input, 'id')) ];
$str [] = $return ['name'];
if($return ['parent'] != 0) {
$str [] = path($return ['parent'], $input);
}
return implode('->', array_reverse($str));
}

var_dump(path(13,$in));


It gets the task done, but I want to know if any improvements can be made to optimize the approach to the problem.

Solution

Your code is really good, so there's not much to improve on, that I can see, but nonetheless:

You shouldn't have whitespace before, or inside your brackets unless required:

$return = $input[ array_search($element, array_column($input, 'id')) ];
                    ^                                                  ^
   $str [] = $return ['name'];
       ^            ^
   if($return ['parent'] != 0) {
             ^
       $str [] = path($return ['parent'], $input);
           ^                 ^


All these lines above have unnecessary whitespace, however the line below is missing whitespace after the comma in path().

var_dump(path(13,$in));
                 ^


You really ought not to name your variables $return or $str, because the first is a system keyword, and the second is undescriptive & possibly confusing as it's really an array, not a string.

The implode string -> could also be -> with an extra space, or even a unicode long (or not long) rightwards arrow: , along with the extra space, visually output would be more readable, and better looking.

13 - Anniversary -> New arrives -> Discount // With added space
13 - Anniversary ⟶ New arrives ⟶ Discount // With added space and long rightwards arrow

Code Snippets

$return = $input[ array_search($element, array_column($input, 'id')) ];
                    ^                                                  ^
   $str [] = $return ['name'];
       ^            ^
   if($return ['parent'] != 0) {
             ^
       $str [] = path($return ['parent'], $input);
           ^                 ^
var_dump(path(13,$in));
                 ^
13 - Anniversary -> New arrives -> Discount // With added space
13 - Anniversary ⟶ New arrives ⟶ Discount // With added space and long rightwards arrow

Context

StackExchange Code Review Q#94669, answer score: 3

Revisions (0)

No revisions yet.