duanche9384 2012-02-22 00:49
浏览 67
已采纳

PHP是目前的下一个关键部分?

Hey guys I'm trying to see where my pairs of keys stop, I have arrays built like this

EDIT People are getting really confused so I'm using a real array instead of an example

array (
  'key' => '',
  'po' => '',
  'label' => '',
  'report_key' => '',
  'shipper' => '',
  'status' => '',
  'location' => '',
  'inspector' => '',
  'commodity' => '',
  'brand' => '',
  'case_count' => '',
  'variety' => '',
  'style' => '',
  'grower_lot' => '',
  'pack_date' => '',

  // grouping 4 items
  'berry_size1' => '',
  'berry_size2' => '',
  'berry_size3' => '',
  'berry_size4' => '',

  // grouping 3 items
  'bunch_color1' => '',
  'bunch_color2' => '',
  'bunch_color3' => '',

  // grouping 2 items     
  'color1' => '',
  'color2' => '',


  // grouping 3 items
  'stem1' => '',
  'stem2' => '',
  'stem3' => '',


  // grouping 2 items
  'shatter1' => '',
  'shatter2' => '',


  // grouping 2 items
  'splits1' => '',
  'splits2' => '',


  // grouping 2 items
  'wet_sticky1' => '',
  'wet_sticky2' => '',

  'overall_quality' => '',


  // grouping 2 items
  'sugar_brix1' => '',
  'sugar_brix2' => '',

  'rating' => '',
  'comments' => '',
)

I came up with some stupid way that really doesn't work to try and sort things out, its extremely backwards, honestly I'm pretty embarrassed by my attempt.

            foreach($obj as $key=>$val) {

            if(strpos(  preg_replace('/[^a-z]/i', '', $key), 
                        preg_replace('/[^a-z]/i', '', $all_keys[$key+$b+1])
                        ) !== false) { echo "<p>$key</p>"; // items 1-3 will show
            } elseif(strpos(preg_replace('/[^a-z]/i', '', $key), 
                            preg_replace('/[^a-z]/i', '', $all_keys[$key+$b-1])
                            ) !== false) { echo "<p>$key</p>"; // show last item
            } else {

                  $in.='<aside class="left">';
                    $in .= "<label for='$key'>". ucwords(strtolower(str_replace('_',' ',$key))) ."</label><br/>";
                    $in .= ($key=='key') ? "<input type='text' value='". $objLastId ."' id='$key' class='disabled' disabled='disabled'>" : "<input type='text' value='' name='$key' id='$key'>";
                  $in.='</aside>';

              $b++;
              }
            }

Anyway what I'm really trying to achieve is something like this, could someone steer me in the right direction please?

<style>
 .row2 input {width: 50px !important;}
 .row3 input {width: 27px !important;}
 .row4 input {width: 15px !important;}
</style>

// stem was a 2 item group, so should have the row4 class
// and should have the second item appended by a &nbsp; 
// all be inside the same grouping, like below ...
<aside class="left row2">
<label for="color1">Color</label>
<br/><input type="text" value="" name="color1" id="color1">
&nbsp;<input type="text" value="" name="color2" id="color2">
</aside>


// stem was a 3 item group, so should have the row4 class
// and should have items 2-3 appended by a &nbsp; all be inside 
// the same grouping, like below ...
<aside class="left row3">
<label for="stem1">Stem</label>
<br><input type="text" id="stem1" name="stem1" value="">
&nbsp;<input type="text" id="stem2" name="stem2" value="">
&nbsp;<input type="text" id="stem3" name="stem3" value="">
</aside>


// berry_size was a 4 item group, so should have the row4 class
// and should have items 2-4 appended by a &nbsp; all be inside 
// the same grouping, like below ...
<aside class="left row4">
<label for="berry_size1">Berry Size</label>
<br/><input type="text" id="berry_size1" name="berry_size1" value="">
&nbsp;<input type="text" id="berry_size2" name="berry_size2" value="">
&nbsp;<input type="text" id="berry_size3" name="berry_size3" value="">
&nbsp;<input type="text" id="berry_size4" name="berry_size4" value="">
</aside>

... or ...

// this is a single, so no extra class and ....
<aside class="left">
<label for="other_item">Other Item</label>
<br/><input type="text" id="other_item" name="other_item" value="">
</aside>

What I see this really boiling down to is reading the next array keys name (I stripped the name and used the integer in my version), atleast I think that's the right way to do it?

  • 写回答

2条回答 默认 最新

  • dongxing2015 2012-02-22 01:02
    关注
      $arr = array(
        'other_item' => 'value',
    
        // this one ranges 1-3
        'first_name1' => 'value',
        'first_name2' => 'value',
        'first_name3' => 'value',
    
        // this one ranges 1-4
        'next_name1' => 'value',
        'next_name2' => 'value',
        'next_name3' => 'value',
        'next_name4' => 'value',
    
        'other_item' => 'value',
    
        // this one ranges 1-4
        'last_name1' => 'value',
        'last_name2' => 'value',
        'last_name3' => 'value',
        'last_name4' => 'value',
    
        'other_item' => 'value'
    );
    
    $newarr = array();
    foreach($arr as $key=>$value)
    {
        if (preg_match('#^([^\d]+)#', $key, $matches)===1)
         $newarr[$matches[1]][] = $value;  
    }
    print_r($newarr);
    

    Output:

    Array
    (
        [other_item] => Array
            (
                [0] => value
            )
    
        [first_name] => Array
            (
                [0] => value
                [1] => value
                [2] => value
            )
    
        [next_name] => Array
            (
                [0] => value
                [1] => value
                [2] => value
                [3] => value
            )
    
        [last_name] => Array
            (
                [0] => value
                [1] => value
                [2] => value
                [3] => value
            )
    
    )
    

    And do whatever you want to do with it. Like that code (just an example, not a very nice one)

    foreach($newarr as $name => $block)
    {
      $cnt = count($block);
      echo '<aside class="left'.($cnt>1?' row' . $cnt:'').'">
            <label for="' . $name . ($cnt>1?'1':''). '">' . 
                   ucwords(str_replace('_', ' ', $name)) . '</label>
            <br/>';
      foreach($block as $key=>$element)
      {
       echo ($key>0?'&nbsp;':'') . '<input type="text" value="" name="' . $name . 
            ($cnt>1?($key+1):'') . '" id="' . $name . 
            ($cnt>1?($key+1):'') . '">' . "
    ";
      }
      echo '</aside>' . "
    ";
    }
    

    It gives:

    <aside class="left">
    <label for="other_item">Other Item</label>
    <br/><input type="text" value="" name="other_item" id="other_item">
    </aside>
    <aside class="left row3">
    <label for="first_name1">First Name</label>
    <br/><input type="text" value="" name="first_name1" id="first_name1">
    &nbsp;<input type="text" value="" name="first_name2" id="first_name2">
    &nbsp;<input type="text" value="" name="first_name3" id="first_name3">
    </aside>
    <aside class="left row4">
    <label for="next_name1">Next Name</label>
    <br/><input type="text" value="" name="next_name1" id="next_name1">
    &nbsp;<input type="text" value="" name="next_name2" id="next_name2">
    &nbsp;<input type="text" value="" name="next_name3" id="next_name3">
    &nbsp;<input type="text" value="" name="next_name4" id="next_name4">
    </aside>
    <aside class="left row4">
    <label for="last_name1">Last Name</label>
    <br/><input type="text" value="" name="last_name1" id="last_name1">
    &nbsp;<input type="text" value="" name="last_name2" id="last_name2">
    &nbsp;<input type="text" value="" name="last_name3" id="last_name3">
    &nbsp;<input type="text" value="" name="last_name4" id="last_name4">
    </aside>
    
    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论
查看更多回答(1条)

报告相同问题?

悬赏问题

  • ¥15 素材场景中光线烘焙后灯光失效
  • ¥15 请教一下各位,为什么我这个没有实现模拟点击
  • ¥15 执行 virtuoso 命令后,界面没有,cadence 启动不起来
  • ¥50 comfyui下连接animatediff节点生成视频质量非常差的原因
  • ¥20 有关区间dp的问题求解
  • ¥15 多电路系统共用电源的串扰问题
  • ¥15 slam rangenet++配置
  • ¥15 有没有研究水声通信方面的帮我改俩matlab代码
  • ¥15 ubuntu子系统密码忘记
  • ¥15 保护模式-系统加载-段寄存器