不能为n个值创建数组[重复]

时间:2022-02-13 01:49:02

This question already has an answer here:

这个问题已经有了答案:

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

你好,我有一个来自html表单的数组

$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转换为

$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

现在在主代码中,“$job_skill”是一个动态值,可以有任意数量的技能。它的值可以为空,可以有1个技能,2个技能或任意数量的技能。问题是我不能为n个值创建job_skill数组

Can anyone please help me with it

谁能帮我一下吗

2 个解决方案

#1


1  

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

你可以使用php防爆函数将字符串转换为数组:

<?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:

o / p:

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

)

#2


3  

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

如果要将字符串“a、b、c”转换为数组(“a”、“b”、“c”),可以使用PHP函数explosion:

$skills = explode(",", $job_skill);

#1


1  

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

你可以使用php防爆函数将字符串转换为数组:

<?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:

o / p:

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

)

#2


3  

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

如果要将字符串“a、b、c”转换为数组(“a”、“b”、“c”),可以使用PHP函数explosion:

$skills = explode(",", $job_skill);