Hello and welcome to our community! Is this your first visit?
Register
Enjoy an ad free experience by logging in. Not a member yet? Register.
Results 1 to 6 of 6
  1. #1
    Regular Coder MrBiggZ's Avatar
    Join Date
    Apr 2005
    Location
    Indianapolis IN
    Posts
    275
    Thanks
    39
    Thanked 0 Times in 0 Posts

    How can I do a foreach loop on a specific key in a multidimensional array?

    Hi!

    I have this two dimension array and I need to read through one of the primary keys in it to reformat the date and just month and day.

    Here's the preg_match I'm using:
    PHP Code:
    preg_match_all('/yweather:forecast.*day="(.*)".date="(.*).low="(.*)".high="(.*)".text="(.*)".code="(.*)"/isU ',$data,$fcast_class); 
    This is URL that I have test output at http://ftscv3.handlersspot.net/ The third key in the array ( [2] => Array ) is what I'm targeting. I want to get that date to output as month day, example Nov 30.

    I just don't know how to do a foreach loop on a single key of a multidimensional array.

    Thanks for your help!

    Dave
    Last edited by MrBiggZ; 11-27-2013 at 02:47 PM. Reason: edit
    “No matter how slick the demo is in rehearsal, when you do it in front of a live audience, the probability of a flawless presentation is inversely proportional to the number of people watching, raised to the power of the amount of money involved.” ~ Mark Gibbs

  • #2
    New Coder
    Join Date
    Nov 2011
    Location
    Ratio, Logic
    Posts
    60
    Thanks
    3
    Thanked 6 Times in 6 Posts
    PHP Code:
    echo $name_of_array[2][3]; //could produce something like: 30 Nov 2013 

  • #3
    Regular Coder MrBiggZ's Avatar
    Join Date
    Apr 2005
    Location
    Indianapolis IN
    Posts
    275
    Thanks
    39
    Thanked 0 Times in 0 Posts
    That is true, but I want just the month and day. (i.e. Nov 30)
    “No matter how slick the demo is in rehearsal, when you do it in front of a live audience, the probability of a flawless presentation is inversely proportional to the number of people watching, raised to the power of the amount of money involved.” ~ Mark Gibbs

  • #4
    New Coder
    Join Date
    Nov 2011
    Location
    Ratio, Logic
    Posts
    60
    Thanks
    3
    Thanked 6 Times in 6 Posts
    PHP Code:
    echo substr($name_of_array[2][3], 0, -5); 

  • #5
    God Emperor Fou-Lu's Avatar
    Join Date
    Sep 2002
    Location
    Saskatoon, Saskatchewan
    Posts
    16,987
    Thanks
    4
    Thanked 2,660 Times in 2,629 Posts
    You simply select the appropriate key:
    PHP Code:
    foreach ($fcast_class[2] AS &$itm)
    {
       
    $dt = new DateTime($itm);
       
    $itm $dt->format('M d');

    You'll want to verify your data there as well; what you have in this link has trailing double quotations (ie: your pattern is missing them so the greed takes them).
    PHP Code:
    header('HTTP/1.1 420 Enhance Your Calm'); 

  • Users who have thanked Fou-Lu for this post:

    MrBiggZ (11-27-2013)

  • #6
    Regular Coder MrBiggZ's Avatar
    Join Date
    Apr 2005
    Location
    Indianapolis IN
    Posts
    275
    Thanks
    39
    Thanked 0 Times in 0 Posts
    Thanks!

    Ok! So I was on the right track initially! I didn't know if using that array variable and the index number was legal or not. Guess it is. Next time I go with the gut feeling!

    Again thanks Fou
    “No matter how slick the demo is in rehearsal, when you do it in front of a live audience, the probability of a flawless presentation is inversely proportional to the number of people watching, raised to the power of the amount of money involved.” ~ Mark Gibbs


  •  

    Tags for this Thread

    Posting Permissions

    • You may not post new threads
    • You may not post replies
    • You may not post attachments
    • You may not edit your posts
    •