Jump to content

Recommended Posts

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 Month
3.140%
3.350%

[3] => Closed

[4] => 1 Year
3.140%
3.250%

Link to comment
https://forums.phpfreaks.com/topic/303452-array-help/
Share on other sites

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
Link to comment
https://forums.phpfreaks.com/topic/303452-array-help/#findComment-1544211
Share on other sites

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%
)
Link to comment
https://forums.phpfreaks.com/topic/303452-array-help/#findComment-1544212
Share on other sites

This thread is more than a year old. Please don't revive it unless you have something important to add.

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.

Guest
Reply to this topic...

×   Pasted as rich text.   Restore formatting

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

×
×
  • Create New...

Important Information

We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue.