$p[] = $MaterialType question in the Foreach Loop

Source: Internet
Author: User
Class So_getmaterialtype{var $CreateTime = '; var $IsDefault = '; var $UpdateTime = ';} $p = Array (), $sql = ' SELECT * from TB where lid<50 '; $re _materialtypeid = $db->getall ($sql); foreach ($re _materialtype  ID as $key + $val) {$getMaterialType = new So_getmaterialtype; Why do we need to apply for a new one every time, instead of applying it once in the loop? $getMaterialType->createtime = $val [' Createtime ']; $getMaterialType->isdefault = $val [' IsDefault '];$ Getmaterialtype->updatetime = $val [' UpdateTime ']; $p [] = $getMaterialType;} Var_dump ($p); return $p;/*???????? What is the difference between this and the above????????????? */$getMaterialType = array (), $getMaterialType [] = new So_getmaterialtype; $sql = ' SELECT * from TB where lid<50 '; $re _ma Terialtypeid = $db->getall ($sql), foreach ($re _materialtypeid as $key = $val) {$getMaterialType [$key]-> Createtime = $val [' Createtime ']; $getMaterialType [$key]->isdefault = $val [' IsDefault ']; $getMaterialType [$key] >updatetime = $val [' UpdateTime '];} Var_dump ($getMaterialType); return $getMaterialType;


Reply to discussion (solution)

I am using amf2.1.1 to communicate with Flex, the problem that is generated during debugging, the passing of such an array of classes is always problematic

First paragraph of code
$p is an array containing several So_getmaterialtype objects

The second piece of code
The first element of the $getMaterialType is the So_getmaterialtype object, and the rest is the StdClass object

Obviously what you need is the result of the first one.
So you can

Class So_getmaterialtype {  var $CreateTime = ';  var $IsDefault = ';  var $UpdateTime = ';  function __construct ($a) {    foreach ($a as $k, $v) $this $k = $v;  }} $p = Array (), $sql = ' SELECT * from TB where lid<50 '; $re _materialtypeid = $db->getall ($sql); foreach ($re _materialtypeid as $key = + $val) {   $p [] = new So_getmaterialtype ($val);}

For example, Loop 10 times, the first one to save the So_getmaterialtype 10 objects, the second way to save only one object, so the second way to get the same array value.

This is like the following piece of code:

$value = ten; $array [0] = $value; $value =12; $array [1] = $value;p rint_r ($array);

The result must be two of 12.

Sorry, the above example is wrong. This is only possible in the assignment of an object, because the object is essentially a pointer

Finally and flex Pass a function, thank the landlord.

For example, Loop 10 times, the first one to save the So_getmaterialtype 10 objects, the second way to save only one object, so the second way to get the same array value.

This is like the following piece of code:

$value = ten; $array [0] = $value; $value =12; $array [1] = $value;p rint_r ($array);

The result must be two of 12.

Line 15th:
$getMaterialType = new So_getmaterialtype; Why do we need to apply for a new one every time, instead of applying it once in the loop?
You mean, if you only apply once to new So_getmaterialtype, then each class object in the array p is the same value, right, I did that in the tracking process,
$p [] = $getMaterialType; Here, p[] understand the array of pointers? , point to 10 class objects, right?

$getMaterialType = new So_getmaterialtype;

The keyword new generates a memory space where the address of the memory is assigned to $getmaterialtype, which means that $getmaterialtype is an address pointing to the previous memory space. So, the object $getmaterialtype is the pointer.

$p [] = $getMaterialType;

Because the first notation does not change the value of $getmaterialtype, all elements in the p[] point to the same memory address, and the data is of course the same.
In the second notation, each time a new memory space is generated and the address is assigned to $getmaterialtype, so the element in p[] points to a different memory space.

In simple terms:
Only $getmaterialtype =xxx Change the $getmaterialtype value, $getMaterialType---property =xxx does not change the value of $getmaterialtype
  • 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.