1

请帮帮我。我想将新数据附加到多维数组,但以与原始数据相同的方式构造它。这是我现在拥有的:

<?php
session_start();
$ses = session_id();
if (isset($_POST['title'])){
if(!empty($_SESSION[$ses])){
    $title = $_POST['title'];
    array_push($_SESSION[$ses], $title);
    array_push($_SESSION[$ses], $_POST['desc']);
}else{
    $title = $_POST['title'];
    $_SESSION[$ses] = array(array($title, $_POST['desc']));
}
}
?>

现在输出如下所示:

Array
    (
    [0] => Array
                (
        [0] => 1
        [1] => 2
                )

        [1] => 3
        [2] => 4
)

但我希望 3 和 4 像这样附加:

Array
    (
    [0] => Array
                (
        [0] => 1
        [1] => 2
                )
    [1] => Array
               (
        [1] => 3
        [2] => 4
      )
)

我怎样才能修改我的代码来实现这一点?我真的卡住了,求救!!!

4

2 回答 2

1

您将每个值、标题和描述作为单独的对象推送到数组中。实例化一个附加数组,在该对象中设置标题和描述,并将该数组推送到您的父级。

<?php
session_start();
$ses = session_id();
if (isset($_POST['title'])){
    if(!empty($_SESSION[$ses])){
        array_push($_SESSION[$ses], array($_POST['title'], $_POST['desc']));
    }else{
        $title = $_POST['title'];
        $_SESSION[$ses] = array(array($title, $_POST['desc'])); // <--- As you had done     here
}

} ?>

于 2013-05-04T00:11:34.543 回答
0

这一定足够了:

session_start();
$ses = session_id();
if (isset($_POST['title'])){
    $_SESSION[$ses][] = array($_POST['title'], $_POST['desc']);
}
于 2013-05-04T00:11:09.717 回答