dongyong2063 2018-01-29 07:46
浏览 41
已采纳

如何通过在PHP中的同一个键中搜索不同数组中的值来查找和数组的值

My Array $json looks like this :

 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"?

I have been working on this using array_filter and array_column but I can't seem to figure it out.

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];
  • 写回答

3条回答 默认 最新

  • douhuanbai6729 2018-01-29 08:10
    关注
    <?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);
    
    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论
查看更多回答(2条)

报告相同问题?

悬赏问题

  • ¥60 pb数据库修改或者求完整pb库存系统,需为pb自带数据库
  • ¥15 spss统计中二分类变量和有序变量的相关性分析可以用kendall相关分析吗?
  • ¥15 拟通过pc下指令到安卓系统,如果追求响应速度,尽可能无延迟,是不是用安卓模拟器会优于实体的安卓手机?如果是,可以快多少毫秒?
  • ¥20 神经网络Sequential name=sequential, built=False
  • ¥16 Qphython 用xlrd读取excel报错
  • ¥15 单片机学习顺序问题!!
  • ¥15 ikuai客户端多拨vpn,重启总是有个别重拨不上
  • ¥20 关于#anlogic#sdram#的问题,如何解决?(关键词-performance)
  • ¥15 相敏解调 matlab
  • ¥15 求lingo代码和思路