Is there a way in PHP or Laravel 5.2 to streamline the following:
- iterate through array. In my example, a collection of industries
- add an item. In my example In my example, 'app_id' and padded uuid
- remove original item. In my case the original uuid element
Ideal Outcome:
Can I streamline function paddUuids($item)
and public function getIndustries()
Context (why do I want to do this):
Using native uuid with CSS doesn't work as class and ID because the first character cannot be numeric so I need to add alpha prefix to uuid. Second, Angular 1.5 seems to struggles with Uuid (even as string) or Uuid with hyphens i.e. keep getting duplicate index error.
class SegmentsController extends Controller
{
use UuidTrait;
/**
* @var Industry
*/
protected $industry;
/**
* SegmentsController constructor.
*/
public function __construct(Industry $industry)
{
$this->industry = $industry;
}
/**
* Remove uuid hypens and add prefix to use with Angular and as CSS class
* @param $item
* @return mixed
*/
function paddUuids($item)
{
$item['app_id'] = $this->paddUuid($item['uuid']);
unset($item['uuid']);
return $item;
}
/**
* Return Iterate through industries
* @return array
*/
public function getIndustries()
{
return array_map (array($this, 'paddUuids'), $this->industry->get()->toArray() );
}
}