0

I have an array which consists of statistics pulled from my database. Each inner array has a key based on where it come from. I want to group all these together in a new array.

array (
  0 => 
  array (
    7 => 
    array (
      'y' => 83,
      'label' => '2019-01-04 00:00:00',
    ),
  ),
  1 => 
  array (
    7 => 
    array (
      'y' => 80,
      'label' => '2019-01-02 00:00:00',
    ),
  ),
  2 => 
  array (
    8 => 
    array (
      'y' => 50,
      'label' => '2019-01-03 00:00:00',
    ),
  ),
)

Would look like this:

array (
    0 =>
    array( 
        0 =>
        array(
            'y' => 83,
            'label' => '2019-01-04 00:00:00',
        ),
        1 =>
        array(
            'y' => 80,
            'label' => '2019-01-02 00:00:00',
        ),
    ),
    1 =>
    array(
        0 =>
        array(
            'y' => 50,
            'label' => '2019-01-03 00:00:00',
        ),
    ),
)

However, I cannot seem to use array_values() to achieve this. I want to group all of the 7's into an array and all of the 8's into an array however, I could have more numeric keys in the future.

Is there a function I can use to do this?

2
  • 1
    Why do you want the keys to become 0 for 7 and 1 for 8? Why not just keep 7 and 8? Commented Jan 4, 2019 at 21:24
  • Because I am json_encode ing the data and the graph API I am using expects no keys in the array @trincot Commented Jan 5, 2019 at 16:10

2 Answers 2

3

Assuming your input is in variable $data, you could do this:

$result = [];
foreach($data as $arr) {
    foreach($arr as $k => $v) {
        $result[$k][] = $v;
    }
}

This will maintain the original inner-key value, so for your example input the result will have keys 7 and 8, not 0 and 1.

If you want that, then do one thing extra:

$result = array_values($result);
Sign up to request clarification or add additional context in comments.

2 Comments

You, my friend, are my hero! Thank you so much! I'll accept when I can - this worked so perfectly and has saved me another n hours of frustration!
@rev, indeed, but I found the OP's "...I could have more numeric keys..." open for interpretation so I thought "better be safe than sorry".
0

Give this a try!

<?php
$array = 
array(
    0 => array (
        7 => array (
            'y' => 83,
            'label' => '2019-01-04 00:00:00'
        )
    ),
    1 => array (
        7 => array (
            'y' => 80,
            'label' => '2019-01-02 00:00:00'
        )
    ),
    2 => array (
        8 => array (
            'y' => 50,
            'label' => '2019-01-03 00:00:00'
        )
    )
);

$sorted = array();
foreach($array as $key => $value)
    $sorted[key($value)][] = $array[$key][key($value)];

print_r($sorted);

Comments

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.