循环以在php中创建选项数据

时间:2018-01-11 03:37:31

标签: php

数据

    Array
        (
            [0] => Array
                (
                    [name] => USPS First Class Package (2 To 3 business days)
                    [value] => USPSFirstClass
                    [category] => STANDARD
                )

            [1] => Array
                (
                    [name] => USPS Priority Mail (1 To 3 business days)
                    [value] => USPSPriority
                    [category] => EXPEDITED
                )

            [2] => Array
                (
                   [name] => FedEx Ground or FedEx Home Delivery (1 To 5 
 business days)
                   [value] => FedExHomeDelivery
                   [category] => STANDARD
        )

            [3] => Array
              (
                  [name] => UPS Ground (1 To 5 business days)
                  [value] => UPSGround
                  [category] => EXPEDITED
              )

        )

问题:以上数据我想将其作为带有optgroup标签的下拉列表循环。下拉数据将显示为

标准

USPS头等舱套餐(2至3个工作日)

FedEx Ground或FedEx送货上门(1至5      工作日)

加速

美国邮政优先邮件(1至3个工作日)

UPS地面(1至5个工作日)

我该怎么做:(

4 个答案:

答案 0 :(得分:0)

您可以执行以下操作:

<div class="form-group form-group-search" style="margin-top:1.5em;">
    <div class="input-group" style="width:92%; margin:auto">
        <input id="txt-search" type="text" class="form-control" placeholder="Search Keyword..." />
        <span class="input-group-btn">
            <a class="btn btn-default" id ="btn-search" data-toggle="tab" href="javascript:void(0)">
            <span class="fa fa-search"></span></a>
        </span>
    </div>
</div>

将您的类别分类到一个数组中:

$(document).on("click", "#btn-search", function () {

    if ($("#txt-search").val() == "")
    {
        console.log('working');
    } else{
        $('#div-search').show();
        //$('#div-search').addClass('in'); //if you use bootstrap
        //write your code
    }

});

并循环播放:

#set paths up
$filepath= 'C:\folder\path\bond.out'
$filepath2= 'C:\folder\path\temp.txt'
$Ticklist='C:\folder\path\tick.txt'
$ratelist='C:\folder\path\rate.txt'

#Import needed data from an excel file which creates and array
$csv = Import-CSV C:\folder\path\RateIDTable.csv | Where { $_.'Rate' -ne "" } | Export-Csv C:\folder\path\out.csv -NoTypeInformation
$bond = Import-CSV C:\folder\path\out.csv | select -Property TickerID, Rate

#Put array from Excel file into two text files
$Tick = $bond | foreach-object {$_.TickerID} | set-content $Ticklist
$replace = $bond | foreach-object {$_.rate} | set-content $Ratelist

#Create two separate arrays from the new text files
$Tickdata = (Get-content $Ticklist ) -join ','
 foreach ($t in $Tickdata)
  {
  $t = $t -split(",")
}

$Ratedata = (Get-content $Ratelist ) -join ','
 foreach ($r in $Ratedata)
  {
  $r = $r -split(",")
}  

#Get main file to search (bond.out) and search for the word that is in the first line from "t" array file

###Replace all pipes with a comma

(get-content $filepath) -replace('\|', ',') | set-content $filepath
$data = Select-String $filepath -pattern $t[0] | Select-Object -ExpandProperty Line
$data

#Once found, split the line, replace the rate on the 3rd line with the rate in the first line from the "r" array file, the put the line back to together
$split=$data.split("{,}")
$split[3]=$r[0]
$join = $split -join ","

#Put the updated line back into the "bond.out" file from whence it came

###change all commas back to pipes

(get-content $filepath) -replace($data,$j) | set-content $filepath
(get-content $filepath) -replace(',', '|') | set-content $filepath
#computer says yay :D

答案 1 :(得分:0)

像这样的东西

<?php
$data = [
    [
        'name' => 'USPS First Class Package (2 To 3 business days)',
        'value' => 'USPSFirstClass',
        'category' => 'STANDARD'
    ],
    [
        'name' => 'USPS Priority Mail (1 To 3 business days)',
        'value' => 'USPSPriority',
        'category' => 'EXPEDITED'
    ],
    [
        'name' => 'FedEx Ground or FedEx Home Delivery (1 To 5
 business days)',
        'value' => 'FedExHomeDelivery',
        'category' => 'STANDARD'
    ],
    [
        'name' => 'UPS Ground (1 To 5 business days)',
        'value' => 'UPSGround',
        'category' => 'EXPEDITED'
    ]
];

function _group_by($array, $key)
{
    $return = array();
    foreach ($array as $val) {
        $return[$val[$key]][] = $val;
    }

    return $return;
}

$result = _group_by($data, 'category');
?>

<select>
    <?php foreach ($result as $key => $value): ?>
        <optgroup label="<?php echo $key ?>">
            <?php foreach ($value as $v): ?>
                <option value="<?php echo $v['value'] ?>"><?php echo $v['name'] ?></option>
            <?php endforeach; ?>
        </optgroup>
    <?php endforeach; ?>
</select>

_group_by函数根据您的密钥

分隔您的数组

答案 2 :(得分:0)

你也可以试试这个:

1)为标准和加速值创建数组

$standard=array();
$expedited=array();

2)循环遍历数组

foreach($array_1 as $k=>$v) {
    if($v['category'] == 'STANDARD'){
        array_push($standard,$v['name']);
    }
    if($v['category'] == 'EXPEDITED'){
        array_push($expedited,$v['name']);
    }
}

3)打印您需要的内容

print("STANDARD");
print nl2br("\n");
foreach($standard as $s){
    print_r($s);
    print nl2br("\n");
}

print nl2br("\n");
print("EXPEDITED");
print nl2br("\n");
foreach($expedited as $e){
    print($e);
    print nl2br("\n");
}

最终输出将是:

STANDARD
USPS First Class Package (2 To 3 business days)
FedEx Ground or FedEx Home Delivery (1 To 5 )

EXPEDITED
USPS Priority Mail (1 To 3 business days)
UPS Ground (1 To 5 business days)

答案 3 :(得分:-1)

您可以使用array_multisort函数

按类别对其进行排序

然后循环。