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_materialTypeID as $key => $val){ $getMaterialType = new so_getMaterialType ; //为什么每次循环都要新申请一遍,而不是放在循环外面申请一次即可?? $getMaterialType->CreateTime = $val['CreateTime']; $getMaterialType->IsDefault = $val['IsDefault']; $getMaterialType->UpdateTime = $val['UpdateTime'] ; $p[] = $getMaterialType ;}var_dump($p);return $p; /*????????这种写法与上面差别是什么?????????????*/$getMaterialType = array();$getMaterialType[] = new so_getMaterialType ;$sql = 'SELECT * from tb where lid<50';$re_materialTypeID = $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;
回复讨论(解决方案)
我在用amf2.1.1 与flex通讯,产生的问题,调试过程中,传递这样类的数组总是有问题
第一段代码
$p 是一个含有若干个 so_getMaterialType 对象的数组
第二段代码
$getMaterialType 的第一个元素是 so_getMaterialType 对象,其余的是 stdClass 对象
显然你需要的是第一种写法的结果
因此可以
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) ;}
比如循环了10次,第一种写法保存了so_getMaterialType的10个对象,第二种写法只保存了一个对象,所以第二种写法里得到的数组值是一样的。
这就好比下面这一段代码:
$value = 10;$array[0] = $value;$value=12;$array[1] = $value;print_r($array);
得到的结果必然是两个12
不好意思,上面的例子错了。只有在对象的赋值中才会有这种,因为对象本质上是一个指针
终于和flex通了一个函数,谢谢楼主。
比如循环了10次,第一种写法保存了so_getMaterialType的10个对象,第二种写法只保存了一个对象,所以第二种写法里得到的数组值是一样的。
这就好比下面这一段代码:
$value = 10;$array[0] = $value;$value=12;$array[1] = $value;print_r($array);
得到的结果必然是两个12
第15行:
$getMaterialType = new so_getMaterialType ; //为什么每次循环都要新申请一遍,而不是放在循环外面申请一次即可??
你的意思是,如果只申请一次new so_getMaterialType 那么数组p中的每个类对象都是一样的值,对吧,我在跟踪过程中的确是这样的,
$p[] = $getMaterialType ; 这里,p[] 理解成数组的指针? ,指向10个类对象,对么?
$getMaterialType = new so_getMaterialType ;
关键字new会生成一个内存空间,该内存的地址赋给$getMaterialType,即$getMaterialType是一个指向前面内存空间的地址。所以,对象$getMaterialType就是指针。
$p[] = $getMaterialType ;
因为第一种写法没有改变$getMaterialType的值,所有p[]里所有元素指向同一块内存地址,数据当然也就一样了。
而第二种写法中,每次都会生成一个新的内存空间,并把地址赋给$getMaterialType,所以p[]中元素指向的是不同的内存空间。
简单来说:
只有$getMaterialType = ×××才会改变$getMaterialType值,$getMaterialType->属性 = ×××不会改变$getMaterialType的值

Long URLs, often cluttered with keywords and tracking parameters, can deter visitors. A URL shortening script offers a solution, creating concise links ideal for social media and other platforms. These scripts are valuable for individual websites a

Following its high-profile acquisition by Facebook in 2012, Instagram adopted two sets of APIs for third-party use. These are the Instagram Graph API and the Instagram Basic Display API.As a developer building an app that requires information from a

Laravel simplifies handling temporary session data using its intuitive flash methods. This is perfect for displaying brief messages, alerts, or notifications within your application. Data persists only for the subsequent request by default: $request-

This is the second and final part of the series on building a React application with a Laravel back-end. In the first part of the series, we created a RESTful API using Laravel for a basic product-listing application. In this tutorial, we will be dev

Laravel provides concise HTTP response simulation syntax, simplifying HTTP interaction testing. This approach significantly reduces code redundancy while making your test simulation more intuitive. The basic implementation provides a variety of response type shortcuts: use Illuminate\Support\Facades\Http; Http::fake([ 'google.com' => 'Hello World', 'github.com' => ['foo' => 'bar'], 'forge.laravel.com' =>

The PHP Client URL (cURL) extension is a powerful tool for developers, enabling seamless interaction with remote servers and REST APIs. By leveraging libcurl, a well-respected multi-protocol file transfer library, PHP cURL facilitates efficient execution of various network protocols, including HTTP, HTTPS, and FTP. This extension offers granular control over HTTP requests, supports multiple concurrent operations, and provides built-in security features.

Do you want to provide real-time, instant solutions to your customers' most pressing problems? Live chat lets you have real-time conversations with customers and resolve their problems instantly. It allows you to provide faster service to your custom

The 2025 PHP Landscape Survey investigates current PHP development trends. It explores framework usage, deployment methods, and challenges, aiming to provide insights for developers and businesses. The survey anticipates growth in modern PHP versio


Hot AI Tools

Undresser.AI Undress
AI-powered app for creating realistic nude photos

AI Clothes Remover
Online AI tool for removing clothes from photos.

Undress AI Tool
Undress images for free

Clothoff.io
AI clothes remover

AI Hentai Generator
Generate AI Hentai for free.

Hot Article

Hot Tools

EditPlus Chinese cracked version
Small size, syntax highlighting, does not support code prompt function

Dreamweaver Mac version
Visual web development tools

ZendStudio 13.5.1 Mac
Powerful PHP integrated development environment

SublimeText3 Mac version
God-level code editing software (SublimeText3)

mPDF
mPDF is a PHP library that can generate PDF files from UTF-8 encoded HTML. The original author, Ian Back, wrote mPDF to output PDF files "on the fly" from his website and handle different languages. It is slower than original scripts like HTML2FPDF and produces larger files when using Unicode fonts, but supports CSS styles etc. and has a lot of enhancements. Supports almost all languages, including RTL (Arabic and Hebrew) and CJK (Chinese, Japanese and Korean). Supports nested block-level elements (such as P, DIV),
