0

I asked a question several days ago that was answered well, but now I've implemented the solution and facing a few issues. (Thanks to Oleg for getting this far)

I'm trying to merge and sum the following arrays:

Array
(
    [Thursday] => Array
        (
            [Lunch] => Array
                (
                    [Total] => 10
                )

            [Date] => 2015-12-31
        )

)


Array
(
    [Thursday] => Array
        (
            [Lunch] => Array
                (
                    [Guarantees] => 231
                )

            [Date] => 2015-12-31
        )

)


Array
(
    [Friday] => Array
        (
            [Breakfast] => Array
                (
                    [Total] => 1
                )

            [Date] => 2016-01-01
            [Lunch] => Array
                (
                    [Total] => 1
                )

        )

    [Thursday] => Array
        (
            [Lunch] => Array
                (
                    [Total] => 1
                )

            [Date] => 2015-12-31
        )

)

And I'm using the following code:

private function _merge( $arr1, $arr2, $arr3 )
    {

        $maxArraysCount = func_num_args();
        $return = array();
        for( $i = 1; $i < $maxArraysCount; $i++ ){
            $arr = 'arr' . $i;
            if( isset( $$arr ) && is_array( $$arr )){
                foreach ( $$arr as $day => $value ) {
                    foreach ( $value as $meal => $time ) {
                        if( $meal == "Date"){
                            $return[$day][$meal] = $time;
                        } else {
                            foreach ( $time as $type => $count ) {
                                if( !isset( $return[$day][$meal][$type] ) )
                                    $return[$day][$meal][$type] = 0;
                                $return[$day][$meal][$type] = $count + $return[$day][$meal][$type];
                            }
                        }
                    }
                }
            }
        }
        return $return;
    }

And it's outputting this:

Array
(
    [Thursday] => Array
        (
            [Lunch] => Array
                (
                    [Total] => 11
                )

            [Date] => 2015-12-31
        )

    [Friday] => Array
        (
            [Breakfast] => Array
                (
                    [Total] => 1
                )

            [Date] => 2016-01-01
            [Lunch] => Array
                (
                    [Total] => 1
                )

        )

)

There is missing information in this merged/summed array. Notice there are no "Guarantees" at all. It appears that arr1 has a higher priority than arr3?

Community
  • 1
  • 1
Adam
  • 9,189
  • 15
  • 46
  • 62
  • why don't use array of objects instead? I mean define an object for each day . I think it's not a very good idea to have array elements with different structure.what is the point in merging these arrays? – Majid Abdolhosseini Dec 25 '14 at 20:29
  • Maybe, I'm not too familiar with the benefits of doing so. These are two separate queries that have been restructured to use in a TWIG template. It is not practical to merge the two queries with MySQL, so I'm doing it with PHP. – Adam Dec 25 '14 at 20:31
  • maybe its practical :) .can you give tables which you are making queries from them? – Majid Abdolhosseini Dec 25 '14 at 20:34
  • The query is a highly advanced Propel query, quite beyond the scope of this question I think! If there's no practical solution for this issue, I'll tackle it from that direction... – Adam Dec 25 '14 at 20:35
  • I think one way is to define a format for your arrays and then merge them easier . I mean like this : array('day_of_week'=>"" , 'date'=> "" , 'breakfast'=> "" , 'lunch' =>"" , 'dinner' => "") is this possible in your software logic ?? – Majid Abdolhosseini Dec 25 '14 at 20:44
  • @majid8911 that is essentially what I am doing. Am I missing something? – Adam Dec 25 '14 at 20:46
  • if the array which you post is output of propel I don't know how to change it but if you yourself have created arrays and then want to merge them I mean just don't make the arrays too deep.just make structure of arrays simpler and don't make nested arrays If it's not possible please edit your arrays and put "," between elements i'm wrting a script to merge the arrays. – Majid Abdolhosseini Dec 25 '14 at 20:52

1 Answers1

0

The problem is very simple actually, you need to change the for to:

for( $i = 1; $i <= $maxArraysCount; $i++ ){

Just add <= instead of <

This will make the for loop through 1 to 3 arrays passed in as parameters, if you just do < it will run until the $i is less than 3, not equal to 3.

Thanasis Pap
  • 2,031
  • 2
  • 17
  • 19