SitePoint Sponsor

User Tag List

Results 1 to 6 of 6
  1. #1
    SitePoint Evangelist perpetual_dream's Avatar
    Join Date
    Nov 2005
    Location
    Jerusalem
    Posts
    542
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)

    How to fill this array on the fly

    Hello,

    I am trying to fill this multidimensional array programmatically.


    Code PHP:
    <?php
     
        $days = array(
     
            2=>array('/weblog/archive/2004/Jan/02','linked-day'),
     
            3=>array('/weblog/archive/2004/Jan/03','linked-day'),
     
            8=>array('/weblog/archive/2004/Jan/08','linked-day'),
     
            22=>array('/weblog/archive/2004/Jan/22','linked-day'),
     
            26=>array(NULL,'linked-day textual','twenty-six'),
     
        ); ?>
    I have tried to do the following to fill it on the fly from the database, but it didn't work.....

    Code PHP:
    $days=array();
     
    while (whatever) {
    $days[]=array($event_day=>array('/weblog/archive/2004/Jan/whatever','linked-day'));}

    Could you please point out what I am doing wrong ?

    Thanks
    Tanzeelnet - Best arabic downloadsite
    Linux Hosting |Free Downloads |

  2. #2
    SitePoint Wizard silver trophybronze trophy Cups's Avatar
    Join Date
    Oct 2006
    Location
    France, deep rural.
    Posts
    6,869
    Mentioned
    17 Post(s)
    Tagged
    1 Thread(s)
    Looks as though it should be;

    PHP Code:
    $days=array();
     
    while (
    whatever) {

    $days[$event_day]=array('/weblog/archive/2004/Jan/whatever','linked-day');

    }

    var_dump$days ) ; 
    where $event_day = the likes of (integer) 22

    Not sure how you are getting this one with 3 elements, or what you are going to do with it;
    PHP Code:
            26=>array(NULL,'linked-day textual','twenty-six'), 

  3. #3
    SitePoint Evangelist perpetual_dream's Avatar
    Join Date
    Nov 2005
    Location
    Jerusalem
    Posts
    542
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    Thanks ... Still I have another question

    if
    Code PHP:
    list($a,$b,$c)=$y[$x];

    and
    Code PHP:
    $x=array('a','b');
    and sometimes
    Code PHP:
    $x=array('a','b','c');

    How to assign Null to ($x[$number]) ex: $x[2] if the offset of ($number) [ex:2] is undefined in all the elements of $x. Otherwise, I am going to end up with an error.

    Thanks
    Tanzeelnet - Best arabic downloadsite
    Linux Hosting |Free Downloads |

  4. #4
    SitePoint Wizard silver trophybronze trophy Cups's Avatar
    Join Date
    Oct 2006
    Location
    France, deep rural.
    Posts
    6,869
    Mentioned
    17 Post(s)
    Tagged
    1 Thread(s)
    Your original sample had lines like this:
    PHP Code:
            22=>array('/weblog/archive/2004/Jan/22','linked-day'),
            
    26=>array(NULL,'linked-day textual','twenty-six'), 
    Hence my question, so now you will have to explain, or show a sample of, the original array, and be very explicit about where you want the NULL value to appear - the first, the last?

  5. #5
    SitePoint Evangelist perpetual_dream's Avatar
    Join Date
    Nov 2005
    Location
    Jerusalem
    Posts
    542
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    Hello,

    I am sending this array to another function. Some fields are optional.

    Therefore $x (sent value) sometimes equal to
    Code PHP:
    $x=array('a','b');

    and sometimes
    Code PHP:
    $x = array('a','b','c');

    in the function
    Code PHP:
    list($a,$b,$c)= $y[$x];
    would generate an error if all the $x values didn't contain the optional field $c........ How can I know if $x has $x[2] is set or not and if it is not set automatically fill it with Null instead.
    Tanzeelnet - Best arabic downloadsite
    Linux Hosting |Free Downloads |

  6. #6
    SitePoint Wizard bronze trophy
    Join Date
    Jul 2006
    Location
    Augusta, Georgia, United States
    Posts
    4,151
    Mentioned
    16 Post(s)
    Tagged
    3 Thread(s)
    PHP Code:
    if(count($x)<3$x[] = null
    ||

    PHP Code:
    if(!array_key_exists(2,$x)) $x[2] = null
    ||

    PHP Code:
    $x[2] = array_key_exists(2,$x)?$x[2]:null
    The only code I hate more than my own is everyone else's.


Tags for this Thread

Bookmarks

Posting Permissions

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