1

I insert data array into database like this :

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

In database:

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

I see this error in action:

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

EDIT:

I need to insert this format for multiple array in database:

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

how do can i fix this ?

4
  • 3
    Warning clearly says array_values() expects parameter 1 to be array Commented Sep 18, 2014 at 5:21
  • you need to do unserialize before passing this array in array_values() function. Commented Sep 18, 2014 at 5:22
  • Please read the documentation for the array_values() function - you are using it incorrectly. Commented Sep 18, 2014 at 5:23
  • hey it is a json encode string . Commented Sep 18, 2014 at 5:43

3 Answers 3

1

I feel you are using array_values before array so try something like this

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

or like this

$value['gallery_data'] = serialize(array("name" , "url", "type")); // LINE 843
Sign up to request clarification or add additional context in comments.

2 Comments

I didn't get your point? If you want to insert it in db than you can insert it in db and un-serialize it after your retrieve it. if you are looking for something different than describe it clearly
No need of array_value() here
1

No need of array_value() here

Reason


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 )

Try:

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

Updates

WORKING DEMO

Passing multiple array in single array like this:

Try:

$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

Output as you want:

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 Comments

Want to see your coee @user27133
Your Code Work for multiple data array?! i.e: 5 image_url , 5 title, 5 alt.
0

You're passing a string to the array_values function instead of an array.

So replace array_values("name") with array_values($someArray) for example.

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

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.