PHP有内置的数据结构吗?

PHP有内置的数据结构吗?,php,data-structures,Php,Data Structures,我看的是,我没有看到关于大多数语言都有的数据结构的部分,比如列表和集合。我是瞎子还是PHP没有类似的内置功能?PHP有数组,实际上是关联数组,也可以用作集合。与许多解释语言一样,PHP提供了所有这些功能,而不是提供不同的显式数据类型 例如 /编辑:另外,请看一看。PHP中唯一的本机数据结构是数组。幸运的是,数组非常灵活,也可以用作哈希表 PSP>但存在SPL,是C++ STL的一个克隆。 PHP兼作列表和字典 $myArray = array("Apples", "Oranges", "Pea

我看的是,我没有看到关于大多数语言都有的数据结构的部分,比如列表和集合。我是瞎子还是PHP没有类似的内置功能?

PHP有数组,实际上是关联数组,也可以用作集合。与许多解释语言一样,PHP提供了所有这些功能,而不是提供不同的显式数据类型

例如


/编辑:另外,请看一看。

PHP中唯一的本机数据结构是数组。幸运的是,数组非常灵活,也可以用作哈希表

PSP>但存在SPL,是C++ STL的一个克隆。

PHP兼作列表和字典

$myArray = array("Apples", "Oranges", "Pears");
$myScalar = $myArray[0] // == "Apples"
或将其用作关联数组:

$myArray = array("a"=>"Apples", "b"=>"Oranges", "c"=>"Pears");
$myScalar = $myArray["a"] // == "Apples"

我想你可能想说得更具体一点,当你说数据结构时,我的思维会朝几个方向发展

阵列-它们当然有很好的文档记录,并且在中可用。()

SQL数据-取决于您使用的数据库,但大多数都是可用的。()

OOP-根据版本,可以设计和实现对象。()我必须搜索OOP才能在php站点上找到它


希望这有帮助,如果没有,很抱歉。

当然PHP有数据结构。php中的数组非常灵活。一些例子:

$foo = array(
  'bar' => array(1,'two',3),
  'baz' => explode(" ", "Some nice words")
);

然后,就有了大量的数组函数可用于映射/过滤/遍历/等结构,或转换、翻转、反转等。

关联数组可用于最基本的数据结构哈希表、队列、堆栈。但是如果你想要像树或堆这样的东西,我认为它们在默认情况下是不存在的,但是我确信任何地方都有免费的库

要让阵列模拟堆栈,请使用
array\u push()
添加,并使用
array\u pop()
启动

要让数组模拟队列,请使用
array\u push()
排队,使用
array\u shift()
出列

默认情况下,关联数组是散列。在PHP中,允许将字符串作为索引,因此这是可以预期的:

$array['key'] = 'value';
最后,您可以用一个可能会浪费空间的数组模拟二叉树。如果你知道你将要有一棵小树,它会很有用。使用一个线性数组,你说对于任何索引(i),你把它的左子元素放在索引(2i+1)上,右子元素放在索引(2i+2)上


关于如何使JavaScript数组模拟更高级别的数据结构,所有这些方法都有很好的介绍。

如果您觉得PHP不包含特定类型的数据结构,您可以创建自己的方法。例如,下面是一个由数组支持的简单集合数据结构

ArraySet:

类数组集
{
/**此集合中的元素*/
私人部分;
/**此集合中的元素数*/
私人$size=0;
/**
*构造此集合。
*/ 
公共函数数组集(){
$this->elements=array();
}
/**
*如果需要,将指定的元素添加到此集合
*它还没有出现。
* 
*@param any$element
*
*@如果指定的元素为,则返回true
*添加到这个集合中。
*/
公共功能添加($element){
如果(!in_数组($element,$this->elements)){
$this->elements[]=$element;
$this->size++;
返回true;
}
返回false;
}
/**
*在指定的列表中添加所有元素
*如果集合尚未存在,则将其添加到此集合。
* 
*@param数组$collection
* 
*@如果
*添加到此集合的指定集合。
*/ 
公共函数addAll($collection){
$changed=false;
foreach($collection作为$element){
如果($this->add($element)){
$changed=true;
}
}
返回$changed;
}
/**
*从该集中删除所有元素。
*/ 
公共功能清除(){
$this->elements=array();
$this->size=0;
}
/**
*检查此集合是否包含指定的元素。
* 
*@param any$element
*
*@如果此集合包含指定的
*元素。
*/ 
公共函数包含($元素){
返回数组($element,$this->elements);
}
/**
*检查此集合是否包含所有指定的
*元素。
* 
*@param数组$collection
* 
*@如果此集合包含所有指定的
*元素。
*/ 
公共功能包含所有($collection){
foreach($collection作为$element){
如果(!in_数组($element,$this->elements)){
返回false;
}
}
返回true;
}
/**
*检查此集合是否包含元素。
* 
*@如果此集合不包含任何元素,则返回true。
*/ 
公共功能是空的{

return count($this->elements)C语言将允许创建一个结构,然后像字符串(char/byte)缓冲区一样填充它。填充后,代码将通过结构成员访问缓冲区。这样解析结构化(数据库、图像等)文件真的很好。我不认为你可以用PHP结构来做这件事,是吗(希望)错了


好的-PHP确实有解包和打包功能-功能相同,但没有那么优雅。

PHP通过标准PHP库(SPL)基本扩展提供数据结构,该扩展在PHP5.0.0中默认可用并编译

PHP 5>=5.3.0提供的数据结构包括:

双链表 双链接列表(DLL)是在两个方向上相互链接的节点列表。当底层结构为DLL时,迭代器的操作、对两端的访问、添加或删除节点的成本为O(1)。因此,它为堆栈和队列提供了一个良好的实现

    • <
      $array['key'] = 'value';
      
      class ArraySet
      {
          /** Elements in this set */
          private $elements;
      
          /** the number of elements in this set */
          private $size = 0;
      
          /**
           * Constructs this set.
           */ 
          public function ArraySet() {
              $this->elements = array();
          }
      
          /**
           * Adds the specified element to this set if 
           * it is not already present.
           * 
           * @param any $element
           *
           * @returns true if the specified element was
           * added to this set.
           */
          public function add($element) {
              if (! in_array($element, $this->elements)) {
                  $this->elements[] = $element;
                  $this->size++;
                  return true;
              }
              return false;
          }
      
          /**
           * Adds all of the elements in the specified 
           * collection to this set if they're not already present.
           * 
           * @param array $collection
           * 
           * @returns true if any of the elements in the
           * specified collection where added to this set. 
           */ 
          public function addAll($collection) {
              $changed = false;
              foreach ($collection as $element) {
                  if ($this->add($element)) {
                      $changed = true;
                  }
              }
              return $changed;
          }
      
          /**
           * Removes all the elements from this set.
           */ 
          public function clear() {
              $this->elements = array();
              $this->size = 0;
          }
      
          /**
           * Checks if this set contains the specified element. 
           * 
           * @param any $element
           *
           * @returns true if this set contains the specified
           * element.
           */ 
          public function contains($element) {
              return in_array($element, $this->elements);
          }
      
          /**
           * Checks if this set contains all the specified 
           * element.
           * 
           * @param array $collection
           * 
           * @returns true if this set contains all the specified
           * element. 
           */ 
          public function containsAll($collection) {
              foreach ($collection as $element) {
                  if (! in_array($element, $this->elements)) {
                      return false;
                  }
              }
              return true;
          }
      
          /**
           * Checks if this set contains elements.
           * 
           * @returns true if this set contains no elements. 
           */ 
          public function isEmpty() {
              return count($this->elements) <= 0;
          }
      
          /**
           * Get's an iterator over the elements in this set.
           * 
           * @returns an iterator over the elements in this set.
           */ 
          public function iterator() {
              return new SimpleIterator($this->elements);
          }
      
          /**
           * Removes the specified element from this set.
           * 
           * @param any $element
           *
           * @returns true if the specified element is removed.
           */ 
          public function remove($element) {
              if (! in_array($element, $this->elements)) return false;
      
              foreach ($this->elements as $k => $v) {
                  if ($element == $v) {
                      unset($this->elements[$k]);
                      $this->size--;
                      return true;
                  }
              }       
          }
      
          /**
           * Removes all the specified elements from this set.
           * 
           * @param array $collection
           *
           * @returns true if all the specified elemensts
           * are removed from this set. 
           */ 
          public function removeAll($collection) {
              $changed = false;
              foreach ($collection as $element) {
                  if ($this->remove($element)) {
                      $changed = true;
                  } 
              }
              return $changed;
          }
      
          /**
           * Retains the elements in this set that are
           * in the specified collection.  If the specified
           * collection is also a set, this method effectively
           * modifies this set into the intersection of 
           * this set and the specified collection.
           * 
           * @param array $collection
           *
           * @returns true if this set changed as a result
           * of the specified collection.
           */ 
          public function retainAll($collection) {
              $changed = false;
              foreach ($this->elements as $k => $v) {
                  if (! in_array($v, $collection)) {
                      unset($this->elements[$k]);
                      $this->size--;
                      $changed = true;
                  }
              }
              return $changed;
          }
      
          /**
           * Returns the number of elements in this set.
           * 
           * @returns the number of elements in this set.
           */ 
          public function size() {
              return $this->size; 
          }
      
          /**
           * Returns an array that contains all the 
           * elements in this set.
           * 
           * @returns an array that contains all the 
           * elements in this set.
           */ 
          public function toArray() {
              $elements = $this->elements;
              return $elements;   
          }
      }
      
      $variable = array(
        'one' => array(1,'char',3),
        'two' => explode("single", "Multiple strings"),
        'three' => all(9,'nine',"nine")
      );
      
      <?php
      $my_array = array("Bird","Cat","Cow");
      
      list($a, $b, $c) = $my_array;
      echo "I have several animals, a $a, a $b and a $c.";
      ?>