gladideg Posted May 31, 2009 Share Posted May 31, 2009 I have an array with 4 arrays inside, which again has 10 tab-seperated fields... I'm trying to match a value inside one of the 4 arrays, and if there was a match, I want to output or save that complete array as an new array I can reuse... I tried using preg_match, but I'm lost. $theinstance = "i-dbc1b3"; $output = thearray.... echo "<pre>"; foreach($output as $row) { echo "<pre>"; preg_match("(^$theinstance^)", $row, $instance); print_r($instance); echo "</pre>"; } echo "</pre>"; Link to comment https://forums.phpfreaks.com/topic/160401-solved-preg_match-on-a-field-within-an-array-and-output-that-complete-array/ Share on other sites More sharing options...
Ken2k7 Posted May 31, 2009 Share Posted May 31, 2009 What do you mean by 10 tab-separated fields? Can you post an example of the array you're using? I'm having trouble picturing what your array looks like. Link to comment https://forums.phpfreaks.com/topic/160401-solved-preg_match-on-a-field-within-an-array-and-output-that-complete-array/#findComment-846436 Share on other sites More sharing options...
gladideg Posted May 31, 2009 Author Share Posted May 31, 2009 Sorry for being unclear, my question was also wrong. It's actually one array, with 4 key's with 14 fields, tab-separated. I need to search this array for a match on "banana" Then I need that complete row as an $array, where all fields can be extracted like : echo $array[5] . Array ( [0] => 0 1 2 3 4 orange 6 7 8 9 10 11 12 13 [1] => 0 1 2 3 4 apple 6 7 8 9 10 11 12 13 [2] => 0 1 2 3 4 banana 6 7 8 9 10 11 12 13 [3] => 0 1 2 3 4 foobar 6 7 8 9 10 11 12 13 ) Link to comment https://forums.phpfreaks.com/topic/160401-solved-preg_match-on-a-field-within-an-array-and-output-that-complete-array/#findComment-846439 Share on other sites More sharing options...
Ken2k7 Posted May 31, 2009 Share Posted May 31, 2009 Like this - <?php $theinstance = "i-dbc1b3"; $new_array = array(); foreach ($output as $key => $value) { if (strpos($value, $theinstance) !== false) $new_array[] = $value; } var_dump($new_array); Link to comment https://forums.phpfreaks.com/topic/160401-solved-preg_match-on-a-field-within-an-array-and-output-that-complete-array/#findComment-846440 Share on other sites More sharing options...
gladideg Posted June 1, 2009 Author Share Posted June 1, 2009 This was exactly what I needed. I did some customizing and now it works just as I need it to. Thank you very much for the help on this one. Link to comment https://forums.phpfreaks.com/topic/160401-solved-preg_match-on-a-field-within-an-array-and-output-that-complete-array/#findComment-846568 Share on other sites More sharing options...
Recommended Posts
Archived
This topic is now archived and is closed to further replies.