简体   繁体   English

如何通过在PHP的同一键中的其他数组中搜索值来找到and数组的值

[英]How can I find the value of and array by searching a value in a different array in the same key in PHP

My Array $json looks like this : 我的数组$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)) ) 
       )

How can I pick (set variable $competitiecode ) out of the asociated poulecode (524572) by searching the teamnaam's ( JO19-1 (0227 Onder 19 competitie (voorjaar)) ) containing a certain string "voorjaar"? 如何通过搜索包含特定字符串“ voorjaar”的teamnaam's ( JO19-1 (0227 Onder 19 competitie (voorjaar)) )从关联的poulecode (524572)挑选(设置变量$competitiecode )?

I have been working on this using array_filter and array_column but I can't seem to figure it out. 我一直在使用array_filterarray_column进行此工作,但似乎无法弄清楚。

This is a piece of my code so far: 到目前为止,这是我的一部分代码:

$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];

You may use foreach loop: 您可以使用foreach循环:

$find = Array();
$search = 'voorjaar';

foreach($json as $j) {
    if (strpos($j['teamnaam'], $search) !== false) {
        $find[] = Array(
            'poulecode' =>  $j['poulecode']
        );
    }
}
<?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);

Your array_filter() should get this arguments: 您的array_filter()应该获得以下参数:

$keys = array_filter(array_column($json, $Teamnaam), function($item) use ($findme){
    if (strpos($item['teamnaam'], $findme) !== false) {
        return true;
    }
    return false;
});

$keys is an array now. $keys现在是一个数组。

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

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