Ask how to add elements to a two-dimensional array.

Source: Internet
Author: User
Ask how to add elements to a two-dimensional array.
 $value);                if($somecondition){                        foreach($arr as $k => $v){                                $arr[$k][] = array("value"=>"");                        }                }        }?>


In this case, create a two-dimensional array and add elements cyclically, but when a condition $ somecondition occurs, add an element with null value to each $ key in the array.

After adding the element, we can see that all the added null elements are at the beginning of the array. why?

How can I add them correctly.


Reply to discussion (solution)

If no key name is specified for the given value, the current maximum integer index value is used, and the new key name is the value plus one. If the specified key name already has a value, the value will be overwritten.

At least your description is incorrect. $ key and $ k are not the same thing.
So
$ Arr [$ key] [] = array ("value" => $ value );
And
$ Arr [$ k] [] = array ("value" => "");
The assignment positions are different.

You 'd better paste the format example you need

At least your description is incorrect. $ key and $ k are not the same thing.
So
$ Arr [$ key] [] = array ("value" => $ value );
And
$ Arr [$ k] [] = array ("value" => "");
The assignment positions are different.

You 'd better paste the format example you need

 $ Value); if ($ somecondition) {foreach ($ arr as $ k => $ v) {$ arr [$ k] [] = array ("value" => "") ;}}}?>


My entire process is like this.
Determine the order of each key in the while (mysql_fetch_array ($ res1.
Then, in the while (mysql_fetch_array ($ res2), add an element to each key. when a condition is met, fill in an empty element.

What I got is:
{
Key1: [[], {"value": "" },{ "value ": 36.63 },{ "value": 35.97 },{ "value": 32.8 },{ "value": 32.65}
Key2: [[], {"value": "" },{ "value ": 36.63 },{ "value": 35.97 },{ "value": 32.8 },{ "value": 32.65}
}
The original array I got is:
{
Key1: [{"value": 36.63 },{ "value": 35.97 },{ "value": "" },{ "value ":""}, {"value": "" },{ "value": 32.8 },{ "value": 32.65}
Key2: [{"value": 36.63 },{ "value": 35.97 },{ "value": "" },{ "value ":""}, {"value": "" },{ "value": 32.8 },{ "value": 32.65}
}

The [] above the array does not affect the drawing, but it does not match the standard format.

At least your description is incorrect. $ key and $ k are not the same thing.
So
$ Arr [$ key] [] = array ("value" => $ value );
And
$ Arr [$ k] [] = array ("value" => "");
The assignment positions are different.

You 'd better paste the format example you need

Or what should I do if I want to implement my LS logic?

So, why do you not associate queries?

Return 2L. I printed the $ k name in foreach. it is indeed the same as the $ key in the outer while.

Back to 5L, there are some time points in the data inventory. each time point has dozens of values corresponding to $ key,

My current practice is to first query it again, for example, all day long, calculate the top 10 in the total ranking to determine the order of the $ key, and then query the value assignment again.

I am not familiar with the select command of mysql if I directly associate the query in the database. I am worried that the previous time of a key is 10th bits and the next time is 11th bits, the statistics will not arrive at that time,

During two queries, if data changes between the two queries, what do you do?

The first query selects a large range to calculate the top 10 in the total ranking, which can reflect the overall trend to a certain extent. the rankings at the intermediate time points are not the same as the total ranking, but the general trend is consistent, in addition, I checked the historical records.

For some reason, there are no records at some time points, so I want to insert a null value for each key at this time point, I just don't know why the inserted null value ran to the front of the array ....

By the way, if it is a one-dimensional array, I can directly use the arsort function for ranking, but because the json format is fixed, we can only get the two-dimensional format specified.

I understand what you mean.

while(mysql_fetch_array){  $arr[$key][] = array("value"=>$value);  if($somecondition){    $arr[$key][] = array("value"=>"");  }}

I understand what you mean.
Python code? 123456 while (mysql_fetch_array) {$ arr [$ key] [] = array ("value" => $ value); if ($ somecondition) {$ arr [$ key] [] = array ("value" => "");}}

.
$ Somecondition is to judge that the time point in mysql_fetch_array is much different from the previous time point, so I will add the time point according to the interval, at this time point, I should add a null value to all keys, so I used foreach to traverse it again. The supplementary result is that the null value is added to the beginning of the array.

Foreach should be needed ..

The idea should be like this.
While (mysql_fetch_array ){
If ($ somecondition) {// if the gap is too large
$ Arr [$ key] [] = array ("value" => ""); // inserts a null value. if there is more than one value, use the for loop.
}
$ Arr [$ key] [] = array ("value" => $ value); // add data
}

The idea should be like this.
While (mysql_fetch_array ){
If ($ somecondition) {// if the gap is too large
$ Arr [$ key] [] = array ("value" => ""); // inserts a null value. if there is more than one value, use the for loop.
}
$ Arr [$ key] [] = array ("value" => $ value); // add data
}
......

I know what went wrong .. The statements I have queried from mysql are not sorted by time point, but by key... It's too bad ..

Didn't you perform two queries?
Sort by key for the first time
The second time is sorted by time point.

The idea should be like this.
While (mysql_fetch_array ){
If ($ somecondition) {// if the gap is too large
$ Arr [$ key] [] = array ("value" => ""); // inserts a null value. if there is more than one value, use the for loop.
}
$ Arr [$ key] [] = array ("value" => $ value); // add data
}
......

Thank you .. In fact, the logic and implementation are both correct. I am not refined in mysql, and the latter two conditions of group by are reversed.

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.