質問

私はそれを見ています PHPマニュアル, 、リストやセットなど、ほとんどの言語にあるデータ構造に関するセクションが表示されません。私が盲目なだけなのでしょうか、それとも PHP にはそのような機能が組み込まれていないのでしょうか?

役に立ちましたか?

解決

PHP の唯一のネイティブ データ構造は配列です。幸いなことに、配列は非常に柔軟であり、ハッシュ テーブルとしても使用できます。

http://www.php.net/array

ただし、C++ STL のクローンのような SPL があります。

http://www.php.net/manual/en/book.spl.php

他のヒント

PHP は、標準 PHP ライブラリ (SPL) 基本拡張機能を介してデータ構造を提供します。この拡張機能は、PHP 5.0.0 でデフォルトで利用およびコンパイルされます。

提供されるデータ構造は PHP 5 以降で使用でき、次のものが含まれます。

二重リンクリスト

二重リンク リスト (DLL) は、相互に両方向にリンクされたノードのリストです。基礎となる構造が DLL の場合、イテレータの操作、両端へのアクセス、ノードの追加または削除には O(1) のコストがかかります。したがって、スタックとキューに適切な実装が提供されます。

山盛り

ヒープは、ヒープ プロパティに従うツリー状の構造です。ヒープに対してグローバルな実装された比較メソッドを使用して比較すると、各ノードはその子以上になります。

配列

配列はデータを連続的に格納する構造であり、インデックスを介してアクセスできます。これらを PHP 配列と混同しないでください。実際、PHP 配列は順序付けされたハッシュテーブルとして実装されます。

地図

マップは、キーと値のペアを保持するデータ構造です。PHP 配列は、整数/文字列から値へのマップとして見ることができます。SPL は、オブジェクトからデータへのマップを提供します。このマップはオブジェクト セットとしても使用できます。

ソース: http://php.net/manual/en/spl.datastructs.php

連想配列は、ハッシュテーブル、キュー、スタックなどのほとんどの基本的なデータ構造に使用できます。ただし、ツリーやヒープのようなものが必要な場合は、デフォルトでは存在しないと思いますが、無料のライブラリがどこにでもあると思います。

配列にスタックをエミュレートさせるには、次を使用します array_push() 追加して array_pop() 離陸する

配列にキューをエミュレートさせるには、次を使用します array_push() エンキューし、 array_shift() デキューする

連想配列はデフォルトではハッシュです。PHP では文字列をインデックスとして使用できるため、これは期待どおりに機能します。

$array['key'] = 'value';

最後に、スペースを無駄にする可能性のある配列を使用してバイナリ ツリーをエミュレートすることができます。小さな木を植えることがわかっている場合に便利です。線形配列を使用すると、任意のインデックス (i) について、その左の子をインデックス (2i+1) に配置し、右の子をインデックス (2i+2) に配置すると言います。

これらのメソッドはすべて、以下で詳しく説明されています この記事 JavaScript 配列で高レベルのデータ構造をエミュレートする方法について説明します。

この質問は 8 年前のものですが、PHP 7 では と呼ばれる拡張機能が導入されているため、回答を投稿しています。 ds 配列の代替として特殊なデータ構造を提供します。

ds,

  • を使用します Ds\ 名前空間。
  • 3 つのインターフェイスがあります。Collection, Sequence そして Hashable
  • 8つのクラスがあります。 Vector, Deque,Queue, PriorityQueue, Map, Set, Stack, 、 そして Pair

詳細については、こちらをチェックしてください マニュアル そしてまた このブログ投稿 にはベンチマークなどの素晴らしい情報が含まれています。

PHP には実際には連想配列であり、セットとしても使用できる配列があります。多くのインタープリタ型言語と同様、PHP はさまざまな明示的なデータ型を提供するのではなく、これらすべてを 1 つの内部で提供します。

例えば。

$lst = array(1, 2, 3);
$hsh = array(1 => "This", 2 => "is a", 3 => "test");

/編集:また、見てください マニュアルの中で.

PHPの 配列 リストと辞書の両方の役割を果たします。

$myArray = array("Apples", "Oranges", "Pears");
$myScalar = $myArray[0] // == "Apples"

または、連想配列として使用するには、次のようにします。

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

データ構造というと、いくつかの方向に考えが行きますが、もう少し具体的にしたほうがよいでしょう...

配列 - 確かに十分に文書化されており、入手可能です。(http://us.php.net/manual/en/book.array.php)

SQL データ - 使用しているデータベースによって異なりますが、ほとんどのデータが利用可能です。(http://us.php.net/manual/en/book.mysql.php)

OOP - バージョンに応じて、オブジェクトを設計および実装できます。(http://us.php.net/manual/en/ language.oop.php) PHP サイトでこれを見つけるには、OOP を検索する必要がありました。

お役に立てば幸いですが、そうでない場合はごめんなさい。

もちろん、PHP にはデータ構造があります。php の配列は非常に柔軟です。いくつかの例:

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

そうすれば、構造のマップ/フィルター/ウォークなどを行ったり、変換、反転、反転などを行うために利用できる配列関数が大量にあります。

PHP に特定のタイプのデータ構造が含まれていないと思われる場合は、いつでも独自のデータ構造を作成できます。たとえば、ここでは配列を基にした単純な Set データ構造を示します。

配列セット: https://github.com/abelperez/collections/blob/master/ArraySet.php

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;   
    }
}

PHP には、 配列の配列 これは「多次元配列」または「行列」と呼ばれます。2 次元配列、3 次元配列などを使用できます。

データ構造の必須の必要性については、以下を参照してください。 SPL(PHP 拡張機能)。それらはヒープ、リンクリストなどのデータ構造を持っています。等...

PHP には正確なデータ構造のリストと設定がありません。ただし、単一のクラスターで複数のデータを提供する配列(n次元)によって実現できます。

$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.";
?>

http://www.w3schools.com/php/func_array_list.asp

C 言語では、構造体を作成し、それを文字列 (文字/バイト) バッファーのように埋めることができます。バッファーがいっぱいになると、コードは構造体のメンバーを介してバッファーにアクセスします。この方法で構造化された (データベース、画像など) ファイルを解析できるのは非常に便利です。PHP 構造ではそれができないと思います。それとも、私が間違っているのでしょうか。

わかりました。PHP にはアンパックとパックがあります。機能的には同じですが、それほどエレガントではありません。

ライセンス: CC-BY-SA帰属
所属していません StackOverflow
scroll top