0

Hi I have an array using a code below

var selectedValues=new Array();
selectedValues=divisi.split(",");

and the result is

ITD, ITO, Keuangan, Sumber Daya Manusia

But I want to have a result like

"ITD", "ITO", "Keuangan", "Sumber Daya Manusia"

Do you know how to make it like that? thankyou

hendraspt
  • 959
  • 3
  • 19
  • 42

2 Answers2

0

The split would return an array of strings itself, if that is the question. http://php.net/manual/en/function.split.php

Edit: Adding a more detailed answer:

<?php

$divisi = "ITD, ITO, Keuangan, Sumber Daya Manusia";
$selectedValues = array();
$selectedValues = split(",", $divisi);
var_dump($selectedValues)

?>

The script above returns the below array when run on http://phpfiddle.org/

array(4) { [0]=> string(3) "ITD" [1]=> string(4) " ITO" [2]=> string(9) " Keuangan" [3]=> string(20) " Sumber Daya Manusia" }

As pointed out split is deprecated: http://php.net/manual/en/function.split.php. Please use explode or str_split

Also your code looks like it is JavaScript and not proper PHP. So take care of that.

Ankit Gupta
  • 179
  • 1
  • 9
0

Try this: Working example

var divisi = ['ITD', 'ITO', 'Keuangan', 'Sumber Daya Manusia']; //you can use user defined array or direct variable divisi
console.log('"'+divisi.join('\",\"')+'"'); //"ITD","ITO","Keuangan","Sumber Daya Manusia"
  • I am adding `var arr = [divisi]; a=console.log('"'+arr.join('\",\"')+'"'); alert(a);` but it's undefine and just show "ITD, ITO, Keuangan, Sumber Daya Manusia" in console – hendraspt Jun 02 '16 at 07:32
  • you need to use `var arr = divisi;` –  Jun 02 '16 at 07:38
  • I've got an error "Uncaught TypeError: arr.join is not a function" if I use `var arr = divisi;` – hendraspt Jun 02 '16 at 07:42
  • if `divisi` is a string then you need convert it into array `selectedValues=divisi.split(",");`. –  Jun 02 '16 at 07:44
  • are these correct? (My `ITD, ITO, Keuangan, Sumber Daya Manusia` is defined as `divisi`) `var selectedValues=new Array(); selectedValues=divisi.split(","); var arr = divisi; a=console.log('"'+arr.join('\",\"')+'"'); alert(a);` – hendraspt Jun 02 '16 at 07:48
  • you can not store console.log in a variable, you can check with inspect of the browser and console tab. –  Jun 02 '16 at 07:52
  • or, `var selectedValues=new Array(); selectedValues=divisi.split(","); var arr = divisi; alert('"'+arr.join('\",\"')+'"');` –  Jun 02 '16 at 07:52
  • Uncaught TypeError: arr.join is not a function – hendraspt Jun 02 '16 at 07:56