Packy Packy - 2 months ago 14
Javascript Question

jquery check if inArray from php array

I have an array in php I pull into my jquery like so:

var hide = <?php echo json_encode($user_dashboard); ?>;


this give me:

var hide = "a:2:{i:0;s:18:\"dashboard-progress\";i:1;s:19:\"dashboard-bookmarks\";}";


I had some code that working fine when the array was just a string but now it doesnt compare the elements:

if ( $.inArray( $this.attr('id') , hide) != -1 )


basically its just testing to group of elements ID's against the array. If the array contains the ID then doing something. When the array was just:

var hide = "dashboard-progress", "dashboard-bookmarks";


It worked fine. But now I have to store the array in php and MySQL.

var_dump($user_dashboard)
contains
string(67) "a:2:{i:0;s:18:"dashboard-progress";i:1;s:19:"dashboard-bookmarks";}"


The data is pulled from an array being put into MySQL field using php serialize.

Answer

Yea the string you have in $user_dashboard is a serialized array, so try this code instead, to first convert that string into an array and then convert it to a JSON String

var hide = <?php echo json_encode(unserialize($user_dashboard)); ?>;
Comments