douzhang6496 2016-06-09 15:20
浏览 40

PHP简化添加和删除数组元素

Is there a way in PHP or Laravel 5.2 to streamline the following:

  1. iterate through array. In my example, a collection of industries
  2. add an item. In my example In my example, 'app_id' and padded uuid
  3. 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() );
        }
    }
  • 写回答

0条回答 默认 最新

    报告相同问题?

    悬赏问题

    • ¥15 对于squad数据集的基于bert模型的微调
    • ¥15 为什么我运行这个网络会出现以下报错?CRNN神经网络
    • ¥20 steam下载游戏占用内存
    • ¥15 CST保存项目时失败
    • ¥15 树莓派5怎么用camera module 3啊
    • ¥20 java在应用程序里获取不到扬声器设备
    • ¥15 echarts动画效果的问题,请帮我添加一个动画。不要机器人回答。
    • ¥15 Attention is all you need 的代码运行
    • ¥15 一个服务器已经有一个系统了如果用usb再装一个系统,原来的系统会被覆盖掉吗
    • ¥15 使用esm_msa1_t12_100M_UR50S蛋白质语言模型进行零样本预测时,终端显示出了sequence handled的进度条,但是并不出结果就自动终止回到命令提示行了是怎么回事: