dongqu7778 2014-05-22 12:57
浏览 93
已采纳

使用串行键合并所有数组值

I am working on a Laravel app where the user can curate posted data with few options. So whenever user make a post he has given option to add category for that and fill more details depending on that category. On submit post data get saved with it's category id( i am saving this category and it's curated values to other table by putting it's id to post table).

array after posting data is like:

            Array
    (
  [title] => this is data 
   [subhead] => this is data this is data this is data this is data 
   [htmlcontent] => this is data this is data this is data this is data this is data this is data 
  [category] => story
   [author] => Array
    (
        [0] => author 1
        [1] => author 2
    )

[facts] => Array
    (
        [0] => facts
    )

[inspiration] => Array
    (
        [0] => insp1 
        [1] => insp2
    )

[tags] => Array
    (
        [0] => tag
    )

[refrence] => Array
    (
        [0] => ref1
        [1] => ref2
        [2] => ref3
    )

)

Now according to my situation, user can edit every single option, like he can edit author 1,2 and save it. for this i have to save every single value with identifier. Hence using author1 ,author2 to make it unique.

I have curated an option array for a post and it looks like this:

  Array ( [_id]       => 537ded7e8b5c880c1f000029
          [category]  => story 
          [author1]   => Anil Sharma 
          [author2]   => Tester 
          [citaion1]  => cited 
          [citaion2]  => cited2
          [tags]      => tag1,tag2,tag3
          [refrence1] => facebook.com
          [refrence2] => twitter
        )

And on the frontend I have to show the data like this:

   Expected Output
        category    story 
        author      Anil Sharma 
                    Tester 
        citaion     cited 
                    cited2
        tags        tag1,tag2,tag3
        refrence    facebook.com
                    twitter    


   Current Output

        category    story 
        author1      Anil Sharma 
        author2     Tester 
        citaion1     cited 
        citaion2     cited2
        tags        tag1,tag2,tag3
        refrence1    facebook.com
        refrence2    twitter                   

 HTML to show this :
    @foreach($category as $key => $value)
            <li>
             <label class="detailhead">{{$key}}</label>
             <p class="inlineedit" data-type="textarea" data-pk="{{$data->id}}" data-placement="left" data-url="{{URL::to('/')}}/ajax/post" data-title="Enter {{$key}}" id="{{$key}}_hoot">{{$value}}
             </p>

  </li>

  @endif

I can't make any change to key's name because they are the identifier for that part.On their value click user can edit the part with jQuery UI editables. User can curate any option and i have to save it accordingly. So what would be the best way to achieve this. should i make or filter array part in view section, but the thing I need to keep in mind is that whenever I am showing this thing into view I have to keep id="{{$key}}" unique in the tag. With its help, I am saving that particular curated part.

So what would be the best way for me to achieve this?

  • 写回答

1条回答 默认 最新

  • douzhang5121 2014-05-22 13:30
    关注

    You could do this (I've stripped the html css stuff out to make it easier to read):

    <?php $previousKey = null; ?>
    @foreach($category as $key => $value)
          <li>
              @if ((substr($key, 0, -1) != substr($previousKey, 0, -1))
                  <label class="detailhead">{{ $key }}</label>
              @endif
              <p>{{$value}}</p>
          </li>
          <?php $previousKey = $key; ?>
    @endif
    

    Its not the cleanest solution - you could refactor it to improve the code - but you get the idea. Basically it just compares the current key to the previous one with the number. So citation1 and citation2 would match, but only print the first citation key name.

    How is the initial array generated? Can you change that to a better structure to make your life easier?

    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论

报告相同问题?

悬赏问题

  • ¥15 如何让企业微信机器人实现消息汇总整合
  • ¥50 关于#ui#的问题:做yolov8的ui界面出现的问题
  • ¥15 如何用Python爬取各高校教师公开的教育和工作经历
  • ¥15 TLE9879QXA40 电机驱动
  • ¥20 对于工程问题的非线性数学模型进行线性化
  • ¥15 Mirare PLUS 进行密钥认证?(详解)
  • ¥15 物体双站RCS和其组成阵列后的双站RCS关系验证
  • ¥20 想用ollama做一个自己的AI数据库
  • ¥15 关于qualoth编辑及缝合服装领子的问题解决方案探寻
  • ¥15 请问怎么才能复现这样的图呀