0

我有以下代码行

$return_array = array(
            $count_answers => array(
                    "name" => $domain,
                    "type" => $type,
                    "class" => $class,
                    "ttl" =>$ttl,
                    "data_lenght" => $data_l
                    )
     );

我想preference在数据长度之后添加以下代码

array_push($return_array[$count_answers]['preference'], $preference);

警告:array_push() 期望参数 1 是数组,在第 367 行的 \functions\functions.php 中给出了 null

为什么我的第一个参数不是数组?

4

5 回答 5

5

$return_array因为索引中没有元素'preference'。你可以追加$preference这个

$return_array[$count_answers]['preference'][] = $preference;

或者先用一个空数组初始化

$return_array[$count_answers]['preference'] = array();

如果您不想添加一首选项,而只想添加一个元素'preference',请将其附加

$return_array[$count_answers]['preference'] = $preference;
于 2012-12-13T11:10:30.933 回答
2

不需要使用array_push,直接添加项目即可。

$return_array[$count_answers]['preference'] = $preference;

array_push不允许字符串作为索引,所以你$preference会在$return_array[$count_answers][0]

在您的第 367 行,您没有提供数组,而是当前数组中的一个空元素。

于 2012-12-13T11:09:04.033 回答
1

您应该在下面更正您的代码。

$return_array = array(
        $count_answers => array(
                "name" => $domain,
                "type" => $type,
                "class" => $class,
                "ttl" =>$ttl,
                "data_lenght" => $data_l
                )
 );

$preference['preference'] = "kkk";

只是改变

$return_array[$count_answers]['preference']

$return_array[$count_answers]

在 array_push 中,如下所示

array_push($return_array[$count_answers], $preference);
于 2012-12-13T11:10:00.990 回答
0

将 array_push() 与多维数组一起使用是矛盾的。

PHP 数组是分层的——不是多维的。并且 array_push 添加具有指定值的编号元素。此外,手册中清楚地解释了array_push() 的用法。

我想使用以下代码在数据长度后添加“首选项”

为什么要使用该代码来执行此操作?它失败了,原因应该很明显。

您应该使用的代码是:

$return_array[$count_answers]['preference']=$preference;
于 2012-12-13T11:22:17.727 回答
0
foreach($arr_data_arrays as $key=>$line_arr) { // do an array looping at first

        $new_arr = array(); // create an array to be included on the second position
        $new_arr[0] = $line_arr;

        array_push($arr_data_arrays[$key][1],$new_arr);//include the whole array on the sec position
};

就这么简单!

于 2014-07-18T03:18:55.987 回答