+ Reply to Thread
Results 1 to 3 of 3

Thread: [PHP]: Handling Multidimensonal Array

  1. #1
    bhupendra2895's Avatar
    bhupendra2895 is offline x10 Elder bhupendra2895 is an unknown quantity at this point
    Join Date
    Jun 2010
    Location
    India
    Posts
    554

    [PHP]: Handling Multidimensonal Array

    I have a array that looks like
    Code:
    Array
    (
        [0] => Array
            (
                [link] => http://updated
                [type] => x10
                [vote] => 2
            )
    
        [1] => Array
            (
                [link] => http://latest
                [type] => hosting
                [vote] => 1
            )
    
        [2] => Array
            (
                [link] => http://paid
                [type] => x10
                [vote] => 3
            )
    
        [3] => Array
            (
                [link] => http://free
                [type] => hosting
                [vote] => 5
            )
    )
    I want the result like this

    Code:
    Array(
             [x10] => Array
                      (
                            [link1] => http://updated
                            [vote1] => 2
                            [link2] => http://paid
                            [vote2] => 3
                      )
             [hosting] => Array
                      (
                            [link1] => http://latest
                            [vote1] => 1
                            [link2] => http://free
                            [vote2] => 5
                      )
    )
    I am trying to find the solution of above problem since many hours.

    How this can be solved?
    Liked this? Click on the icon on the bottom of post to make me .

  2. #2
    misson is offline x10 Spammer misson is a jewel in the rough
    Join Date
    Mar 2008
    Location
    Libertatia
    Posts
    2,506

    Re: [PHP]: Handling Multidimensonal Array

    Iterate over the array, filling out another array.
    PHP Code:
    foreach ($source as $entry) {
        
    $byType[$entry['type']][] = $entry;
    }
    var_export($byType);
    /*Result: 
    array (
      'x10' => 
      array (
        0 => 
        array (
          'link' => 'http://updated',
          'type' => 'x10',
          'vote' => 2,
        ),
        1 => 
        array (
          'link' => 'http://paid',
          'type' => 'x10',
          'vote' => 3,
        ),
      ),
      'hosting' => 
      array (
        0 => 
        array (
          'link' => 'http://latest',
          'type' => 'hosting',
          'vote' => 1,
        ),
        1 => 
        array (
          'link' => 'http://free',
          'type' => 'hosting',
          'vote' => 5,
        ),
      ),
    )
    */ 
    Don't use 'link1', 'vote2' &c. as indices; use subarrays.
    Be sure to read all pages linked in this post; they have further information that should prove useful. When asking for help, make sure you follow Eric Raymond's and Jon Skeet's guidelines for prompt, accurate responses. Please answer any questions I ask; they're not rhetorical (probably). Any posted code is intended as illustrative example, rather than a solution to your problem to be copied without alteration. Study it to learn how to write your own solution.
    Misson, not Mission.

  3. #3
    bhupendra2895's Avatar
    bhupendra2895 is offline x10 Elder bhupendra2895 is an unknown quantity at this point
    Join Date
    Jun 2010
    Location
    India
    Posts
    554

    Re: [PHP]: Handling Multidimensonal Array

    @ Misson Thanks, What you described above is a better way to solve the problem.
    Liked this? Click on the icon on the bottom of post to make me .

+ Reply to Thread

Similar Threads

  1. Replies: 4
    Last Post: 04-23-2010, 02:38 AM
  2. Much 503 Errors with a 403 in error handling!
    By felabria in forum Free Hosting
    Replies: 1
    Last Post: 10-13-2009, 12:58 PM
  3. Password handling
    By espfutbol98 in forum Programming Help
    Replies: 5
    Last Post: 06-25-2009, 03:37 AM
  4. Recursive PHP array to Javascript array/object
    By Veridis in forum Programming Help
    Replies: 0
    Last Post: 04-16-2008, 02:55 AM

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
x10hosting free hosting for the masses
dedicated servers