foreach ($response as $object) {
$basename = basename($object);
$structure = explode("/", $object);
echo $structure[0] . '<br>';
...
}
If you want to prevent duplicates in echo'ing out $structure[0], you need to check whether you have had it already, e.g. by keeping a history:
$history = array();
foreach ($response as $object) {
$name = strstr($object, "/", true);
if ($name !== false && !isset($history[$name])) {
$history[$name] = 1;
echo $name . '<br>';
}
...
}
You probably want to stream line your code. Let's review:
foreach ($response as $object) {
$basename = basename($object);
$structure = explode("/", $object);
echo $structure[0] . '<br>';
}
The line $basename = basename($object); is not used. It can be removed:
foreach ($response as $object) {
$structure = explode("/", $object);
echo $structure[0] . '<br>';
}
Then you only need the part of the string until the first "/", the strstr function is handy for that:
foreach ($response as $object) {
$part = strstr($object, "/", true);
FALSE === $part && $part = $object;
echo $part . '<br>';
}
Now as we have already simplified it that much, we can create a simple mapping function:
$map = function($v) {
$k = strstr($v, "/", true);
FALSE === $k && $k = $v;
return $k;
};
and map the $response:
$mapped = array_map($map, $response);
and then unique it:
$unique = array_unique($mapped);
And job done. The code is much more easy to read:
$map = function($v) {
$k = strstr($v, "/", true);
FALSE === $k && $k = $v;
return $k;
};
$mapped = array_map($map, $response);
$unique = array_unique($mapped);
foreach ($unique as $name) {
echo $name, "<br>\n";
}
The additional benefit is here, in the moment you care about the output, the data to be outputted is already in order and properly available. As output itself counts as one part of your application, it should not be mixed with the data processing. See the IPO Model.