Questions about php dynamic generation of array elements

Source: Internet
Author: User
Php needs an array to dynamically generate array elements, but the elements in the array are not fixed. Therefore, we need to generate the elements of the array in advance and then create an array, as a result, the generated element becomes an element of the subsequently created array. The code is as follows: for ($ i0; $ I & lt; 100; $ I ++) {$ ary. $ ay. & quot; & gt; & quot ;. $ as; element} $ dataarray ($ ary); php dynamically generates array elements
Now we need an array, but the elements in the array are not fixed, so we will generate the elements of the array in advance, and then create an array, as a result, the generated element becomes an element of the subsequently created array. The code is as follows:
For ($ I = 0; $ I <= 100; $ I ++ ){
$ Ary. = $ ay. "=>". $ as; // element
}
$ Data = array ($ ary); // The created array

The format I want is as follows. $ data = array ('a' => 1, 'B' = 2, 'C' => 3); however, it is impossible to put the generated elements in it, enter the generated elements, copy them, and put them in.
I don't know what the problem is. please help me solve it!

------ Solution --------------------
For ($ I = 0; $ I <= 100; $ I ++ ){
$ Ary. = $ ay. "=>". $ as; // element
}
$ Data = array ($ ary); // The created array
This statement is incorrect. when creating an array, array ($ list );
$ List: this is a list, not a string. you can create only one element in this way.
------ Solution --------------------

To read this reply, log on to or register CSDN!


------ Solution --------------------
PHP code
$ Arr = array (); for ($ I = 0; $ I <10; $ I ++) $ arr [] = $ I * $ I;

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.