Skip to content

Instantly share code, notes, and snippets.

@ajuchacko
Last active November 3, 2018 13:09
Show Gist options
  • Save ajuchacko/54d676300497f8d78c38e83a6eb7cc8e to your computer and use it in GitHub Desktop.
Save ajuchacko/54d676300497f8d78c38e83a6eb7cc8e to your computer and use it in GitHub Desktop.
Create described structure from the given string (name of software can't be repeated even though years/versions can)
<?php
// Given String:
$string = "word_2007, word_2010, word_2007, sublime, phpstorm_2010, phpstorm_2010,
netbeans, netbeans, sublime, word_2007, phpstorm_2010, atom";
// Desired Structure:
// sample : ['word' => [2007, 2010], 'sublime', 'netbeans', 'phpstorm' => [2010], 'atom'];
/* 1. Solution
What I Have Been Trying to Write the whole time
*/
function string_to_array_one(String $string)
{
$result = [];
$array = explode(', ', $string);
$soft_version = array_map(function ($item) {
if (! strstr($item, '_')) {
return [$item];
}
return explode('_', $item);
}, $array);
$softwares = array_values(array_unique(array_column($soft_version, '0')));
foreach ($array as $key => $value) {
$i = 0;
while ($i < count($softwares)) {
$year = ltrim(substr($value, strpos($value, '_')), '_');
if (! strstr($value, '_') && (! in_array($value, $result)) ) {
$result[] = $value;
} elseif (strstr($value, '_') && strstr($value, $softwares[$i])) {
if (! array_key_exists($softwares[$i], $result)) {
$result[$softwares[$i]] = [];
} elseif(! in_array($year, $result[$softwares[$i]])) {
$result[$softwares[$i]][] = $year;
}
}
$i++;
}
}
return $result;
}
// 2. Solution
function string_to_array_two(String $string)
{
$result = [];
$array = explode(', ', $string);
array_walk($array, function($item, $key) use (&$result) {
$software = substr($item, 0, strpos($item, '_'));
$year = ltrim(substr($item, strpos($item, '_')), '_');
if (! strpos($item, '_')) {
$software = $item;
$year = null;
}
if (! key_exists($software, $result) && $year !== null) {
$result[$software] = [];
} elseif ($year == null) {
$result[] = $software;
} else {
$result[$software][] = $year;
}
});
return $result;
}
// 3. Solution
function string_to_array_three(String $string)
{
$array = explode(', ', $string);
foreach ($array as $key => $value) {
$soft_version[] = explode('_', $value);
}
$result = [];
foreach ($soft_version as $key => $value) {
if (! array_key_exists($value[0], $result)) {
$result[$value[0]] = [];
} elseif(! in_array($value[1], $result[$value[0]])) {
$result[$value[0]][] = $value[1];
}
continue;
}
array_map(function ($item, $key) use (&$result) {
if (empty($item)) {
$result[] = $key;
unset($result[$key]);
}
}, $result, array_keys($result));
return $result;
}
// 4. Solution
function string_to_array_four(String $string)
{
$base_array = explode(', ', $string);
foreach ($base_array as $value) {
if (! strpos($value, '_')) {
$no_version[] = $value;
continue;
}
$software_list[] = substr($value, 0, strpos($value, '_'));
}
$software_list = array_unique($software_list);
foreach ($base_array as $key => $base_value) {
foreach ($software_list as $key => $software) {
if (strstr($base_value, $software) && strpos($base_value, '_')) {
$year = ltrim(substr($base_value, strpos($base_value, '_')), '_');
$result[$software][] = $year;
}
}
}
$result = array_merge($no_version, $result);
array_walk($result, function($item, $key) use (&$result) {
if (is_array($item)) {
$result[$key] = array_unique($item);
}
});
return $result;
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment