Zugzwangle Posted June 18, 2010 Share Posted June 18, 2010 I have the below array, $withoutBreaks: Array ( [0] => [Event 90m + 5s, rated [1] => [site Room 1 [2] => [Date 2010.06.18 [3] => [Round ? [4] => [White Zugzwang1 [5] => [black Jeff Morris [6] => [Result 1-0 [7] => [WhiteElo 1543 [8] => [blackElo 1537 [9] => [PlyCount 5 [10] => [EventDate 2010.06.18 [11] => 1. d4 {3} c6 {2} 2. Nc3 {2} d5 {1} 3. Nf3 {4 Jeff Morris resigns (Lag: Av=0. 22s, max=0.3s)} 1-0 ) This array is made by a form. The elements may differ in both number and in order, for example [blackElo may be element [3] rather than [6]. One constant, is the '[' followed by the immediate word i.e. [Event. What I would like to happen, is to use array_search() to find which element the data is held in. My code: $datePart = array_search("[Date ", $withoutBreaks); echo ($datePart); Output: None!!! What am I doing wrong? Quote Link to comment https://forums.phpfreaks.com/topic/205199-rather-simple-question-array_search-or-other/ Share on other sites More sharing options...
Alex Posted June 18, 2010 Share Posted June 18, 2010 In your array it's [EventDate 2010.06.18 not [Date 2010.06.18 Edit: Since you modified your post it leads me to believe you want something more like this: foreach($withoutBreaks as $key => $val) { if(substr($val, 0, 11) == '[EventDate ') { echo "Key: $key<br />\n"; break; } } Quote Link to comment https://forums.phpfreaks.com/topic/205199-rather-simple-question-array_search-or-other/#findComment-1074096 Share on other sites More sharing options...
Psycho Posted June 18, 2010 Share Posted June 18, 2010 It looks like the values in the array actually contain a name and the actual valule. So, Building upon AlexWD's post, here is a function you could use. Just pass the function the "name" you are looking for and it will return the value: function getValue($array, $name) { foreach($array as $key => $value) { if(substr($value, 1, strlen($name))==$name) { return substr($value, strlen($name)+2); } } //No match found return false; } //Usage echo getValue($withoutBreaks, 'EventDate'); //Ouput: 2010.06.18 Edit: Now that I think of it, it might be better to just create a function to conver the array to where the names are the keys and the values are the actual values: Array ( ['Event'] => 90m + 5s, rated ['Site Room'] => 1 ['Date'] => 2010.06.18 ['Round'] => ? ... Quote Link to comment https://forums.phpfreaks.com/topic/205199-rather-simple-question-array_search-or-other/#findComment-1074106 Share on other sites More sharing options...
Alex Posted June 18, 2010 Share Posted June 18, 2010 Edit: Now that I think of it, it might be better to just create a function to conver the array to where the names are the keys and the values are the actual values: Array ( ['Event'] => 90m + 5s, rated ['Site Room'] => 1 ['Date'] => 2010.06.18 ['Round'] => ? ... And to do something like that this should work: $new = array(); foreach($withoutBreaks as $val) { $parts = explode(' ', $val); $new[substr(array_shift($parts), 1)] = implode(' ', $parts); } I made the assumption that the first word is the name and the rest is the value. Wasn't completely sure if it should be like 'Site Room' => 1 or 'Site' => 'Room 1', this code should do the latter. Quote Link to comment https://forums.phpfreaks.com/topic/205199-rather-simple-question-array_search-or-other/#findComment-1074116 Share on other sites More sharing options...
Recommended Posts
Join the conversation
You can post now and register later. If you have an account, sign in now to post with your account.