SitePoint Sponsor

User Tag List

Results 1 to 6 of 6
  1. #1
    SitePoint Member
    Join Date
    Apr 2011
    Posts
    11
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)

    Reorganizing an array: odd entries as KEY, even entries as VALUE

    Hey everyone,
    I'm trying to finish up a URL router that I created for my custom MVC framework. I have a list of parameters that I dissected from the URL, but the problem is that they only have numerical keys. What I want to do is set it up so the first value in the $params array will be the KEY and then the second value in the array is the VALUE of the first KEY. But I need to take it beyond that even further. Essentially, I need all odd number key's value in the array to be the new KEY and the even number key's value to be the value.

    Example:

    This is how it's CURRENTLY set up:
    Array
    (
    [0] => greeting
    [1] => hello
    [2] => question
    [3] => how-are-you
    [4] => response
    [5] => im-fine
    )

    This is how it NEEDS to be (after conversion):
    Array
    (
    [greeting] => hello
    [question] => how-are-you
    [response] => im-fine
    )

    It's probably a simple solution, because I'm sure this is a common issue, but any enlightenment?

  2. #2
    @php.net Salathe's Avatar
    Join Date
    Dec 2004
    Location
    Edinburgh
    Posts
    1,397
    Mentioned
    64 Post(s)
    Tagged
    0 Thread(s)
    A simple way would be to loop over the first array two at a time, building the associative structure as you go.

    PHP Code:
    $array = array("greeting""hello""question""how-are-you""response""im-fine");

    $assoc = array();
    foreach (
    array_chunk($array2) as $pair) {
        list(
    $key$value) = $pair;
        
    $assoc[$key] = $value;
    }

    var_export($assoc);

    /*
    array (
      'greeting' => 'hello',
      'question' => 'how-are-you',
      'response' => 'im-fine',
    )
    */ 
    There are likely ways of doing this in one line of code but the above should be nice and clear in what it is doing.
    Salathe
    Software Developer and PHP Manual Author.

  3. #3
    SitePoint Member
    Join Date
    Apr 2011
    Posts
    11
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    This worked very nicely! Thanks!

  4. #4
    I solve practical problems. bronze trophy
    Michael Morris's Avatar
    Join Date
    Jan 2008
    Location
    Knoxville TN
    Posts
    2,034
    Mentioned
    65 Post(s)
    Tagged
    0 Thread(s)
    Quote Originally Posted by CSHoffie View Post

    It's probably a simple solution, because I'm sure this is a common issue, but any enlightenment?
    A simpler solution would be to properly format your input control names in the HTML form.

    HTML Code:
    <input name="form[greeting]" value="hello">
    <input name="form[question]" value="how -are- you">
    <input name="form[response]" value="im-fine">
    Will be received by such that $_POST or $_GET (depending on the request method) will hold an array of "form" and that array will have your key value pairs.

    This cockamamie scheme you're thinking up right now with the odd / even pairing nonsense is only going to have the maintenance programmer who has to work on your code months from now want to punch you as hard as they can in the face.

  5. #5
    @php.net Salathe's Avatar
    Join Date
    Dec 2004
    Location
    Edinburgh
    Posts
    1,397
    Mentioned
    64 Post(s)
    Tagged
    0 Thread(s)
    Michael, these aren't values submitted from a form. The first post tells us where they're coming from.
    Salathe
    Software Developer and PHP Manual Author.

  6. #6
    I solve practical problems. bronze trophy
    Michael Morris's Avatar
    Join Date
    Jan 2008
    Location
    Knoxville TN
    Posts
    2,034
    Mentioned
    65 Post(s)
    Tagged
    0 Thread(s)
    Ah... oops.


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
  •