Using compact() with the elements of an array?

Discussion in 'PHP' started by mcdeere02, Aug 7, 2009.

Thread Status:
Not open for further replies.
  1. #1
    Is it possible to apply compact() to the elements of an array? For instance, instead of
    Code:

    $name = 'John';
    $city = 'Denver';
    $state = 'Colorado';
    print_r(compact('name', 'city', 'state'));

    Something like
    Code:

    $arr['name'] = 'John';
    $arr['city'] = 'Denver';
    $arr['state'] = 'Colorado';
    print_r(compact(...on each key of $arr...));

    I was thinking of iterating over $arr with a foreach() loop and compact()ing each element individually, but I don't know how to pass the element keys as varnames that make sense to compact().
    Kadence is offline Reply With Quote
     
    mcdeere02, Aug 7, 2009 IP
  2. oop

    oop Peon

    Messages:
    35
    Likes Received:
    1
    Best Answers:
    0
    Trophy Points:
    0
    #2
    
    //This what you mean
    
    $element = array();
    
    $element['Name'] = 'OOP';
    $element['Age'] = 20;
    $element['colour'] = 'white';
    
    foreach($element as $key => $val){
        print_r( compact( $key , $val ) );
    }
    
    PHP:
    lol cant really userstand what you mean but thought id have a shot at it.
     
    oop, Aug 7, 2009 IP
Thread Status:
Not open for further replies.