2

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];
LW001
  • 2,452
  • 6
  • 27
  • 36
Mark
  • 23
  • 3

3 Answers3

2

You may use foreach loop:

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

foreach($json as $j) {
    if (strpos($j['teamnaam'], $search) !== false) {
        $find[] = Array(
            'poulecode' =>  $j['poulecode']
        );
    }
}
Vladimir
  • 1,373
  • 8
  • 12
1
<?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);
Oleg Butuzov
  • 4,795
  • 2
  • 24
  • 33
  • This is getting somewhere.. I now get : array(1) { [2]=> int(524572) } as result fromt the var_dump. How do I set the $competiecode = "524572" ?. $competitiecode = $result_ids[2]; works but how can I get the "2" as a variable? – Mark Jan 29 '18 at 08:49
  • `$result_ids` is filtered ids of matched keyword. If you sure taht match will be only one or you are OK with a toppest argument you can simple push value out of the array using [`array_shift()`](https://secure.php.net/manual/en/function.array-shift.php) – Oleg Butuzov Jan 29 '18 at 08:52
  • Great. That works for me.. I will edit the post with answer. Thanks! – Mark Jan 29 '18 at 08:53
0

Your array_filter() should get this arguments:

$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.

MAZux
  • 911
  • 1
  • 15
  • 28
  • isn't it so that array_search only matches on exact match and not when containing a value? I tried replacing the array_filter with array_search, unfortunately, it is not working! Even tried using $find_me = '%voorjaar%'; but no luck.. – Mark Jan 29 '18 at 08:04
  • Yes, you're right. But I prefer to work on arrays using PHP arrays functions not loops. I gonna edit my answer with another approach. – MAZux Jan 29 '18 at 08:07
  • @Mark The thing is you're using `array_filter()` but not passing the right params to it, it should be like this `array_filter(array, callbackfunction())` – MAZux Jan 29 '18 at 08:09