doulang1945 2018-12-14 18:29
浏览 31

关于搜索php数组并返回值的问题

Wondering how I can search through the following array for [law] and just return [fine] for that law?

    Array ( [0] => Array ( 
[law] => Unroadworthy Vehicle 
[fine] => 500 
[jail] => 0
[statute] => Any registered vehicle that is lacking headlights, taillights, windshields, has extensive damage, or deemed unsafe to operate, can be considered unroadworthy. Vehicles to do not return a registration shall also be considered unroadworthy. )
            [1] => Array ( 
[law] => Headlights Required 
[fine] => 500 
[jail] => 0 
[statute] => Failure to use headlights after dark or in poorly lit areas or roadways )

..(array continues through 108 unique items)

I have this to loop through and display the law in a dropdown

echo "<select name=\"charge\">";
$strJsonFileContents = file_get_contents("./includes/laws.json");
$issue = json_decode($strJsonFileContents, true);
$arrlength = count($issue);
for ($x = 0; $x < $arrlength; $x++)
    {
    $law = $issue[$x][law];
    echo "<option name=\"law\" value=\"$law\">$law</option>";
    }
echo "</select><br><br>";

I'm actually not going to do the hidden element part.

I want to search through json_decode for [law] and return it's [fine]

  • 写回答

3条回答 默认 最新

  • douyuan6490 2018-12-14 18:40
    关注

    Your $law variable has not the expected value and you should get an error like this:

    Use of undefined constant law - assumed 'law'

    echo "<select name=\"charge\">";
    $strJsonFileContents = file_get_contents("./includes/laws.json");
    $issue = json_decode($strJsonFileContents, true);
    $arrlength = count($issue);
    for ($x = 0; $x < $arrlength; $x++)
        {
        $law = $issue[$x]['law'];
        echo "<option name=\"law\" value=\"$law\">$law</option>";
        }
    echo "</select><br><br>";
    

    About the hidden [fine] element you can have a look to this question and do something like this:

    $('#mySelect').change(function(){
       var id = $(this).find(':selected')[0].id;
       $('#hiddenFineInput').val(id);
    })
    
    评论

报告相同问题?

悬赏问题

  • ¥100 set_link_state
  • ¥15 虚幻5 UE美术毛发渲染
  • ¥15 CVRP 图论 物流运输优化
  • ¥15 Tableau online 嵌入ppt失败
  • ¥100 支付宝网页转账系统不识别账号
  • ¥15 基于单片机的靶位控制系统
  • ¥15 真我手机蓝牙传输进度消息被关闭了,怎么打开?(关键词-消息通知)
  • ¥15 装 pytorch 的时候出了好多问题,遇到这种情况怎么处理?
  • ¥20 IOS游览器某宝手机网页版自动立即购买JavaScript脚本
  • ¥15 手机接入宽带网线,如何释放宽带全部速度