使用 PHP 在 JSON 文件中搜索带有德语字符的字符串



JSON 文件:

{"verbs":[{"_id":1,"option1":"ändern","option2":"öl","option3":"über","answer":"über"},{"_id":2,"option1":"mit","option2":"aus ","option3":"zu","answer":"aus "}]}

法典:

<?php
$string = file_get_contents("http://xyz-abc.com/xyz.json");
$arrays = json_decode($string,true);
$found = array_search( "ändern", array_column( $arrays, 'option1' ) );
if( $found === False ) echo "Not Found";
else   echo $data[$found]['option1'];

我正在 JSON 文件中搜索"ändern"。我得到输出:未找到。任何人都可以在我的代码中找到错误吗?

这有效:

<?php
$string = '{"verbs":[{"_id":1,"option1":"ändern","option2":"öl","option3":"über","answer":"über"},{"_id":2,"option1":"mit","option2":"aus ","option3":"zu","answer":"aus "}]}';
$arrays = json_decode($string,true);
foreach ($arrays as $array) {
$needle = "ändern";
$found = array_search($needle, array_column($array, 'option1'));
if($found === false) {
echo 'Not Found: ' . $needle; 
} else {
echo 'Found: ' . $needle;
}
}

您找不到任何东西,因为您没有将$arrays视为多维数组,您搜索$arrays如下所示:

array(1) {
'verbs' =>
array(2) {
[0] =>
array(5) {
'_id' =>
int(1)
'option1' =>
string(7) "ändern"
'option2' =>
string(3) "öl"
'option3' =>
string(5) "über"
'answer' =>
string(5) "über"
}
[1] =>
array(5) {
'_id' =>
int(2)
'option1' =>
string(3) "mit"
'option2' =>
string(4) "aus "
'option3' =>
string(2) "zu"
'answer' =>
string(4) "aus "
}
}
}

最新更新