如何通过在 PHP 中的同一键中搜索不同数组中的值来找到和数组的值
How can I find the value of and array by searching a value in a different array in the same key in PHP
我的数组 $json
看起来像这样:
Array (
[0] => Array ( [poulecode] => 495271
[teamcode] => 277986
[teamnaam] => JO19-1 (0225 Onder 19 competitie (najaar)) )
[1] => Array ( [poulecode] => 500027
[teamcode] => 277986
[teamnaam] => JO19-1 (B3200 Zwaluwen jeugdbeker Onder 19 poule) )
[2] => Array ( [poulecode] => 524572
[teamcode] => 277986
[teamnaam] => JO19-1 (0227 Onder 19 competitie (voorjaar)) )
)
如何通过搜索包含特定字符串 "voorjaar" 的 teamnaam's ( JO19-1 (0227 Onder 19 competitie (voorjaar)) )
从关联的 poulecode (524572)
中挑选(设置变量 $competitiecode
)?
我一直在使用 array_filter
和 array_column
来解决这个问题,但我似乎无法弄清楚。
到目前为止,这是我的一段代码:
$json = json_decode($content_programma, true);
$Poulecode = array_column($json, 'poulecode');
$Teamnaam = array_column($json, 'teamnaam');
$findme = 'voorjaar';
$key = array_filter($findme, array_column($json, $Teamnaam));
$competitiecode = $Poulecode[$key];
您可以使用 foreach
循环:
$find = Array();
$search = 'voorjaar';
foreach($json as $j) {
if (strpos($j['teamnaam'], $search) !== false) {
$find[] = Array(
'poulecode' => $j['poulecode']
);
}
}
你的 array_filter()
应该得到这个参数:
$keys = array_filter(array_column($json, $Teamnaam), function($item) use ($findme){
if (strpos($item['teamnaam'], $findme) !== false) {
return true;
}
return false;
});
$keys
现在是一个数组。
<?php
$array = [
[
"poulecode" => 495271,
"teamcode" => 277986,
"teamnaam" => "JO19-1 (0225 Onder 19 competitie (najaar)) )"
], [
"poulecode" => 500027,
"teamcode" => 277986,
"teamnaam" => "JO19-1 (B3200 Zwaluwen jeugdbeker Onder 19 poule) )"
], [
"poulecode" => 524572,
"teamcode" => 277986,
"teamnaam" => "JO19-1 (0227 Onder 19 competitie (voorjaar)) )"
]
];
$keyword = "voorjaar";
// You can filter your array using array_filter function
$result = array_filter( $array, function( $item ) use ( $keyword ) {
return strpos( $item['teamnaam'], $keyword ) !== false;
});
// and then map result to your datastructure
$result_ids = array_map( function($item){
return $item['poulecode'];
}, $result );
var_dump($result_ids);
我的数组 $json
看起来像这样:
Array (
[0] => Array ( [poulecode] => 495271
[teamcode] => 277986
[teamnaam] => JO19-1 (0225 Onder 19 competitie (najaar)) )
[1] => Array ( [poulecode] => 500027
[teamcode] => 277986
[teamnaam] => JO19-1 (B3200 Zwaluwen jeugdbeker Onder 19 poule) )
[2] => Array ( [poulecode] => 524572
[teamcode] => 277986
[teamnaam] => JO19-1 (0227 Onder 19 competitie (voorjaar)) )
)
如何通过搜索包含特定字符串 "voorjaar" 的 teamnaam's ( JO19-1 (0227 Onder 19 competitie (voorjaar)) )
从关联的 poulecode (524572)
中挑选(设置变量 $competitiecode
)?
我一直在使用 array_filter
和 array_column
来解决这个问题,但我似乎无法弄清楚。
到目前为止,这是我的一段代码:
$json = json_decode($content_programma, true);
$Poulecode = array_column($json, 'poulecode');
$Teamnaam = array_column($json, 'teamnaam');
$findme = 'voorjaar';
$key = array_filter($findme, array_column($json, $Teamnaam));
$competitiecode = $Poulecode[$key];
您可以使用 foreach
循环:
$find = Array();
$search = 'voorjaar';
foreach($json as $j) {
if (strpos($j['teamnaam'], $search) !== false) {
$find[] = Array(
'poulecode' => $j['poulecode']
);
}
}
你的 array_filter()
应该得到这个参数:
$keys = array_filter(array_column($json, $Teamnaam), function($item) use ($findme){
if (strpos($item['teamnaam'], $findme) !== false) {
return true;
}
return false;
});
$keys
现在是一个数组。
<?php
$array = [
[
"poulecode" => 495271,
"teamcode" => 277986,
"teamnaam" => "JO19-1 (0225 Onder 19 competitie (najaar)) )"
], [
"poulecode" => 500027,
"teamcode" => 277986,
"teamnaam" => "JO19-1 (B3200 Zwaluwen jeugdbeker Onder 19 poule) )"
], [
"poulecode" => 524572,
"teamcode" => 277986,
"teamnaam" => "JO19-1 (0227 Onder 19 competitie (voorjaar)) )"
]
];
$keyword = "voorjaar";
// You can filter your array using array_filter function
$result = array_filter( $array, function( $item ) use ( $keyword ) {
return strpos( $item['teamnaam'], $keyword ) !== false;
});
// and then map result to your datastructure
$result_ids = array_map( function($item){
return $item['poulecode'];
}, $result );
var_dump($result_ids);