36898

How can I find the value of and array by searching a value in a different array in the same key in P

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

Answer1:

<?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);

Answer2:

You may use foreach loop:

$find = Array(); $search = 'voorjaar'; foreach($json as $j) { if (strpos($j['teamnaam'], $search) !== false) { $find[] = Array( 'poulecode' => $j['poulecode'] ); } }

Answer3:

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.

Recommend

  • Apply CSS to single instance of Custom user Control in ASP:NET
  • Only the first and third element printed from python list to csv file
  • C++ cout and enum representations
  • XSLT foreach repeating nodes to flat
  • How to create a 2D image by rotating 1D vector of numbers around its center element?
  • git add error : “fatal : malloc, out of memory”
  • Why the SequenceFile is truncated?
  • Thread 1: EXC_BAD_ACCESS (code =1 address = 0x0)
  • DIV instruction jumping to random location?
  • How to make JSON.NET deserialize to Microsoft Date Time?
  • Extracting HTML between tags
  • javaw.exe and eclipse startup problems
  • Date difference with leap year
  • How would I use PHP exceptions to define a redirect?
  • DirectX11 ClearRenderTargetViewback with transparent buffer?
  • Does CUDA 5 support STL or THRUST inside the device code?
  • Join two tables and save into third-sql
  • JSON with duplicate key names losing information when parsed
  • Display Images one by one with next and previous functionality
  • Why is the timeout on a windows udp receive socket always 500ms longer than set by SO_RCVTIMEO?
  • Weird JavaScript statement, what does it mean?
  • How do you troubleshoot character encoding problems?
  • Matplotlib draw Spline from multiple points
  • XCode can't find symbols for a specific iOS library/framework project
  • Calling of Constructors in a Java
  • Compare two NSDates in iPhone
  • How to format a variable of double type
  • Transpose CSV data with awk (pivot transformation)
  • Use group_by to filter specific cases while keeping NAs
  • Proper folder structure for lots of source files
  • log4net write single file for each call to log.info
  • Benchmarking RAM performance - UWP and C#
  • Acquiring multiple attributes from .xml file in c#
  • How to CLICK on IE download dialog box i.e.(Open, Save, Save As…)
  • Change div Background jquery
  • Qt: Run a script BEFORE make
  • How can I remove ASP.NET Designer.cs files?
  • reshape alternating columns in less time and using less memory
  • How to Embed XSL into XML
  • java string with new operator and a literal