Questions about adding elements to a two-dimensional array

Source: Internet
Author: User
Ask how to add elements to a two-dimensional array. & Lt ;? Php & nbsp; while (mysql_fetch_array) {& nbsp; & nbsp; & nbs ask to add elements to a two-dimensional array.
 
        while(mysql_fetch_array){
                $arr[$key][] = array("value"=>$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. Two-dimensional array PHP
------ 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.
------ Solution --------------------
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
}

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.