警告:array_values()期望参数1是数组

时间:2014-09-18 05:19:28

标签: php mysql

我将数据数组插入数据库,如下所示:

$value['gallery_data'] = array();
$value['gallery_data'] = serialize((array(array_values("name"), array_values("url"), array_values("type")))); // LINE 843

在数据库中:

a:3:{i:0;N;i:1;N;i:2;N;}

我在行动中看到了这个错误:

Warning: array_values() expects parameter 1 to be array, string given in C:\xampp\htdocs\user\modules\editnews.php on line 843

Warning: array_values() expects parameter 1 to be array, string given in C:\xampp\htdocs\user\modules\editnews.php on line 843

Warning: array_values() expects parameter 1 to be array, string given in C:\xampp\htdocs\user\modules\editnews.php on line 843

编辑:

我需要在数据库中插入多个数组的格式:

a:3:{i:0;a:4:{i:0;s:48:"uploads/media/galleries/nature/butterfly_001.jpg";i:1;s:43:"uploads/media/galleries/nature/IMG_4181.jpg";i:2;s:43:"uploads/media/galleries/nature/swan_002.jpg";i:3;s:45:"uploads/media/galleries/nature/zzmed copy.jpg";}i:1;a:4:{i:0;s:19:"test_title";i:1;s:20:"test_title1";i:2;s:25:"test_title2";i:3;s:24:"677777777777777777777777";}i:2;a:4:{i:0;s:19:"test_alt";i:1;s:21:"test_alt1";i:2;s:21:"test_alt2";i:3;s:26:"77888888888888888888888888";}}

我该如何解决这个问题?

3 个答案:

答案 0 :(得分:1)

我觉得你在数组之前使用array_values所以尝试这样的事情

$value['gallery_data'] = serialize(array_values(array("key1" => "name", "key2" => "url", "key3" => "type")));

或者像这样

$value['gallery_data'] = serialize(array("name" , "url", "type")); // LINE 843

答案 1 :(得分:1)

此处不需要array_value()

<强>原因


print_r(array("name", "url", "type"));
//output
Array ( [0] => name [1] => url [2] => type )
//output
print_r(array_values(array("name", "url", "type")));
Array ( [0] => name [1] => url [2] => type )

尝试:

$value['gallery_data'] = serialize(array("name", "url", "type")); // LINE 843

更新

WORKING DEMO

在单个数组中传递多个数组,如下所示:

尝试:

$value['gallery_data'] = array();
echo $value['gallery_data'] = serialize(array(
    array(
        "uploads/media/galleries/nature/butterfly_001.jpg", 
        "uploads/media/galleries/nature/IMG_4181.jpg", 
        "uploads/media/galleries/nature/swan_002.jpg",
        "uploads/media/galleries/nature/zzmed copy.jpg",
    ),
    array("test_title", 
          "test_title1", 
          "test_title2",
          "test_title3"
         ),
    array("test_alt",
          "test_alt1",
          "test_alt2",
          "test_alt3"
    )
)); // LINE 843

根据需要输出:

a:3:{i:0;a:4:{i:0;s:48:"uploads/media/galleries/nature/butterfly_001.jpg";i:1;s:43:"uploads/media/galleries/nature/IMG_4181.jpg";i:2;s:43:"uploads/media/galleries/nature/swan_002.jpg";i:3;s:45:"uploads/media/galleries/nature/zzmed copy.jpg";}i:1;a:4:{i:0;s:10:"test_title";i:1;s:11:"test_title1";i:2;s:11:"test_title2";i:3;s:11:"test_title3";}i:2;a:4:{i:0;s:8:"test_alt";i:1;s:9:"test_alt1";i:2;s:9:"test_alt2";i:3;s:9:"test_alt3";}}

答案 2 :(得分:0)

您将字符串传递给array_values函数而不是数组。

例如,将array_values("name")替换为array_values($someArray)

来源:http://php.net/manual/en/function.array-values.php