Jump to content

Recommended Posts

Hello

I have an array where i want the join values (not sum) where the date are the same

This my array(var_export)

array (
  0 => 
  array (
    0 => 
    array (
      0 => 
      array (
        'dataAula' => '2020-09-21',
        'tempos' => '4',
        'tempos2' => 'Segunda',
        'total' => 4,
      ),
    ),
    1 => 
    array (
      1 => 
      array (
        'dataAula' => '2020-09-22',
        'tempos' => '4',
        'tempos2' => 'Terça',
        'total' => 8,
      ),
    ),
    2 => 
    array (
      2 => 
      array (
        'dataAula' => '2020-09-28',
        'tempos' => '4',
        'tempos2' => 'Segunda',
        'total' => 12,
      ),
    ),
    3 => 
    array (
      3 => 
      array (
        'dataAula' => '2020-09-29',
        'tempos' => '4',
        'tempos2' => 'Terça',
        'total' => 16,
      ),
    ),
    4 => 
    array (
      4 => 
      array (
        'dataAula' => '2020-10-06',
        'tempos' => '4',
        'tempos2' => 'Terça',
        'total' => 20,
      ),
    ),
    5 => 
    array (
      5 => 
      array (
        'dataAula' => '2020-10-12',
        'tempos' => '4',
        'tempos2' => 'Segunda',
        'total' => 24,
      ),
    ),
    6 => 
    array (
      6 => 
      array (
        'dataAula' => '2020-10-13',
        'tempos' => '4',
        'tempos2' => 'Terça',
        'total' => 28,
      ),
    ),
    7 => 
    array (
      7 => 
      array (
        'dataAula' => '2020-10-19',
        'tempos' => '4',
        'tempos2' => 'Segunda',
        'total' => 32,
      ),
    ),
    8 => 
    array (
      8 => 
      array (
        'dataAula' => '2020-10-20',
        'tempos' => 1,
        'tempos2' => 'Terça',
        'total' => 33,
      ),
    ),
  ),
  1 => 
  array (
    0 => 
    array (
      0 => 
      array (
        'dataAula' => '2020-10-20',
        'tempos' => 3,
        'tempos2' => 'Segunda',
        'total' => 3,
      ),
    ),
    1 => 
    array (
      1 => 
      array (
        'dataAula' => '2020-10-27',
        'tempos' => '4',
        'tempos2' => 'Terça',
        'total' => 7,
      ),
    ),
    2 => 
    array (
      2 => 
      array (
        'dataAula' => '2020-11-02',
        'tempos' => '4',
        'tempos2' => 'Segunda',
        'total' => 11,
      ),
    ),
    3 => 
    array (
      3 => 
      array (
        'dataAula' => '2020-11-03',
        'tempos' => '4',
        'tempos2' => 'Terça',
        'total' => 15,
      ),
    ),
    4 => 
    array (
      4 => 
      array (
        'dataAula' => '2020-11-09',
        'tempos' => '4',
        'tempos2' => 'Segunda',
        'total' => 19,
      ),
    ),
    5 => 
    array (
      5 => 
      array (
        'dataAula' => '2020-11-10',
        'tempos' => '4',
        'tempos2' => 'Terça',
        'total' => 23,
      ),
    ),
    6 => 
    array (
      6 => 
      array (
        'dataAula' => '2020-11-16',
        'tempos' => '4',
        'tempos2' => 'Segunda',
        'total' => 27,
      ),
    ),
    7 => 
    array (
      7 => 
      array (
        'dataAula' => '2020-11-17',
        'tempos' => '4',
        'tempos2' => 'Terça',
        'total' => 31,
      ),
    ),
    8 => 
    array (
      8 => 
      array (
        'dataAula' => '2020-11-23',
        'tempos' => '4',
        'tempos2' => 'Segunda',
        'total' => 35,
      ),
    ),
    9 => 
    array (
      9 => 
      array (
        'dataAula' => '2020-11-24',
        'tempos' => '4',
        'tempos2' => 'Terça',
        'total' => 39,
      ),
    ),
    10 => 
    array (
      10 => 
      array (
        'dataAula' => '2020-11-30',
        'tempos' => '4',
        'tempos2' => 'Segunda',
        'total' => 43,
      ),
    ),
    11 => 
    array (
      11 => 
      array (
        'dataAula' => '2020-12-07',
        'tempos' => '4',
        'tempos2' => 'Segunda',
        'total' => 47,
      ),
    ),
    12 => 
    array (
      12 => 
      array (
        'dataAula' => '2020-12-14',
        'tempos' => '4',
        'tempos2' => 'Segunda',
        'total' => 51,
      ),
    ),
    13 => 
    array (
      13 => 
      array (
        'dataAula' => '2020-12-15',
        'tempos' => '4',
        'tempos2' => 'Terça',
        'total' => 55,
      ),
    ),
    14 => 
    array (
      14 => 
      array (
        'dataAula' => '2021-01-04',
        'tempos' => '4',
        'tempos2' => 'Segunda',
        'total' => 59,
      ),
    ),
    15 => 
    array (
      15 => 
      array (
        'dataAula' => '2021-01-05',
        'tempos' => '4',
        'tempos2' => 'Terça',
        'total' => 63,
      ),
    ),
    16 => 
    array (
      16 => 
      array (
        'dataAula' => '2021-01-11',
        'tempos' => 4,
        'tempos2' => 'Segunda',
        'total' => 67,
      ),
    ),
  ),
  2 => 
  array (
    0 => 
    array (
      0 => 
      array (
        'dataAula' => '2021-01-12',
        'tempos' => '4',
        'tempos2' => 'Terça',
        'total' => 4,
      ),
    ),
    1 => 
    array (
      1 => 
      array (
        'dataAula' => '2021-01-18',
        'tempos' => '4',
        'tempos2' => 'Segunda',
        'total' => 8,
      ),
    ),
    2 => 
    array (
      2 => 
      array (
        'dataAula' => '2021-01-19',
        'tempos' => '4',
        'tempos2' => 'Terça',
        'total' => 12,
      ),
    ),
    3 => 
    array (
      3 => 
      array (
        'dataAula' => '2021-01-25',
        'tempos' => '4',
        'tempos2' => 'Segunda',
        'total' => 16,
      ),
    ),
    4 => 
    array (
      4 => 
      array (
        'dataAula' => '2021-01-26',
        'tempos' => '4',
        'tempos2' => 'Terça',
        'total' => 20,
      ),
    ),
    5 => 
    array (
      5 => 
      array (
        'dataAula' => '2021-02-01',
        'tempos' => '4',
        'tempos2' => 'Segunda',
        'total' => 24,
      ),
    ),
    6 => 
    array (
      6 => 
      array (
        'dataAula' => '2021-02-02',
        'tempos' => '4',
        'tempos2' => 'Terça',
        'total' => 28,
      ),
    ),
    7 => 
    array (
      7 => 
      array (
        'dataAula' => '2021-02-08',
        'tempos' => '4',
        'tempos2' => 'Segunda',
        'total' => 32,
      ),
    ),
    8 => 
    array (
      8 => 
      array (
        'dataAula' => '2021-02-09',
        'tempos' => 1,
        'tempos2' => 'Terça',
        'total' => 33,
      ),
    ),
  ),
)[]

So, for example i have repeated the date 2020-10-20, one with "tempos =1" and another with "tempos=3"

I'm looking to have only one date with concatened tempos field

Example

array (
        'dataAula' => '2020-10-20',
        'tempos' => 1+3,
        'tempos2' => 'Terça',
        'total' => 33,
      ),

So what i have now is this code but not getting what i need

 

foreach ($finalCronograma as $sub) {
                $row = current($sub);
                if (isset($result[$row[0]['dataAula']])) {
                    $result[$row[0]['dataAula']]['tempos'] .= '+' . $row[0]['tempos'];
                } else {
                    $result[$row[0]['dataAula']] = $sub;
                }
            }

 

any help?

Link to comment
https://forums.phpfreaks.com/topic/311647-php-array-concatenate-values/
Share on other sites

Your starting array is in a strange format.   You have your outer array with 3 sub-array lists.  Each element in that sub-list is then a single-item array with an index matching it's parent index.   I'd probably see if that can be cleaned up and better organized first if possible.   The code would end up cleaner and easier to understand if you could remove one or two of those levels.   It seems like you could probably just form one single list instead of the nested setup you have now.  For example:

array (
    0 => 
    array (
        'dataAula' => '2020-09-21',
        'tempos' => '4',
        'tempos2' => 'Segunda',
        'total' => 4,
    ),
    1 => 
    array (
        'dataAula' => '2020-09-22',
        'tempos' => '4',
        'tempos2' => 'Terça',
        'total' => 8,
    ),
    //...
);

At the vary least it should be possible to remove the third level of the array you currently have.

 

That said, what you want to do is possible with your setup.  You need another loop though as you need to loop over the outer array first.  Each item of that loop is another array you need to loop over.  Each item of that inner loop is another single-element array that you need to "unwrap" so to speak to get access to the data.

$result = [];
//Loop over list $array, each item of which is another list.
foreach ($array as $level1){
    //Loop over the sub-list.
    foreach ($level1 as $level2){
        //$level2 is a single-element array with the same index as it's parent.  Extract that single element.
        $level2 = current($level2);
        $date = $level2['dataAula'];
        if (isset($result[$date])){
            $result[$date]['tempos'] .= '+' . $level2['tempos'];
        } else {
            $result[$date] = $level2;
        }
    }
}

 

Hi Kicken

 

Thank's for helping me.

You are right about your explanation.

I have followed your explaination and tried your code but there is an error, the var_export of the code provided gives me this array

 

array (
  'idPlan' => '718',
  'idCpModulos' => '318',
  'dataAula' => '2020-09-22',
  'tempos' => '4',
  'tempos2' => '0',
  'dataCriado' => '2020-10-27 17:52:00',
)array (
  'idPlan' => '718',
  'idCpModulos' => '318',
  'dataAula' => '2020-09-23',
  'tempos' => '4',
  'tempos2' => '0',
  'dataCriado' => '2020-10-27 17:52:00',
)array (
  'idPlan' => '718',
  'idCpModulos' => '318',
  'dataAula' => '2020-09-29',
  'tempos' => '4',
  'tempos2' => '0',
  'dataCriado' => '2020-10-27 17:52:00',
)array (
  'idPlan' => '718',
  'idCpModulos' => '318',
  'dataAula' => '2020-09-30',
  'tempos' => '4',
  'tempos2' => '0',
  'dataCriado' => '2020-10-27 17:52:00',
)array (
  'idPlan' => '718',
  'idCpModulos' => '318',
  'dataAula' => '2020-10-06',
  'tempos' => '4',
  'tempos2' => '0',
  'dataCriado' => '2020-10-27 17:52:00',
)array (
  'idPlan' => '718',
  'idCpModulos' => '318',
  'dataAula' => '2020-10-07',
  'tempos' => '4',
  'tempos2' => '0',
  'dataCriado' => '2020-10-27 17:52:00',
)array (
  'idPlan' => '718',
  'idCpModulos' => '318',
  'dataAula' => '2020-10-13',
  'tempos' => '4',
  'tempos2' => '0',
  'dataCriado' => '2020-10-27 17:52:00',
)array (
  'idPlan' => '718',
  'idCpModulos' => '318',
  'dataAula' => '2020-10-14',
  'tempos' => '4',
  'tempos2' => '0',
  'dataCriado' => '2020-10-27 17:52:00',
)array (
  'idPlan' => '718',
  'idCpModulos' => '318',
  'dataAula' => '2020-10-20',
  'tempos' => '1',
  'tempos2' => '0',
  'dataCriado' => '2020-10-27 17:52:00',
)array (
  'idPlan' => '719',
  'idCpModulos' => '319',
  'dataAula' => '2020-10-20',
  'tempos' => '3',
  'tempos2' => '0',
  'dataCriado' => '2020-10-27 17:52:00',
)array (
  'idPlan' => '719',
  'idCpModulos' => '319',
  'dataAula' => '2020-10-27',
  'tempos' => '4',
  'tempos2' => '0',
  'dataCriado' => '2020-10-27 17:52:00',
)array (
  'idPlan' => '719',
  'idCpModulos' => '319',
  'dataAula' => '2020-10-28',
  'tempos' => '4',
  'tempos2' => '0',
  'dataCriado' => '2020-10-27 17:52:00',
)array (
  'idPlan' => '719',
  'idCpModulos' => '319',
  'dataAula' => '2020-11-03',
  'tempos' => '4',
  'tempos2' => '0',
  'dataCriado' => '2020-10-27 17:52:00',
)array (
  'idPlan' => '719',
  'idCpModulos' => '319',
  'dataAula' => '2020-11-04',
  'tempos' => '4',
  'tempos2' => '0',
  'dataCriado' => '2020-10-27 17:52:00',
)array (
  'idPlan' => '719',
  'idCpModulos' => '319',
  'dataAula' => '2020-11-10',
  'tempos' => '4',
  'tempos2' => '0',
  'dataCriado' => '2020-10-27 17:52:00',
)array (
  'idPlan' => '719',
  'idCpModulos' => '319',
  'dataAula' => '2020-11-11',
  'tempos' => '4',
  'tempos2' => '0',
  'dataCriado' => '2020-10-27 17:52:00',
)array (
  'idPlan' => '719',
  'idCpModulos' => '319',
  'dataAula' => '2020-11-17',
  'tempos' => '4',
  'tempos2' => '0',
  'dataCriado' => '2020-10-27 17:52:00',
)array (
  'idPlan' => '719',
  'idCpModulos' => '319',
  'dataAula' => '2020-11-18',
  'tempos' => '4',
  'tempos2' => '0',
  'dataCriado' => '2020-10-27 17:52:00',
)array (
  'idPlan' => '719',
  'idCpModulos' => '319',
  'dataAula' => '2020-11-24',
  'tempos' => '4',
  'tempos2' => '0',
  'dataCriado' => '2020-10-27 17:52:00',
)array (
  'idPlan' => '719',
  'idCpModulos' => '319',
  'dataAula' => '2020-11-25',
  'tempos' => '4',
  'tempos2' => '0',
  'dataCriado' => '2020-10-27 17:52:00',
)array (
  'idPlan' => '719',
  'idCpModulos' => '319',
  'dataAula' => '2020-12-02',
  'tempos' => '4',
  'tempos2' => '0',
  'dataCriado' => '2020-10-27 17:52:00',
)array (
  'idPlan' => '719',
  'idCpModulos' => '319',
  'dataAula' => '2020-12-09',
  'tempos' => '4',
  'tempos2' => '0',
  'dataCriado' => '2020-10-27 17:52:00',
)array (
  'idPlan' => '719',
  'idCpModulos' => '319',
  'dataAula' => '2020-12-15',
  'tempos' => '4',
  'tempos2' => '0',
  'dataCriado' => '2020-10-27 17:52:00',
)array (
  'idPlan' => '719',
  'idCpModulos' => '319',
  'dataAula' => '2020-12-16',
  'tempos' => '4',
  'tempos2' => '0',
  'dataCriado' => '2020-10-27 17:52:00',
)array (
  'idPlan' => '719',
  'idCpModulos' => '319',
  'dataAula' => '2021-01-05',
  'tempos' => '4',
  'tempos2' => '0',
  'dataCriado' => '2020-10-27 17:52:00',
)array (
  'idPlan' => '719',
  'idCpModulos' => '319',
  'dataAula' => '2021-01-06',
  'tempos' => '4',
  'tempos2' => '0',
  'dataCriado' => '2020-10-27 17:52:00',
)array (
  'idPlan' => '720',
  'idCpModulos' => '320',
  'dataAula' => '2021-01-12',
  'tempos' => '4',
  'tempos2' => '0',
  'dataCriado' => '2020-10-27 17:52:00',
)array (
  'idPlan' => '720',
  'idCpModulos' => '320',
  'dataAula' => '2021-01-13',
  'tempos' => '4',
  'tempos2' => '0',
  'dataCriado' => '2020-10-27 17:52:00',
)array (
  'idPlan' => '720',
  'idCpModulos' => '320',
  'dataAula' => '2021-01-19',
  'tempos' => '4',
  'tempos2' => '0',
  'dataCriado' => '2020-10-27 17:52:00',
)array (
  'idPlan' => '720',
  'idCpModulos' => '320',
  'dataAula' => '2021-01-20',
  'tempos' => '4',
  'tempos2' => '0',
  'dataCriado' => '2020-10-27 17:52:00',
)array (
  'idPlan' => '720',
  'idCpModulos' => '320',
  'dataAula' => '2021-01-26',
  'tempos' => '4',
  'tempos2' => '0',
  'dataCriado' => '2020-10-27 17:52:00',
)array (
  'idPlan' => '720',
  'idCpModulos' => '320',
  'dataAula' => '2021-01-27',
  'tempos' => '4',
  'tempos2' => '0',
  'dataCriado' => '2020-10-27 17:52:00',
)array (
  'idPlan' => '720',
  'idCpModulos' => '320',
  'dataAula' => '2021-02-02',
  'tempos' => '4',
  'tempos2' => '0',
  'dataCriado' => '2020-10-27 17:52:00',
)array (
  'idPlan' => '720',
  'idCpModulos' => '320',
  'dataAula' => '2021-02-03',
  'tempos' => '4',
  'tempos2' => '0',
  'dataCriado' => '2020-10-27 17:52:00',
)array (
  'idPlan' => '720',
  'idCpModulos' => '320',
  'dataAula' => '2021-02-09',
  'tempos' => '1',
  'tempos2' => '0',
  'dataCriado' => '2020-10-27 17:52:00',
)array (
)

When it reachs this line in your code

$date = $level2['dataAula'];

I have this error

<b>Warning</b>:  Illegal string offset 'dataAula'

But when i see the array, it looks right to me.....what do i missing?

 

Thanks

 

This thread is more than a year old. Please don't revive it unless you have something important to add.

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.

Guest
Reply to this topic...

×   Pasted as rich text.   Restore formatting

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

×
×
  • Create New...

Important Information

We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue.