-3

Here is the example array :

Array
(
    [0] => Array
        (
            [action] => move
            [para1] => h_1
            [para2] => loc_231
            [day] => day_1
        )

    [1] => Array
        (
            [action] => visit
            [para1] => poi_231
            [para2] => loc_231
            [day] => day_1

        )
     [2] => Array
        (
            [action] => visit
            [para1] => poi_231
            [para2] => loc_231
            [day] => day_2

        )
   )

I would like to split the array based on the value of key day and then combine them together.

The final array should look like:

    Array
    (
        Array(
         [0] => Array
            (
                [action] => move
                [para1] => h_1
                [para2] => loc_231
                [day] => day_1
            )

        [1] => Array
            (
                [action] => visit
                [para1] => poi_231
                [para2] => loc_231
                [day] => day_1

            )
       )
      Array(
        [0] => Array
            (
                [action] => visit
                [para1] => poi_231
                [para2] => loc_231
                [day] => day_2

            )
      )

 )

What is the better way other than iterating through the array? Is there any php function for that ?

4
  • 2
    is this related? stackoverflow.com/questions/18224502/… Commented Oct 11, 2015 at 12:47
  • it seems to be related ? let me try first. Commented Oct 11, 2015 at 12:49
  • Have you tried anything yet?.... Commented Oct 11, 2015 at 12:50
  • I tried to loop the array but keeps getting array_push() expects parameter 1 to be array, null given Commented Oct 11, 2015 at 12:58

1 Answer 1

0

You can do your own function

$data = array(array("action"=>"1","day"=>"day_2"),array("action"=>"2","day"=>"day_1"),array("action"=>"3","day"=>"day_1"),array("action"=>"4","day"=>"day_2"),array("action"=>"5","day"=>"day_1"));
print_r($data);
function split_on($array,$field)
{
    $ret = array();
    foreach($array as $arr)
    {
        $value_field = $arr[$field];
        $ret[$value_field][] = $arr;
    }
    return array_values($ret);
}
print_r(split_on($data,"day"));

Result:

Array
(
    [0] => Array
        (
            [action] => 1
            [day] => day_2
        )

    [1] => Array
        (
            [action] => 2
            [day] => day_1
        )

    [2] => Array
        (
            [action] => 3
            [day] => day_1
        )

    [3] => Array
        (
            [action] => 4
            [day] => day_2
        )

    [4] => Array
        (
            [action] => 5
            [day] => day_1
        )

)
Array
(
    [0] => Array
        (
            [0] => Array
                (
                    [action] => 1
                    [day] => day_2
                )

            [1] => Array
                (
                    [action] => 4
                    [day] => day_2
                )

        )

    [1] => Array
        (
            [0] => Array
                (
                    [action] => 2
                    [day] => day_1
                )

            [1] => Array
                (
                    [action] => 3
                    [day] => day_1
                )

            [2] => Array
                (
                    [action] => 5
                    [day] => day_1
                )

        )

)
Sign up to request clarification or add additional context in comments.

1 Comment

Its not a problem. The function is generic

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.