2

フォームがあり、URLは次のようになっています。

http://hostname/projectname/classname/methodname/variablename

そして、私のJavaScriptでは、次のような配列を入力します。

var currentrelations = new Array();
    $(".ioAddRelation").each(function(index) {
        currentrelations[index] = $(this).html();
    });

そして、この配列には2つの値があります['eeeeee','eeeeee']

したがって、URLは次のとおりです。

http://localhost/Mar7ba/InformationObject/addIO/eeeeee,eeeeee

そして私のPHPでは、クラスInformationObjectで、メソッドaddIOで:

public function addIO($currentRelations =null) {
        $name = $_POST['name'];
        $type = $_POST['type'];
        $concept = $_POST['concept'];
        $contents = $_POST['contents'];
        $this->model->addIO($name, $type, $concept, $contents);
        if (isset($_POST['otherIOs'])) {
            $otherIOs = $_POST['otherIOs'];
            $this->model->addOtherIOs($name, $otherIOs);
        }
        $NumArguments = func_num_args();
        if ($currentRelations!=null) {
            $IOs = $_POST['concetedIOs'];
            $this->model->setIoRelations($name,$IOs, $currentRelations);
        }
        exit;
        include_once 'Successful.php';
        $s = new Successful();
        $s->index("you add the io good");
}

しかし、$currentRelationsこのステートメントを使用して配列を出力すると、次のようになります。

echo count($currentRelations)

結果はでした。この1 not 2ステートメントecho $currentRelations[0]を使用して最初の要素を印刷すると、次のようになります。e not eeeeee

何故ですか?解決策は何ですか?私は何が間違っているのですか?

4

1 に答える 1

2

コメントしたように、$currentRalationsこれは文字列であるためcount、配列またはオブジェクトではない任意の型で使用すると1が返されます。また、文字列でこれを行うと、のゼロベースのインデックスの文字にアクセスすること
に注意してください。$currentRelations[0]文字列。文字列は文字の配列であるため、角かっこを使用して文字列内の特定の文字にアクセスできます。echo $currentRelations[0];これが、コードに出力される理由eです。

文字列を分割するには、次のexplodeような関数 を使用する必要があります。

$curRel = explode(',', $currentRelations);

そして、あなたが得るものを見てください

var_dump($curRel);

それが役に立てば幸い。

于 2012-05-20T20:18:55.457 に答える