1. Advertising
    y u no do it?

    Advertising (learn more)

    Advertise virtually anything here, with CPM banner ads, CPM email ads and CPC contextual links. You can target relevant areas of the site and show ads based on geographical location of the user if you wish.

    Starts at just $1 per CPM or $0.10 per CPC.

PHP Array help needed

Discussion in 'PHP' started by slimbachiya, Jul 21, 2008.

  1. #1
    I have an array like,

    Array
    (
    [0] => 1_8
    [26] => 1_9
    [56] => 2_9
    [65] => 3_7
    [68] => 1_6
    )

    I want to arrange this it as,

    [1] = 8,9,6
    [2] = 9
    [3] = 7

    Please suggest any idea.
    Thanks & regards,
     
    slimbachiya, Jul 21, 2008 IP
  2. cornetofreak

    cornetofreak Peon

    Messages:
    170
    Likes Received:
    6
    Best Answers:
    0
    Trophy Points:
    0
    #2
    show us your php array
     
    cornetofreak, Jul 21, 2008 IP
  3. ahowell

    ahowell Peon

    Messages:
    38
    Likes Received:
    2
    Best Answers:
    0
    Trophy Points:
    0
    #3
    $newArray  = array();                  
    $tempArray = array();
    $oldArray  = array(
        0  => '1_8',
        26 => '1_9',
        56 => '2_9',
        65 => '3_7',
        68 => '1_6'
    );
                      
    foreach ($oldArray as $value) {
        $valParts = explode('_', $value);
        $tempArray[$valParts[0]][] = $valParts[1];
    }
    
    foreach ($tempArray as $key => $value) {
        $newArray[$key] = implode(',', $tempArray[$key]);
    }
    PHP:
    this will give you the desired output.
     
    ahowell, Jul 21, 2008 IP
  4. cornetofreak

    cornetofreak Peon

    Messages:
    170
    Likes Received:
    6
    Best Answers:
    0
    Trophy Points:
    0
    #4
    sorry i aint got a clue bout this one :(
     
    cornetofreak, Jul 21, 2008 IP
  5. slimbachiya

    slimbachiya Peon

    Messages:
    36
    Likes Received:
    0
    Best Answers:
    0
    Trophy Points:
    0
    #5
    Thanks, Ahowell

    It works. Thank you very much for the code.
     
    slimbachiya, Jul 21, 2008 IP