kproctor Posted March 14, 2017 Share Posted March 14, 2017 Hi I I have the following the following results that are returned from an array. I want to be able to just call one part of the array. Example: $array[7][2]. I have tried this but no result is returned. [7] => Array([name] => Convertible[types] => Array([0] => Term Posted Rates APR(2) [1] => Convertible[2] => 6 Month3.140%3.350%[3] => Closed[4] => 1 Year3.140%3.250% Quote Link to comment Share on other sites More sharing options...
kproctor Posted March 14, 2017 Author Share Posted March 14, 2017 and I only want the first number for the second part of the array Quote Link to comment Share on other sites More sharing options...
kproctor Posted March 14, 2017 Author Share Posted March 14, 2017 okay.. this is getting me closer. echo $royal_list[5] ['types'][8]; It returns 5 Year 4.640% 4.660% Now I need to extract "5 Years" and 4.640% for the results and assign them to a variable for use. thinking... Quote Link to comment Share on other sites More sharing options...
kproctor Posted March 14, 2017 Author Share Posted March 14, 2017 And I get closer maybe.... This: echo $rate; //echo substr_count($rate, ' '); $rate_details = explode(" ",$rate); echo print_r($rate_details); returns this: 4 Year 4.390% 4.420% Array ( [0] => 4 [1] => Year [2] => [3] => [4] => [5] => [6] => [7] => [8] => [9] => [10] => [11] => [12] => [13] => [14] => [15] => [16] => [17] => [18] => [19] => [20] => [21] => [22] => [23] => [24] => [25] => [26] => [27] => 4.390% [28] => [29] => [30] => [31] => [32] => [33] => [34] => [35] => [36] => [37] => [38] => [39] => [40] => [41] => [42] => [43] => [44] => [45] => [46] => [47] => [48] => [49] => [50] => [51] => [52] => [53] => 4.420% [54] => [55] => [56] => [57] => [58] => [59] => [60] => [61] => [62] => [63] => [64] => [65] => [66] => [67] => [68] => [69] => [70] => [71] => [72] => [73] => [74] => [75] => [76] => [77] => ) 1 Quote Link to comment Share on other sites More sharing options...
Psycho Posted March 14, 2017 Share Posted March 14, 2017 Wow, you are all over the place. You've completely deviated from your original example of $array[7][2] (which should have been $array[7]['types'][2]) to $royal_list[5] ['types'][8] So, I'm not following why the indexes are changing or how you determine which ones to use. But, assuming you know the correct indexes to use to reference the correct field, you need to know the exact types of values and the formats that will exist in that field. Your examples show a single numeric digit, followed by a space, followed by an alpha string, followed by a space, followed by a "percentage", followed by a space and finally another percentage. Are those percentages always a digit[period]three_digits? Although, I don't know if those are always spaces or if they are line breaks based on your first post. I can make some guesses on how to parse the value, but it really helps when I know all the possible values that could exist. Here is my best guess based on the limited information. function parseRateInfo($value) { if(!preg_match("#(\d*\s*\w*)\s*([^\s]*)\s*([^\s]*)#i", $value, $matches)) { //No match found return false; } //Parse the matches and return results $return['description'] = $matches[1]; $return['perc_low'] = $matches[2]; $return['perc_high'] = $matches[3]; return $return; } //Usage $value = "6 Month 3.140% 3.350%"; $rateInfo = parseRateInfo($value); print_r($rateInfo); Output Array ( [description] => 6 Month [perc_low] => 3.140% [perc_high] => 3.350% ) Quote Link to comment Share on other sites More sharing options...
benanamen Posted March 14, 2017 Share Posted March 14, 2017 I smell an XY problem. OP, where does this array data come from? A database? How about showing the code how you come up with this array. Quote Link to comment 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.