eludlow Posted December 15, 2008 Share Posted December 15, 2008 I have an array in which each element is two numbers separated by a comma, eg 50,3 How would I go about checking whether a certain number matches the first half of the element? Ie, I provide a variable $x and want to output each element that starts $x,5 or $x,49 etc etc I tried using in_array but then realised that this only matched the element in its entirety. I've a feeling it may need regular expressions? Many thanks, Ed Ludlow Link to comment https://forums.phpfreaks.com/topic/137040-solved-searching-within-an-array-element/ Share on other sites More sharing options...
JonnoTheDev Posted December 15, 2008 Share Posted December 15, 2008 explode the array value into separate elements i.e $x = array('5,10'); $parts = explode(",",$x[0]); if($parts[0] == 5) { // the first number is 5 } Link to comment https://forums.phpfreaks.com/topic/137040-solved-searching-within-an-array-element/#findComment-715748 Share on other sites More sharing options...
eludlow Posted December 15, 2008 Author Share Posted December 15, 2008 Perfect, thanks. Solved a very similar problem I had earlier using explode() (after posting on the forum - see reg ex forum!) - should have thought of it myself Many thanks. Link to comment https://forums.phpfreaks.com/topic/137040-solved-searching-within-an-array-element/#findComment-715751 Share on other sites More sharing options...
Recommended Posts
Archived
This topic is now archived and is closed to further replies.