douxihui8270
2012-01-10 10:50 阅读 67
已采纳

Smarty从索引号获取数组

I am trying to do something like this:

{$order.delivery_country.options.labels[$order.delivery_country.value]}

So using the delivery_country.value I would like to return the option label. How could I do this in Smarty?

when I manually write {$order.delivery_country.options.labels.358} it works perfectly, although abviously this need to be dynamic and use the .value

[delivery_country] => Array
            (
                [name] => orders.order_delivery_country
                [value] => 358
                [label] => Delivery Country
                [message] => 
                [default] => 58
                [options] => Array
                    (
                        [name] => country-names
                        [labels] => Array
                            (
                                [0] => --- Select ---
                                [1] => Afghanistan
                                [2] => Aland Islands
                                [3] => Albania
                                [4] => Algeria
                                [5] => American Samoa
                                [6] => Andorra
                                [7] => Angola
                                [8] => Anguilla
                                [9] => Antarctica
                                [10] => Antigua and Barbuda
                                [11] => Argentina
                                [12] => Armenia
                                [13] => Aruba
                                [14] => Ascension Island
                                [15] => Australia
                                [16] => Austria
                                [17] => Azerbaijani
                                [18] => Azores
                                [19] => Azores
                                [20] => Azores
                                [21] => Bahamas
                                [22] => Bahrain
                                [23] => Bangladesh
                                [24] => Barbados
                                [25] => Belarus
                                [26] => Belgium
                                [27] => Belize
                                [28] => Benin
                                [29] => Bermuda
                                [30] => Bhutan
                                [31] => Bolivia
                                [32] => Bosnia and Herzegowina
                                [33] => Botswana
                                [34] => Bouvet Island
                                [35] => Brazil
                                [36] => Brunei Darussalam
                                [37] => Bulgaria
                                [38] => Burkina Faso
                                [39] => Burundi
                                [40] => Cambodia
                                [41] => Cameroon
                                [42] => Canada
                                [43] => Canada
                                [44] => Cape Verde
                                [45] => Cayman Islands
                                [46] => Central African Republic
                                [47] => Chad
                                [48] => Chile
                                [49] => China
                                [50] => Christmas Island
                                [51] => Cocos (Keeling) Islands
                                [52] => Colombia
                                [53] => Comoros
                                [54] => Congo, Democratic Republic of
                                [55] => Congo, People's Republic of
                                [56] => Cook Islands
                                [57] => Costa Rica
                                [58] => Cote d'Ivoire
                                [59] => Croatia
                                [60] => Cuba
                                [61] => Cyprus
                                [62] => Czech Republic
                                [63] => Denmark
                                [64] => Diego Garcia
                                [65] => Djibouti
                                [66] => Dominica
                                [67] => Dominican Republic
                                [68] => Dominican Republic
                                [69] => East Timor
                                [70] => Ecuador
                                [71] => Egypt
                                [72] => El Salvador
                                [73] => Equatorial Guinea
                                [74] => Eritrea
                                [75] => Estonia
                                [76] => Ethiopia
                                [77] => Falkland Islands
                                [78] => Faroe Islands
                                [79] => Fiji
                                [80] => Finland
                                [81] => France
                                [82] => French Guiana
                                [83] => French Polynesia
                                [84] => Gabon
                                [85] => Gambia
                                [86] => Georgia
                                [87] => Germany
                                [88] => Ghana
                                [89] => Gibraltar
                                [90] => Greece
                                [91] => Greenland
                                [92] => Grenada
                                [93] => Guadeloupe
                                [94] => Guam
                                [95] => Guatemala
                                [96] => Guinea
                                [97] => Guinea-Bissau
                                [98] => Guyana
                                [99] => Haiti
                                [100] => Heard and McDonald Islands
                                [101] => Holy See (Vatican City State)
                                [102] => Honduras
                                [103] => Hong Kong
                                [104] => Hungary
                                [105] => Iceland
                                [106] => India
                                [107] => Indonesia
                                [108] => Iran
                                [109] => Iraq
                                [110] => Ireland
                                [111] => Israel
                                [112] => Italy
                                [113] => Jamaica
                                [114] => Japan
                                [115] => Jordan
                                [116] => Kazakhstan
                                [117] => Kenya
                                [118] => Kiribati
                                [119] => Korea, Democratic People's Republic of
                                [120] => Korea, Republic of
                                [121] => Kuwait
                                [122] => Kyrgyzstan
                                [123] => Laos
                                [124] => Latvia
                                [125] => Lebanon
                                [126] => Lesotho
                                [127] => Liberia
                                [128] => Libya
                                [129] => Liechtenstein
                                [130] => Lithuania
                                [131] => Luxembourg

I would normally use this below in a booking create page, although I need only the country name on the booking confirm page.

<select id="delivery_country" name="{$order.delivery_country.formname}">
    {html_options values=$order.delivery_country.options.values output=$order.delivery_country.options.labels selected=$order.delivery_country.value}   
</select>

Update with var_dump output showing types

["delivery_country"]=>
array(15) {
  ["name"]=>
  string(29) "orders.order_delivery_country"
  ["value"]=>
  string(3) "358"
  ["label"]=>
  string(16) "Delivery Country"
  ["input"]=>
  string(6) "select"
  ["message"]=>
  string(0) ""
  ["options"]=>
  array(3) {
    ["name"]=>
    string(13) "country-names"
    ["labels"]=>
    array(249) {
      [0]=>
      string(14) "--- Select ---"
      [1]=>
      string(11) "Afghanistan"
      [2]=>
      string(13) "Aland Islands"
      [3]=>
      string(7) "Albania"
      [4]=>
      string(7) "Algeria"
      [5]=>
      string(14) "American Samoa"
      [6]=>
      string(7) "Andorra"
      [7]=>
      string(6) "Angola"
      [8]=>
      string(8) "Anguilla"
      [9]=>
      string(10) "Antarctica"
      [10]=>
      string(19) "Antigua and Barbuda"
      [11]=>
      string(9) "Argentina"
      [12]=>
      string(7) "Armenia"
      [13]=>
      string(5) "Aruba"
      [14]=>
      string(16) "Ascension Island"
      [15]=>
      string(9) "Australia"
      [16]=>
      string(7) "Austria"
      [17]=>
      string(11) "Azerbaijani"
      [18]=>
      string(6) "Azores"
      [19]=>
      string(6) "Azores"
      [20]=>
      string(6) "Azores"
      [21]=>
      string(7) "Bahamas"
      [22]=>
      string(7) "Bahrain"
      [23]=>
      string(10) "Bangladesh"
      [24]=>
      string(8) "Barbados"
      [25]=>
      string(7) "Belarus"
      [26]=>
      string(7) "Belgium"
      [27]=>
      string(6) "Belize"
      [28]=>
      string(5) "Benin"
      [29]=>
      string(7) "Bermuda"
      [30]=>
      string(6) "Bhutan"
      [31]=>
      string(7) "Bolivia"
      [32]=>
      string(22) "Bosnia and Herzegowina"
      [33]=>
      string(8) "Botswana"
      [34]=>
      string(13) "Bouvet Island"
      [35]=>
      string(6) "Brazil"
      [36]=>
      string(17) "Brunei Darussalam"
      [37]=>
      string(8) "Bulgaria"
      [38]=>
      string(12) "Burkina Faso"
      [39]=>
      string(7) "Burundi"
      [40]=>
      string(8) "Cambodia"
      [41]=>
      string(8) "Cameroon"
      [42]=>
      string(6) "Canada"
      [43]=>
      string(6) "Canada"
      [44]=>
      string(10) "Cape Verde"
      [45]=>
      string(14) "Cayman Islands"
      [46]=>
      string(24) "Central African Republic"
      [47]=>
      string(4) "Chad"
      [48]=>
      string(5) "Chile"
      [49]=>
      string(5) "China"
      [50]=>
      string(16) "Christmas Island"
      [51]=>
      string(23) "Cocos (Keeling) Islands"
      [52]=>
      string(8) "Colombia"
      [53]=>
      string(7) "Comoros"
      [54]=>
      string(29) "Congo, Democratic Republic of"
      [55]=>
      string(27) "Congo, People's Republic of"
      [56]=>
      string(12) "Cook Islands"
      [57]=>
      string(10) "Costa Rica"
      [58]=>
      string(13) "Cote d'Ivoire"
      [59]=>
      string(7) "Croatia"
      [60]=>
      string(4) "Cuba"
      [61]=>
      string(6) "Cyprus"
      [62]=>
      string(14) "Czech Republic"
      [63]=>
      string(7) "Denmark"
      [64]=>
      string(12) "Diego Garcia"
      [65]=>
      string(8) "Djibouti"
      [66]=>
      string(8) "Dominica"
      [67]=>
      string(18) "Dominican Republic"
      [68]=>
      string(18) "Dominican Republic"
      [69]=>
      string(10) "East Timor"
      [70]=>
      string(7) "Ecuador"
      [71]=>
      string(5) "Egypt"
      [72]=>
      string(11) "El Salvador"
      [73]=>
      string(17) "Equatorial Guinea"
      [74]=>
      string(7) "Eritrea"
      [75]=>
      string(7) "Estonia"
      [76]=>
      string(8) "Ethiopia"
      [77]=>
      string(16) "Falkland Islands"
      [78]=>
      string(13) "Faroe Islands"
      [79]=>
      string(4) "Fiji"
      [80]=>
      string(7) "Finland"
      [81]=>
      string(6) "France"
      [82]=>
      string(13) "French Guiana"
      [83]=>
      string(16) "French Polynesia"
      [84]=>
      string(5) "Gabon"
      [85]=>
      string(6) "Gambia"
      [86]=>
      string(7) "Georgia"
      [87]=>
      string(7) "Germany"
      [88]=>
      string(5) "Ghana"
      [89]=>
      string(9) "Gibraltar"
      [90]=>
      string(6) "Greece"
      [91]=>
      string(9) "Greenland"
      [92]=>
      string(7) "Grenada"
      [93]=>
      string(10) "Guadeloupe"
      [94]=>
      string(4) "Guam"
      [95]=>
      string(9) "Guatemala"
      [96]=>
      string(6) "Guinea"
      [97]=>
      string(13) "Guinea-Bissau"
      [98]=>
      string(6) "Guyana"
      [99]=>
      string(5) "Haiti"
      [100]=>
      string(26) "Heard and McDonald Islands"
      [101]=>
      string(29) "Holy See (Vatican City State)"
      [102]=>
      string(8) "Honduras"
      [103]=>
      string(9) "Hong Kong"
      [104]=>
      string(7) "Hungary"
      [105]=>
      string(7) "Iceland"
      [106]=>
      string(5) "India"
      [107]=>
      string(9) "Indonesia"
      [108]=>
      string(4) "Iran"
      [109]=>
      string(4) "Iraq"
      [110]=>
      string(7) "Ireland"
      [111]=>
      string(6) "Israel"
      [112]=>
      string(5) "Italy"
      [113]=>
      string(7) "Jamaica"
      [114]=>
      string(5) "Japan"
      [115]=>
      string(6) "Jordan"
      [116]=>
      string(10) "Kazakhstan"
      [117]=>
      string(5) "Kenya"
      [118]=>
      string(8) "Kiribati"
      [119]=>
      string(38) "Korea, Democratic People's Republic of"
      [120]=>
      string(18) "Korea, Republic of"
      [121]=>
      string(6) "Kuwait"
      [122]=>
      string(10) "Kyrgyzstan"
      [123]=>
      string(4) "Laos"
      [124]=>
      string(6) "Latvia"
      [125]=>
      string(7) "Lebanon"
      [126]=>
      string(7) "Lesotho"
      [127]=>
      string(7) "Liberia"
      [128]=>
      string(5) "Libya"
      [129]=>
      string(13) "Liechtenstein"
      [130]=>
      string(9) "Lithuania"
      [131]=>
      string(10) "Luxembourg"
      [132]=>
      string(5) "Macao"
      [133]=>
      string(9) "Macedonia"
      [134]=>
      string(10) "Madagascar"
      [135]=>
      string(7) "Madeira"
      [136]=>
      string(6) "Malawi"
      [137]=>
      string(8) "Malaysia"
      [138]=>
      string(8) "Maldives"
      [139]=>
      string(4) "Mali"
      [140]=>
      string(5) "Malta"
      [141]=>
      string(16) "Marshall Islands"
      [142]=>
      string(10) "Martinique"
      [143]=>
      string(10) "Mauritania"
      [144]=>
      string(9) "Mauritius"
      [145]=>
      string(7) "Mayotte"
      [146]=>
      string(6) "Mexico"
      [147]=>
      string(31) "Micronesia, Federated States of"
      [148]=>
      string(13) "Midway Island"
      [149]=>
      string(20) "Moldova, Republic of"
      [150]=>
      string(6) "Monaco"
      [151]=>
      string(8) "Mongolia"
      [152]=>
      string(10) "Montserrat"
      [153]=>
      string(7) "Morocco"
      [154]=>
      string(10) "Mozambique"
      [155]=>
      string(7) "Myanmar"
      [156]=>
      string(7) "Namibia"
      [157]=>
      string(5) "Nauru"
      [158]=>
      string(5) "Nepal"
      [159]=>
      string(11) "Netherlands"
      [160]=>
      string(20) "Netherlands Antilles"
      [161]=>
      string(13) "New Caledonia"
      [162]=>
      string(11) "New Zealand"
  • 点赞
  • 写回答
  • 关注问题
  • 收藏
  • 复制链接分享

2条回答 默认 最新

  • 已采纳
    dpdp42233 dpdp42233 2012-01-10 11:03

    in Smarty3 you could do

    {$order.delivery_country.options.labels.{$order.delivery_country.value}}
    

    in Smarty2 you could do

    {assign var="_country_value" value=$order.delivery_country.value}
    {$order.delivery_country.options.labels.$_country_value}
    
    点赞 评论 复制链接分享
  • douzhi2012 douzhi2012 2012-01-10 10:54

    Perhaps like this:

    {assign var=labelIndex value=$order.delivery_country.value}
    {$order.delivery_country.options.labels.$labelIndex}
    
    点赞 评论 复制链接分享

相关推荐