如何在PHP中创建列表或对象数组?

时间:2022-01-22 04:18:40

I'm a .net programmer vb & c#, but I can't seem to figure out how to get my objects into a list or array in PHP.

我是.net程序员vb&c#,但我似乎无法弄清楚如何将我的对象变成PHP中的列表或数组。

var mylist = new List<myobject>();

mylist.add(myobject1);
mylist.add(myobject2);

What I have tried.
Products being a property for a collection of orderitems:

我试过的。产品是一系列orderitems的财产:

$this->Products = getOrderItems();

public function getOrderItems()
{
    $items = array();
    $count = 0;

      // connect to db, query.....

    while($row = mysql_fetch_array($result, MYSQL_BOTH)){
        $count++;
        $items[$count] = ($row);
    }
    echo 'Count of Order Items...' . $count;

    return $items;
}

Am I even close?

我甚至关闭了吗?

3 个解决方案

#1


$items = array();

while($row = mysql_fetch_array($result, MYSQL_BOTH)) {
    $items[] = $row;
}
echo 'Count of Order Items...', count($items);

#2


  • $this->Products = getOrderItems(); is legal in PHP, but it refers to the (global) function getOrderItems() instead of the class method. class methods and variables always have to be prefixed with $this-> (or self::, if they're static vars) when called from inside the class.
    in your sample-code, you have that wrong. getOrderItems is defined as class method, but your call is not $this->-scoped, thus php assumes a function. it should throw an function not found-error.

    $ this-> Products = getOrderItems();在PHP中是合法的,但它指的是(全局)函数getOrderItems()而不是类方法。当从类内部调用时,类方法和变量总是必须以$ this->(或self ::,如果它们是静态变量)作为前缀。在您的示例代码中,您有错误。 getOrderItems被定义为类方法,但是你的调用不是$ this - > - scoped,因此php假定一个函数。它应该抛出一个未找到的函数 - 错误。

  • the [] notation adds an element to the end of an array.

    []表示法将一个元素添加到数组的末尾。

  • the index of the first element in your sample code is 1 (isn't that the standard case for VB?). php normally starts at 0 - though it's possible (because php-arrays are not real arrays) to start at arbitrary indices i'd recommend to stick with zero.

    示例代码中第一个元素的索引是1(不是VB的标准情况?)。 php通常从0开始 - 虽然它可能(因为php数组不是真正的数组)从任意索引开始,我建议坚持零。

  • mysql_fetch_array is an ancient way of working with mysql. nowadays you're better of with mysqli or (even better) PDO.

    mysql_fetch_array是一种使用mysql的古老方式。现在你对mysqli或(甚至更好)PDO更好。

  • (...) a list or array in php.

    (...)php中的列表或数组。

    lists, arrays, stacks, whatever: in php everthing is an ordered map (misleadingly called array):

    列表,数组,堆栈,等等:在php中,everthing是一个有序的映射(误导性地称为数组):

    PHP: Arrays: An array in PHP is actually an ordered map. A map is a type that associates values to keys. This type is optimized for several different uses; it can be treated as an array, list (vector), hash table (an implementation of a map), dictionary, collection, stack, queue, and probably more. As array values can be other arrays, trees and multidimensional arrays are also possible.

    PHP:数组:PHP中的数组实际上是一个有序的映射。映射是将值与键关联的类型。此类型针对多种不同用途进行了优化;它可以被视为数组,列表(向量),哈希表(地图的实现),字典,集合,堆栈,队列,甚至更多。由于数组值可以是其他数组,因此树和多维数组也是可能的。

update:

sorry, i haven't got enough time right now to explain the finer nuances of pdo/mysqli over mysql.

对不起,我现在还没有足够的时间来解释pdo / mysqli在mysql上的细微差别。

so here are just the basics:

所以这里只是基础知识:

  • oop: pdo and mysqli are object oriented (tough mysqli got functional aliases)

    oop:pdo和mysqli是面向对象的(艰难的mysqli有功能别名)

  • prep statements: most important: pdo/mysqli got prepared statements. that means, you first prepare the query with placeholders once, then fill in the values later (without the need to prepare the query a second time). this approach has 3 obvious advantages:

    预备陈述:最重要的是:pdo / mysqli得到了准备好的陈述。这意味着,您首先使用占位符准备查询一次,然后稍后填写值(无需再次准备查询)。这种方法有3个明显的优点:

    • performance: it's faster, because the database only has to analyze, compile and optimize the query once (at least with complex queries)

      性能:它更快,因为数据库只需要分析,编译和优化查询一次(至少对于复杂的查询)

    • security: no need for quoted strings (happens automatically!), making sql-injection attacks harder

      安全性:不需要引用字符串(自动发生!),使sql注入攻击更难

    • maintainability: the logic and data part of the query are separated, thus easier to read and you don't have to do a lot of string concenation

      可维护性:查询的逻辑和数据部分是分开的,因此更容易阅读,您不必进行大量的字符串概念

  • driver driven: pdo is not database specific. there are several supported db-systems, making it easier to port your code to other db-backends (but it's not an db-abstraction layer like ODBC, so the SQL still has to be compatible) and increasing reusability

    驱动程序驱动:pdo不是特定于数据库的。有几个受支持的数据库系统,可以更容易地将代码移植到其他数据库后端(但它不像ODBC这样的数据库抽象层,因此SQL仍然必须兼容)并提高可重用性

of course, there's a lot more to it

当然,还有很多东西

#3


What orlandu63 posted is correct - using $items[] = $row means that $row is appended numerically as the next element of $items.

发布的orlandu63是正确的 - 使用$ items [] = $ row表示$ row作为$ items的下一个元素以数字形式附加。

Another option is that if there's an id field in $row, you can do $items[$row->id] = $row;, which has the advantage of indexing your array and making it easier to find a given item.

另一种选择是,如果$ row中有一个id字段,你可以执行$ items [$ row-> id] = $ row;,这样可以为你的数组编制索引并更容易找到给定的项目。

I really suggest reading through http://www.php.net/manual/en/language.types.array.php, which will explain to you some of the cool things PHP allows with arrays.

我真的建议通过http://www.php.net/manual/en/language.types.array.php阅读,它将向您解释PHP允许使用数组的一些很酷的东西。

#1


$items = array();

while($row = mysql_fetch_array($result, MYSQL_BOTH)) {
    $items[] = $row;
}
echo 'Count of Order Items...', count($items);

#2


  • $this->Products = getOrderItems(); is legal in PHP, but it refers to the (global) function getOrderItems() instead of the class method. class methods and variables always have to be prefixed with $this-> (or self::, if they're static vars) when called from inside the class.
    in your sample-code, you have that wrong. getOrderItems is defined as class method, but your call is not $this->-scoped, thus php assumes a function. it should throw an function not found-error.

    $ this-> Products = getOrderItems();在PHP中是合法的,但它指的是(全局)函数getOrderItems()而不是类方法。当从类内部调用时,类方法和变量总是必须以$ this->(或self ::,如果它们是静态变量)作为前缀。在您的示例代码中,您有错误。 getOrderItems被定义为类方法,但是你的调用不是$ this - > - scoped,因此php假定一个函数。它应该抛出一个未找到的函数 - 错误。

  • the [] notation adds an element to the end of an array.

    []表示法将一个元素添加到数组的末尾。

  • the index of the first element in your sample code is 1 (isn't that the standard case for VB?). php normally starts at 0 - though it's possible (because php-arrays are not real arrays) to start at arbitrary indices i'd recommend to stick with zero.

    示例代码中第一个元素的索引是1(不是VB的标准情况?)。 php通常从0开始 - 虽然它可能(因为php数组不是真正的数组)从任意索引开始,我建议坚持零。

  • mysql_fetch_array is an ancient way of working with mysql. nowadays you're better of with mysqli or (even better) PDO.

    mysql_fetch_array是一种使用mysql的古老方式。现在你对mysqli或(甚至更好)PDO更好。

  • (...) a list or array in php.

    (...)php中的列表或数组。

    lists, arrays, stacks, whatever: in php everthing is an ordered map (misleadingly called array):

    列表,数组,堆栈,等等:在php中,everthing是一个有序的映射(误导性地称为数组):

    PHP: Arrays: An array in PHP is actually an ordered map. A map is a type that associates values to keys. This type is optimized for several different uses; it can be treated as an array, list (vector), hash table (an implementation of a map), dictionary, collection, stack, queue, and probably more. As array values can be other arrays, trees and multidimensional arrays are also possible.

    PHP:数组:PHP中的数组实际上是一个有序的映射。映射是将值与键关联的类型。此类型针对多种不同用途进行了优化;它可以被视为数组,列表(向量),哈希表(地图的实现),字典,集合,堆栈,队列,甚至更多。由于数组值可以是其他数组,因此树和多维数组也是可能的。

update:

sorry, i haven't got enough time right now to explain the finer nuances of pdo/mysqli over mysql.

对不起,我现在还没有足够的时间来解释pdo / mysqli在mysql上的细微差别。

so here are just the basics:

所以这里只是基础知识:

  • oop: pdo and mysqli are object oriented (tough mysqli got functional aliases)

    oop:pdo和mysqli是面向对象的(艰难的mysqli有功能别名)

  • prep statements: most important: pdo/mysqli got prepared statements. that means, you first prepare the query with placeholders once, then fill in the values later (without the need to prepare the query a second time). this approach has 3 obvious advantages:

    预备陈述:最重要的是:pdo / mysqli得到了准备好的陈述。这意味着,您首先使用占位符准备查询一次,然后稍后填写值(无需再次准备查询)。这种方法有3个明显的优点:

    • performance: it's faster, because the database only has to analyze, compile and optimize the query once (at least with complex queries)

      性能:它更快,因为数据库只需要分析,编译和优化查询一次(至少对于复杂的查询)

    • security: no need for quoted strings (happens automatically!), making sql-injection attacks harder

      安全性:不需要引用字符串(自动发生!),使sql注入攻击更难

    • maintainability: the logic and data part of the query are separated, thus easier to read and you don't have to do a lot of string concenation

      可维护性:查询的逻辑和数据部分是分开的,因此更容易阅读,您不必进行大量的字符串概念

  • driver driven: pdo is not database specific. there are several supported db-systems, making it easier to port your code to other db-backends (but it's not an db-abstraction layer like ODBC, so the SQL still has to be compatible) and increasing reusability

    驱动程序驱动:pdo不是特定于数据库的。有几个受支持的数据库系统,可以更容易地将代码移植到其他数据库后端(但它不像ODBC这样的数据库抽象层,因此SQL仍然必须兼容)并提高可重用性

of course, there's a lot more to it

当然,还有很多东西

#3


What orlandu63 posted is correct - using $items[] = $row means that $row is appended numerically as the next element of $items.

发布的orlandu63是正确的 - 使用$ items [] = $ row表示$ row作为$ items的下一个元素以数字形式附加。

Another option is that if there's an id field in $row, you can do $items[$row->id] = $row;, which has the advantage of indexing your array and making it easier to find a given item.

另一种选择是,如果$ row中有一个id字段,你可以执行$ items [$ row-> id] = $ row;,这样可以为你的数组编制索引并更容易找到给定的项目。

I really suggest reading through http://www.php.net/manual/en/language.types.array.php, which will explain to you some of the cool things PHP allows with arrays.

我真的建议通过http://www.php.net/manual/en/language.types.array.php阅读,它将向您解释PHP允许使用数组的一些很酷的东西。