PHP:将逗号分隔列表转换为多维数组

时间:2022-09-02 00:21:26

I have a columns string (comma separated list of columns) as such:

我有一个列字符串(逗号分隔的列列表),如下所示:

$columns = "project,description,due_date,tags";

Convert into this:

转换成这个:

$columns = array(
  array( 'db' => 'project',  'dt' => 1 ),
  array( 'db' => 'description',  'dt' => 2 ),
  array( 'db' => 'due_date',  'dt' => 3 ),
  array( 'db' => 'tags',  'dt' => 4 )
);

7 个解决方案

#1


4  

Try this

尝试这个

    <?php
    $columns = "project,description,due_date,tags";
    $columns = explode(",", $columns);
    $cArray = array();
    foreach ($columns as $key => $value) {
        $cArray[$key]['db'] = $value;
          $cArray[$key]['dt'] = $key+1; 
    }
    echo '<pre>'; print_r($cArray); echo '</pre>';

Output:-

输出: -

Array
(
    [0] => Array
        (
            [db] => project
            [dt] => 1
        )

    [1] => Array
        (
            [db] => description
            [dt] => 2
        )

    [2] => Array
        (
            [db] => due_date
            [dt] => 3
        )

    [3] => Array
        (
            [db] => tags
            [dt] => 4
        )

)

?>

#2


1  

$columns = "project,description,due_date,tags";

$col_Arr = explode(',',$columns);
$i=1;
$new_arr = [];
foreach($col_Arr as $val){
    $new_arr[] = array('db'=>$val,'dt'=>$i);
    $i++;
}
echo "<pre>"; print_r($new_arr); echo "</pre>";

#3


1  

You can do it like this :

你可以这样做:

$arr= explode(",", $columns );
$result = [];
for(int i =0; i<arr.length; i++)
  $result[] = ['db' => $arr[i], 'dt' => i+1];

#4


1  

You can try like this

你可以这样试试

$columns = "project,description,due_date,tags";
$explode_arr = explode(',', $columns);
$arrayval = "";
for($i=0; $i< count($explode_arr); $i++)
{
  $arrayval[$i]['db'] = $explode_arr[$i];
  $arrayval[$i]['dt'] = ($i+1);
}
print_r($arrayval);
exit;

#5


0  

You can split the string into a one-dimensional array with split or explode and then iterate over it with foreach.

您可以将字符串拆分为具有拆分或爆炸的一维数组,然后使用foreach迭代它。

#6


0  

First you need to convert the string to an array :

首先,您需要将字符串转换为数组:

$columns = "project,description,due_date,tags";
$columns_array = explode(',', $columns);
// then you need to build your multi-dimensional array
$result = array();
foreach ($columns_array as $key => $item) {
    $result[] = array(
        'db' => $item,
        'dt' => $key+1
    );
}
var_dump($result);

#7


0  

$columns = "project,description,due_date,tags";
$myArray = explode(',', $columns);
$len=sizeof($myArray);
for($i=0;$i<$len;$i++){
    $newArr[] = array('db'=>$myArray[$i],'dt'=>$i);
}
 echo '<pre>'; print_r($newArr); echo '</pre>';

Working PHP Fiddle

工作PHP小提琴

#1


4  

Try this

尝试这个

    <?php
    $columns = "project,description,due_date,tags";
    $columns = explode(",", $columns);
    $cArray = array();
    foreach ($columns as $key => $value) {
        $cArray[$key]['db'] = $value;
          $cArray[$key]['dt'] = $key+1; 
    }
    echo '<pre>'; print_r($cArray); echo '</pre>';

Output:-

输出: -

Array
(
    [0] => Array
        (
            [db] => project
            [dt] => 1
        )

    [1] => Array
        (
            [db] => description
            [dt] => 2
        )

    [2] => Array
        (
            [db] => due_date
            [dt] => 3
        )

    [3] => Array
        (
            [db] => tags
            [dt] => 4
        )

)

?>

#2


1  

$columns = "project,description,due_date,tags";

$col_Arr = explode(',',$columns);
$i=1;
$new_arr = [];
foreach($col_Arr as $val){
    $new_arr[] = array('db'=>$val,'dt'=>$i);
    $i++;
}
echo "<pre>"; print_r($new_arr); echo "</pre>";

#3


1  

You can do it like this :

你可以这样做:

$arr= explode(",", $columns );
$result = [];
for(int i =0; i<arr.length; i++)
  $result[] = ['db' => $arr[i], 'dt' => i+1];

#4


1  

You can try like this

你可以这样试试

$columns = "project,description,due_date,tags";
$explode_arr = explode(',', $columns);
$arrayval = "";
for($i=0; $i< count($explode_arr); $i++)
{
  $arrayval[$i]['db'] = $explode_arr[$i];
  $arrayval[$i]['dt'] = ($i+1);
}
print_r($arrayval);
exit;

#5


0  

You can split the string into a one-dimensional array with split or explode and then iterate over it with foreach.

您可以将字符串拆分为具有拆分或爆炸的一维数组,然后使用foreach迭代它。

#6


0  

First you need to convert the string to an array :

首先,您需要将字符串转换为数组:

$columns = "project,description,due_date,tags";
$columns_array = explode(',', $columns);
// then you need to build your multi-dimensional array
$result = array();
foreach ($columns_array as $key => $item) {
    $result[] = array(
        'db' => $item,
        'dt' => $key+1
    );
}
var_dump($result);

#7


0  

$columns = "project,description,due_date,tags";
$myArray = explode(',', $columns);
$len=sizeof($myArray);
for($i=0;$i<$len;$i++){
    $newArr[] = array('db'=>$myArray[$i],'dt'=>$i);
}
 echo '<pre>'; print_r($newArr); echo '</pre>';

Working PHP Fiddle

工作PHP小提琴