0

I have an array with float indexes that I'm getting from a plugin. I want to parse this array and take the ones that are float and put them in as a new array. Meaning what I have is

[1] => gsurvey128f54af2
[2] => gsurvey282bd4253
[5.1] => gsurvey5649a964f
[5.2] => gsurvey5fddb5e9f
[5.3] => gsurvey533c5b311
[5.4] => gsurvey5c8933efb
[5.5] => gsurvey5da48f59b

What I want is:

    [1] => gsurvey128f54af2
    [2] => gsurvey282bd4253
    [5] => array ( 
         [0] => gsurvey5649a964f
         [1] => gsurvey5fddb5e9f
         [2] => gsurvey533c5b311
         [3] => gsurvey5c8933efb
         [4] => gsurvey5da48f59b
)

I'm not clear on what's the best way to approach this.

6
  • 1
    Are you sure you have those keys for your array, because a float is an invalid key value Commented Oct 22, 2014 at 16:55
  • You may want to modify the code generating this interesting array to exactly what you have in your result. Floats are not valid. Commented Oct 22, 2014 at 16:56
  • They are actually strings. Try a var_dump. Commented Oct 22, 2014 at 16:58
  • Floats as string will work. Commented Oct 22, 2014 at 16:59
  • This is coming from a wordpress plugin. That's the raw data I get. Even if I get it from database the arrays still contain floats. Somehow I want to combine floats for each into a new array. Commented Oct 22, 2014 at 16:59

3 Answers 3

1

I made the comment above that this problem is very prone to error because it is possible to have input that would set a single value to be both a string and an array at the same time. That will obviously fail. Assuming that the input is clean...

$new_array = array();
foreach($old_array as $key=>$val)
{
  $a = explode('.', $key);
  if(sizeof($a)==2)
  {
    if(!isset($new_array[$a[0]])) $new_array[$a[0]] = array();
    $new_array[$a[0]][] = $val;
  }
  else $new_array[$key]=$val;
}
Sign up to request clarification or add additional context in comments.

3 Comments

That worked. It does leave an extra blank index at end of each one but I can debug it at this point. Thank you.
I tested it using the example array from your question and I do not see a blank index. I am not sure where it may come from. Perhaps you could drop the "isset" check to ensure that $a[0] key is set. That is there just to help newer programmers read the code. PHP creates the key in the next line if it doesn't exist. So, the code works fine without it.
That was due to some blank in my form so the code you put works great.
0

Example with array_walk:

$array = array (
    '1' => 'gsurvey128f54af2',
    '2' => 'gsurvey282bd4253',
    '5.1' => 'gsurvey5649a964f',
    '5.2' => 'gsurvey5fddb5e9f',
    '5.3' => 'gsurvey533c5b311',
    '5.4' => 'gsurvey5c8933efb',
    '5.5' => 'gsurvey5da48f59b'
);

$output = array();
array_walk( $array, function( $item, $key ) use ( &$output ) {
    $keys = explode( '.', $key );
    isset( $keys[1] ) ? $output[ $keys[0] ][ $keys[1] ] = $item : $output[ $keys[0] ] = $item;
});

print_r( $output );
/*
Array
(
    [1] => gsurvey128f54af2
    [2] => gsurvey282bd4253
    [5] => Array
        (
            [1] => gsurvey5649a964f
            [2] => gsurvey5fddb5e9f
            [3] => gsurvey533c5b311
            [4] => gsurvey5c8933efb
            [5] => gsurvey5da48f59b
        )

)
*/

Comments

0

For this specific case, this should work:

foreach($array as $k => $v) {
    if(count($i = explode('.', $k)) > 1) {
        $result[$i[0]][] = $v;
    } else {
        $result[$k] = $v;
    }
}

3 Comments

Care to leave constructive criticism?
Your code does not handle the cases where there is no '.' in the key. The request is that those are mapped to the new array as is.
Damn, I tested in my IDE and never edited the answer. Edited, thanks for explanation.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.