简体   繁体   English

通过PHP数组搜索并返回值的问题

[英]Question on searching through a php array and return a value

Wondering how I can search through the following array for [law] and just return [fine] for that law? 想知道如何在以下数组中搜索[law]并仅返回[fine]该法律?

    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) ..(数组通过108个唯一项继续)

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] 我想通过json_decode搜索[law]并返回[fine]

Your $law variable has not the expected value and you should get an error like this: 您的$ law变量没有期望的值,您应该得到如下错误:

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: 关于隐藏的[fine]元素,您可以看一下这个问题,然后执行以下操作:

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

If all you need to do is pass the fine, then this would work: 如果您需要做的只是通过罚款,那么这将起作用:

echo "<select name=\"charge\">";
$strJsonFileContents = file_get_contents("./includes/laws.json");
$laws = json_decode($strJsonFileContents, true);

foreach ($laws as $law) {
    echo '<option name="law" value="' . $law['fine'] . '">' . $law['law'] . '</option>';
}
echo "</select><br><br>";

If you need to pass the law and the fine, then you'd need to use some javascript to store the json object, listen for the select field to change and then add the fine to a hidden field when it is changed by looping through the json object and grabbing the fine based on which law was chosen. 如果您需要通过法律和罚款,那么您需要使用一些JavaScript来存储json对象,侦听选择字段的更改,然后通过循环遍历罚款字段将罚款添加到隐藏字段中json对象,并根据选择的法律进行罚款。

I was way overthinking it.. seems this will achieve what I was after. 我想得太多了..看来这将实现我的追求。

$arrlength = count($issue);
for ($x = 0; $x < $arrlength; $x++) {
    if($issue[$x]['law'] == $law) { $fine = $issue[$x]['fine']; }
}

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM