SitePoint Sponsor

User Tag List

Results 1 to 6 of 6
  1. #1
    SitePoint Wizard PHPycho's Avatar
    Join Date
    Dec 2005
    Posts
    1,201
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)

    Reformulate new array

    I had the following array:
    PHP Code:
    array(
    'title_1' => 'some value',
    'link_1' => 'some value',
    'title_2' => 'some value',
    'link_2' => 'some value',
    'title_3' => 'some value',
    'link_3' => 'some value',
    ...

    I want to convert this array as follow:
    PHP Code:
    array(
        array(
    'title_1' => 'some value',
              
    'link_1' => 'some value'),
        array(
    'title_2' => 'some value',
              
    'link_2' => 'some value'),
        array(
    'title_3' => 'some value',
              
    'link_3' => 'some value'),
    ...

    So that i can group items and loop easily.

    Thanks

  2. #2
    Non-Member
    Join Date
    Oct 2009
    Posts
    1,852
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    well just loop it once and group whatever you want

    But why don't you make it simple
    array(link = > value, link => value)?

  3. #3
    SitePoint Wizard PHPycho's Avatar
    Join Date
    Dec 2005
    Posts
    1,201
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    One quick dirty solution i have found is:
    PHP Code:
    $new_array array_chunk($old_array2true); 
    This only works correct when we kept the elements in the order else it won't work.
    Any good approaches ?

  4. #4
    Twitter: @AnthonySterling silver trophy AnthonySterling's Avatar
    Join Date
    Apr 2008
    Location
    North-East, UK.
    Posts
    6,111
    Mentioned
    3 Post(s)
    Tagged
    0 Thread(s)
    A proper sorting callback would be better, although, here's a pre-coffee offering.
    PHP Code:
    <?php
    $array 
    = array(
        
    'link_2'    =>    '2',
        
    'title_4'    => '4',
        
    'link_6'    =>    '6',
        
    'title_2'    => '2',
        
    'link_3'    =>    '3',
        
    'title_1'    => '1',
        
    'link_1'    =>    '1',
        
    'title_5'    => '5',
        
    'link_4'    =>    '4',
        
    'title_6'    => '6',
        
    'link_5'    =>    '5',
        
    'title_3'    => '3'
    );

    $final = array();
    foreach(
    $array as $key => $value)
    {
        
    $int preg_replace('~[^0-9]~'null$key);
        
    $final[$int][$key] = $value;
    }
    ksort($final);
    print_r($final);
    /*
        Array
        (
            [1] => Array
                (
                    [title_1] => 1
                    [link_1] => 1
                )
        
            [2] => Array
                (
                    [link_2] => 2
                    [title_2] => 2
                )
        
            [3] => Array
                (
                    [link_3] => 3
                    [title_3] => 3
                )
        
            [4] => Array
                (
                    [title_4] => 4
                    [link_4] => 4
                )
        
            [5] => Array
                (
                    [title_5] => 5
                    [link_5] => 5
                )
        
            [6] => Array
                (
                    [link_6] => 6
                    [title_6] => 6
                )
        )
    */
    Edit: 2nd attempt.

    PHP Code:
    <?php
    $array 
    = array(
        
    'link_2'    =>    '2',
        
    'title_4'    => '4',
        
    'link_6'    =>    '6',
        
    'title_2'    => '2',
        
    'link_3'    =>    '3',
        
    'title_1'    => '1',
        
    'link_1'    =>    '1',
        
    'title_5'    => '5',
        
    'link_4'    =>    '4',
        
    'title_6'    => '6',
        
    'link_5'    =>    '5',
        
    'title_3'    => '3'
    );

    asort($array);
    print_r(
        
    array_chunk(
            
    $array,
            
    2,
            
    true
        
    ),
        
    true
    );
    /*
        Array
        (
            [1] => Array
                (
                    [title_1] => 1
                    [link_1] => 1
                )
        
            [2] => Array
                (
                    [link_2] => 2
                    [title_2] => 2
                )
        
            [3] => Array
                (
                    [link_3] => 3
                    [title_3] => 3
                )
        
            [4] => Array
                (
                    [title_4] => 4
                    [link_4] => 4
                )
        
            [5] => Array
                (
                    [title_5] => 5
                    [link_5] => 5
                )
        
            [6] => Array
                (
                    [link_6] => 6
                    [title_6] => 6
                )
        )
    */
    @AnthonySterling: I'm a PHP developer, a consultant for oopnorth.com and the organiser of @phpne, a PHP User Group covering the North-East of England.

  5. #5
    SitePoint Wizard PHPycho's Avatar
    Join Date
    Dec 2005
    Posts
    1,201
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    Thanks AnthonySterling.
    That was trickysome.

  6. #6
    SitePoint Wizard PHPycho's Avatar
    Join Date
    Dec 2005
    Posts
    1,201
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    One another solution:
    PHP Code:
    <?php

    $your_array 
    = array(
    'title_1' => 'some value',
    'link_1' => 'some value',
    'title_2' => 'some value',
    'link_2' => 'some value',
    'title_3' => 'some value',
    'link_3' => 'some value');

    //This shall be our result_array
    $result_array = array();

    for(
    $i 0$i<sizeof($your_array); $i++){    
        
        
    //Slice first two elements of array and add it to result_array
        
    array_push($result_array,array_slice($your_array,0,2));
        
        
    //Slice your_array to remove first two elements
        
    $your_array array_slice($your_array,2);
        
        
    //if only last 2 elements are left with your_array then add them to result_array
        
    if(sizeof($your_array) == 2){
            
    array_push($result_array,$your_array);
            break;
        }
    }

    //Print the output
    echo "<pre>";
    print_r($result_array);
    echo 
    "</pre>";
    ?>
    Credit for this solution goes to tbankar.


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
  •