PHP multi-dimensional array problem, a little difficult!

Source: Internet
Author: User
PHP multi-dimensional array problem, a little difficult! Two arrays are as follows:
// Registration information $ array1 = array (0 => array ('day' => '2017-3-7 ', // registration date 'data' => array (0 => array ('uid' => '123 '), 1 => array ('uid' => '000000'), 3 => array ('uid' => '000000 '), 4 => array ('uid' => '000000'),), 1 => array ('day' => '2017-3-8 ', // registration date 'data' => array (0 => array ('uid' => '123 '), 1 => array ('uid' => '000000'), 2 => array ('uid' => '000000 '),)), 2 => array ('day' => '2017-3-10 ', // registration date 'data' => array (0 => array ('uid' => '123 '), 1 => array ('uid' => '000000'), 2 => array ('uid' => '000000 '), 3 => array ('uid' => '000000'), 4 => array ('uid' => '000000 '),)), 3 => array ('day' => '2017-3-11 ', // registration date 'data' => array (0 => array ('uid' => '123 '), 1 => array ('uid' => '000000'), 2 => array ('uid' => '000000 '), 3 => array ('uid' => '000000'), 4 => array ('uid' => '000000 '),)),); // log $ array2 = array (0 => array ('day' => '2017-3-8 ', // logon date: 'GE _ data' => array (0 => array ('uid' => '123 '), 1 => array ('uid' => '000000'), 3 => array ('uid' => '000000 '))), 1 => array ('day' => '2017-3-9 ', // logon date: 'GE _ data' => array (0 => array ('uid' => '123 '), 1 => array ('uid' => '000000'), 2 => array ('uid' => '000000 '),)), 2 => array ('day' => '2017-3-11 ', // logon date: 'GE _ data' => array (0 => array ('uid' => '123 '), 1 => array ('uid' => '000000'), 2 => array ('uid' => '000000 '), 3 => array ('uid' => '000000'), 4 => array ('uid' => '000000 '), 5 => array ('uid' => '000000'), 6 => array ('uid' => '000000 '))));


How to obtain the following array:


Help me!


Reply to discussion (solution)

It's really hard...

It is purely a group.

However, the code is too specific and may not be useful once.

We recommend that you store this type of data in the database and then click it.

Your array and requirements are not clear.

It is purely a group.

However, the code is too specific and may not be useful once.

We recommend that you store this type of data in the database and then click it. this is what you read from the database.

It seems that your array and requirements are not very clear. In fact, it is to calculate the number of retained data for the next day. For example, four people were registered on July 15, March 7, and who had logged on July 15, March 8.

3rd why should the number of logins on the seventh day be 0 and not 11? Seven people logged on that day.

3rd why should the number of logins on the seventh day be 0 and not 11? Seven people logged on that day because the number of registrants in March 9 was zero. Therefore, the number of logon users on the nth day of June is 0.


3rd why should the number of logins on the seventh day be 0 and not 11? Seven people logged on that day because the number of registrants in March 9 was zero. Therefore, the number of logon users on the nth day of June is 0.
In addition, I am confused about the logic.



3rd why should the number of logins on the seventh day be 0 and not 11? Seven people logged on that day because the number of registrants in March 9 was zero. Therefore, the number of logon users on the nth day of June is 0.
In addition, this logic indicates that I had a messy number of registrants in and the number of registrants in was zero. Therefore, the uid of this day is empty. Therefore, the number of registered users in the logon log table cannot be found for the next day in.

Query the account registered on the current day. the account is logged on the next n (n is a natural number) day;
The final result to be implemented is as follows:


Don't paste it! Which of the following experts can help you !!!

It's not hard to take time to work overtime today...

You can use this function multiple times.

Declare an empty array of results first

Then loop through the first array and save the date

Recycle the first and second arrays, and then judge the desired data and add it to the result array.

It's not hard to take time to work overtime today...

You can use this function multiple times.

Declare an empty array of results first

Then loop through the first array and save the date

Recycle the first and second arrays, judge the desired data and add it to the result array. I tried to write this loop myself. Unfortunately, I cannot write it!

It's not like multiplication. look at the chart.

Write like this

// Registration information $ array1 = array (0 => array ('day' => '2017-3-7 ', // registration date 'data' => array (0 => array ('uid' => '123 '), 1 => array ('uid' => '000000'), 3 => array ('uid' => '000000 '), 4 => array ('uid' => '000000'),), 1 => array ('day' => '2017-3-8 ', // registration date 'data' => array (0 => array ('uid' => '123 '), 1 => array ('uid' => '000000'), 2 => array ('uid' => '000000 '),)), 2 => array ('day' => '2017-3-10 ', // registration date 'data' => array (0 => array ('uid' => '123 '), 1 => array ('uid' => '000000'), 2 => array ('uid' => '000000 '), 3 => array ('uid' => '000000'), 4 => array ('uid' => '000000 '),)), 3 => array ('day' => '2017-3-11 ', // registration date 'data' => array (0 => array ('uid' => '123 '), 1 => array ('uid' => '000000'), 2 => array ('uid' => '000000 '), 3 => array ('uid' => '000000'), 4 => array ('uid' => '000000 '),)),); // log $ array2 = array (0 => array ('day' => '2017-3-8 ', // logon date: 'GE _ data' => array (0 => array ('uid' => '123 '), 1 => array ('uid' => '000000'), 3 => array ('uid' => '000000 '))), 1 => array ('day' => '2017-3-9 ', // logon date: 'GE _ data' => array (0 => array ('uid' => '123 '), 1 => array ('uid' => '000000'), 2 => array ('uid' => '000000 '),)), 2 => array ('day' => '2017-3-11 ', // logon date: 'GE _ data' => array (0 => array ('uid' => '123 '), 1 => array ('uid' => '000000'), 2 => array ('uid' => '000000 '), 3 => array ('uid' => '000000'), 4 => array ('uid' => '000000 '), 5 => array ('uid' => '000000'), 6 => array ('uid' => '000000'); $ res = array (); // read the daily registration record foreach ($ array1 as $ r) {$ day = $ r ['day']; $ res [$ day] = array ('day' => $ day, 'count' => count ($ r ['data']); $ d = date ('Z', strtotime ($ day); // for every user registered on the current day, foreach ($ r ['data'] as $ us) {foreach ($ array2 as $ t) {$ rd = date ('Z', strtotime ($ t ['day']); // check the day when he logs on to foreach ($ t ['GE _ data'] as $ u) {if ($ us ['uid'] = $ u ['uid']) $ res [$ day] ['Day _'. ($ rd-$ d)] [] = $ us ['uid']; // Save the login uid for easy check }}} print_r ($ res );
Array(    [2014-3-7] => Array        (            [day] => 2014-3-7            [count] => 4            [day_1] => Array                (                    [0] => 0000001                    [1] => 0000002                    [2] => 0000003                )            [day_4] => Array                (                    [0] => 0000001                    [1] => 0000002                    [2] => 0000003                )            [day_2] => Array                (                    [0] => 0000002                )        )    [2014-3-8] => Array        (            [day] => 2014-3-8            [count] => 3            [day_1] => Array                (                    [0] => 0000007                    [1] => 0000006                )            [day_3] => Array                (                    [0] => 0000007                )        )    [2014-3-10] => Array        (            [day] => 2014-3-10            [count] => 5            [day_1] => Array                (                    [0] => 0000008                    [1] => 0000012                    [2] => 0000013                )        )    [2014-3-11] => Array        (            [day] => 2014-3-11            [count] => 5        ))

Unfortunately, based on your data, you cannot get the results of your example.
You probably made a mistake.

It's not like multiplication. look at the chart. haha, it's a bit like.
My function is like this. The start date is the start date, for example, 2014-1-1 1-1, and the end date, for example, 2014-1-6. the number of retention records from 2014-1-1 to 2014-1-6 is queried. check the first row, add 100 new users in 1-1, and 1-2 indicates the number of users logging on to 1-2, and 2 indicates the number of users logging on to 1-3. Ah, this function was proposed by operations. it really gave me a headache !!!

Write like this

// Registration information $ array1 = array (0 => array ('day' => '2017-3-7 ', // registration date 'data' => array (0 => array ('uid' => '123 '), 1 => array ('uid' => '000000'), 3 => array ('uid' => '000000 '), 4 => array ('uid' => '000000'),), 1 => array ('day' => '2017-3-8 ', // registration date 'data' => array (0 => array ('uid' => '123 '), 1 => array ('uid' => '000000'), 2 => array ('uid' => '000000 '),)), 2 => array ('day' => '2017-3-10 ', // registration date 'data' => array (0 => array ('uid' => '123 '), 1 => array ('uid' => '000000'), 2 => array ('uid' => '000000 '), 3 => array ('uid' => '000000'), 4 => array ('uid' => '000000 '),)), 3 => array ('day' => '2017-3-11 ', // registration date 'data' => array (0 => array ('uid' => '123 '), 1 => array ('uid' => '000000'), 2 => array ('uid' => '000000 '), 3 => array ('uid' => '000000'), 4 => array ('uid' => '000000 '),)),); // log $ array2 = array (0 => array ('day' => '2017-3-8 ', // logon date: 'GE _ data' => array (0 => array ('uid' => '123 '), 1 => array ('uid' => '000000'), 3 => array ('uid' => '000000 '))), 1 => array ('day' => '2017-3-9 ', // logon date: 'GE _ data' => array (0 => array ('uid' => '123 '), 1 => array ('uid' => '000000'), 2 => array ('uid' => '000000 '),)), 2 => array ('day' => '2017-3-11 ', // logon date: 'GE _ data' => array (0 => array ('uid' => '123 '), 1 => array ('uid' => '000000'), 2 => array ('uid' => '000000 '), 3 => array ('uid' => '000000'), 4 => array ('uid' => '000000 '), 5 => array ('uid' => '000000'), 6 => array ('uid' => '000000'); $ res = array (); // read the daily registration record foreach ($ array1 as $ r) {$ day = $ r ['day']; $ res [$ day] = array ('day' => $ day, 'count' => count ($ r ['data']); $ d = date ('Z', strtotime ($ day); // for every user registered on the current day, foreach ($ r ['data'] as $ us) {foreach ($ array2 as $ t) {$ rd = date ('Z', strtotime ($ t ['day']); // check the day when he logs on to foreach ($ t ['GE _ data'] as $ u) {if ($ us ['uid'] = $ u ['uid']) $ res [$ day] ['Day _'. ($ rd-$ d)] [] = $ us ['uid']; // Save the login uid for easy check }}} print_r ($ res );
Array(    [2014-3-7] => Array        (            [day] => 2014-3-7            [count] => 4            [day_1] => Array                (                    [0] => 0000001                    [1] => 0000002                    [2] => 0000003                )            [day_4] => Array                (                    [0] => 0000001                    [1] => 0000002                    [2] => 0000003                )            [day_2] => Array                (                    [0] => 0000002                )        )    [2014-3-8] => Array        (            [day] => 2014-3-8            [count] => 3            [day_1] => Array                (                    [0] => 0000007                    [1] => 0000006                )            [day_3] => Array                (                    [0] => 0000007                )        )    [2014-3-10] => Array        (            [day] => 2014-3-10            [count] => 5            [day_1] => Array                (                    [0] => 0000008                    [1] => 0000012                    [2] => 0000013                )        )    [2014-3-11] => Array        (            [day] => 2014-3-11            [count] => 5        ))

Unfortunately, based on your data, you cannot get the results of your example.
Thank you for making a mistake! Let me see! For more information, see! See if I can get the desired result!

Since it is read from the database .. I think it is better to improve the SQL statement .. You have checked and modified the array.

Since it is read from the database .. I think it is better to improve the SQL statement .. After you have checked and modified the array, it's a little close to the distance. this is read from the mongodb data set! What if mysql is okay! I don't have to work that hard!

Let's take a look at how you analyze this type of problem.

Rising posture! They are all great gods

Great Gods! Are there any other tricks? I am already overwhelmed here!

After reading it for a long time, I was a little dizzy. I don't know whether the content in your array is written casually or real data. if it's real data, I can't even count it, except for the second day.
If your logon count is the number of logged-on users, how can it be 1 in the third day?
The number of logon users = the number of logon logs, or the number of logon users = the number of logon logs + the number of registered users.

After reading it for a long time, I was a little dizzy. I don't know whether the content in your array is written casually or real data. if it's real data, I can't even count it, except for the second day.
If your logon count is the number of logged-on users, how can it be 1 in the third day?
The number of logon users = the number of logon logs, or the number of logon users = the number of logon logs + the number of registered users.
UID registered in March 7: 0000001,0000002, 0000003,0000004
On September 6, 0000003, the following UIDs were logged on :,
The UID for logon on April 9, March 9 is: 0000002, 0000006, 0000007
So for June March 7, the second day of his login, that is, June March 9, only one 0000002 UID
And so on...


After reading it for a long time, I was a little dizzy. I don't know whether the content in your array is written casually or real data. if it's real data, I can't even count it, except for the second day.
If your logon count is the number of logged-on users, how can it be 1 in the third day?
The number of logon users = the number of logon logs, or the number of logon users = the number of logon logs + the number of registered users.
UID registered in March 7: 0000001,0000002, 0000003,0000004
On September 6, 0000003, the following UIDs were logged on :,
The UID for logon on April 9, March 9 is: 0000002, 0000006, 0000007
So for June March 7, the second day of his login, that is, June March 9, only one 0000002 UID
And so on... Wrong. it's the third day, that is, January 1, March 9.

So it should be

    [2014-3-7] => Array        (            [day] => 2014-3-7            [count] => 4            [day_1] => Array                (                    [0] => 0000001                    [1] => 0000002                    [2] => 0000003                )             [day_4] => Array                (                    [0] => 0000001                    [1] => 0000002                    [2] => 0000003                )             [day_2] => Array                (                    [0] => 0000002                )         )

That is
3 in the next day
One on the third day
3 on the fifth day
There are no two in the fourth day!

That's not the case if you say so. why should the number of logins on the first day of June 4th Be 1 or 2?

That's not the case if you say so. why should the number of logins on the first day of June 4th Be 1 or 2?
Oh, wrong.

So it should be

    [2014-3-7] => Array        (            [day] => 2014-3-7            [count] => 4            [day_1] => Array                (                    [0] => 0000001                    [1] => 0000002                    [2] => 0000003                )             [day_4] => Array                (                    [0] => 0000001                    [1] => 0000002                    [2] => 0000003                )             [day_2] => Array                (                    [0] => 0000002                )         )

That is
3 in the next day
One on the third day
3 on the fifth day
There are no two in the fourth day! Well, I am wrong about this.

The correct algorithm will not get the wrong result
I have already provided the basic algorithm #15. your job is just to optimize it.

The correct algorithm will not get the wrong result
I have already given you the basic algorithm #15. your job is just to optimize it. I want to convert your array into an array I want.

$array = array(    '2014-3-7' => array(        'day' => '2014-3-7',        'count' => 4,        'day_1' => array(            0 => 0000001,            1 => 0000002,            2 => 0000003        ) ,        'day_4' => array(            0 => 0000001,            1 => 0000002,            2 => 0000003        ) ,        'day_3' => array(            0 => 0000001,            1 => 0000002        ) ,        'day_2' => array(            0 => 0000002        ) ,        'day_5' => array(            0 => 0000002        )    ) ,    '2014-3-8' => array(        'day' => '2014-3-8',        'count' => 3,        'day_4' => array(            0 => 0000005,            1 => 0000007        ) ,        'day_2' => array(            0 => 0000005,            1 => 0000007,            2 => 0000006        ) ,        'day_1' => array(            0 => 0000007,            1 => 0000006        ) ,        'day_3' => array(            0 => 0000007,            1 => 0000006        )    ) ,    '2014-3-10' => array(        'day' => '2014-3-10',        'count' => 5,        'day_1' => array(            0 => 0000008,            1 => 0000012,            2 => 0000013        ) ,        'day_2' => array(            0 => 0000008,            1 => 0000010,            2 => 0000012,            3 => 0000013        )    ) ,    '2014-3-11' => array(        'day' => '2014-3-11',        'count' => 5,        'day_1' => array(            0 => 0000015,            1 => 0000025,            2 => 0000018        )    ));

What is this to think about?
$ Res [$ day] ['Day _ '. ($ rd-$ d)] [] = $ us ['uid'];
Change
$ Res [$ day] ['Day _ '. ($ rd-$ d)] ++;
Isn't it counting?

What is this to think about?
$ Res [$ day] ['Day _ '. ($ rd-$ d)] [] = $ us ['uid'];
Change
$ Res [$ day] ['Day _ '. ($ rd-$ d)] ++;
Isn't it counting?

$ D = date ('Z', strtotime ($ day); what does this mean?

$ D = date ('Z', strtotime ($ day ));
Medium:
$ Day is the date string provided by the array.
Strtotime ($ day) converts a date string to a unix timestamp
Date ('Z', strtotime ($ day) gets the day of the year

Ah! The final result is still a little different from what I want!

$ D = date ('Z', strtotime ($ day ));
Medium:
$ Day is the date string provided by the array.
Strtotime ($ day) converts a date string to a unix timestamp
Date ('Z', strtotime ($ day) is the day of the year when the date is obtained. why do I change a group of data? [day_0] => Array
(
[0] = & gt; 3001000
[1] => 3001726.
[2] => 3001728.
[3] = & gt; 3001733
[4] = & gt; 3001734
[5] = & gt; 3001735
[6] = & gt; 3001745
) What is this?

When the registration information ($ array1) is the same as the logon information ($ array2) time (day), day_0 appears.

If the registration information ($ array1) is the same as the logon information ($ array2) time (day), how can day_0 be avoided? Where can I add a judgment?

What should we avoid? Registration on the same day and login on the same day, isn't it normal?

What should we avoid? Registration on the same day and login on the same day, isn't it normal?

What should we avoid? Registration on the same day and login on the same day, isn't it normal? But I just want to calculate his N-day login!


What should we avoid? Registration on the same day and login on the same day, isn't it normal?

What should we avoid? Registration on the same day and login on the same day, isn't it normal? But I just want to calculate his N-day login! Oh, I know. I can do it without it.

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.