2

Hi i have the following array that comes from a html form

$job_title = 'Developer';
$job_skill = 'html,css,javascript';


$post_fields = array(
    'job_title' => $job_title,
    'skills' => $job_skill
);

echo "<pre>";
print_r($post_fields);
echo "</pre>";

that gives the output as

Array
(
    [job_title] => Developer
    [skills] => html,css,javascript
)

I wanted to convert the skills to an array itself so i converted the $post_fields to

$post_fields = array(
    'job_title' => $job_title,
    'skills' => 
        array (
            0 => 'html',
            1 => 'css',
            2 => 'javascript'
        )
);

Now in the main code, "$job_skill" is a dynamic value and can have any number of skills. It's value can be null, can have 1 skill, 2 skill or any number of skill. The problem is that i am not able to create array of job_skill for 'n' number of values

Can anyone please help me with it

sammy
  • 201
  • 2
  • 9

2 Answers2

3

If you want to convert a string "a,b,c" to an array ("a", "b", "c") you can use the PHP function explode:

$skills = explode(",", $job_skill);
m13r
  • 1,853
  • 2
  • 22
  • 33
1

You can use php explode function which will convert string to array:

<?php
$job_title = 'Developer';
$job_skill = 'html,css,javascript';


$post_fields = array(
    'job_title' => $job_title,
    'skills' => explode(",",$job_skill)
);

echo "<pre>";
print_r($post_fields);
echo "</pre>";

o/p:

Array
(
    [job_title] => Developer
    [skills] => Array
        (
            [0] => html
            [1] => css
            [2] => javascript
        )

)
B. Desai
  • 16,092
  • 5
  • 22
  • 43