简体   繁体   English

PHP 关联数组 - 将逗号分隔值拆分为单独的数组行

[英]PHP Associative Array - Split comma separated value into separate array rows

I have an associative array, with some of the values in "CAT" having multiple words, separated by a comma.我有一个关联数组,“CAT”中的一些值有多个单词,用逗号分隔。

I would like to split those into separate array entries (each comma separated word to create another array entry) as shown below.我想将它们拆分为单独的数组条目(每个逗号分隔的单词以创建另一个数组条目),如下所示。

I know that I can use explode() to separate those values with commas, but not sure on how to create a new array entry from it?我知道我可以使用explode()用逗号分隔这些值,但不确定如何从中创建新的数组条目?

$categories = explode(',', $details[0]['CAT']);

Current array structure:当前数组结构:

Array
(
    [0] => Array
        (
            [ID] => 50829
            [CAT] => "furniture,home,garden"
        )

    [1] => Array
        (
            [ID] => 50832
            [CAT] => "kids"
        )

    [2] => Array
        (
            [ID] => 50854
            [CAT] => "toys"
        )
)

Desired result:期望的结果:

Array
(
    [0] => Array
        (
            [ID] => 50829
            [CAT] => "furniture"
        )

    [1] => Array
        (
            [ID] => 50829
            [CAT] => "home"
        )

    [2] => Array
        (
            [ID] => 50829
            [CAT] => "garden"
        )

    [3] => Array
        (
            [ID] => 50832
            [CAT] => "kids"
        )

    [4] => Array
        (
            [ID] => 50854
            [CAT] => "toys"
        )
)

Really appreciate your help!非常感谢您的帮助!

While there are always very cryptic tricks you can use, it is often very easy to do this work exactly as you describe you want it done.尽管您总是可以使用非常神秘的技巧,但通常很容易完全按照您描述的方式完成这项工作。 You want to explode the CAT index and create a new array with each value tied to the original index.您想要分解 CAT 索引并创建一个新数组,其中每个值都与原始索引相关联。 So, do exactly that:所以,请这样做:

// Assume $old_array is your original array.
$new_array = array(); // Originally empty.
foreach($old_array as $e) { // For each element in the old array...
  $cats = explode(',',$e['CAT']);
  // $cats is every word exploded (one word if there is no comma).
  foreach($cats as $cat) { // For each word in $cats...
    $new_array[] = array('ID'=>$e['ID'], 'CAT'=>$cat);
    // Add a pair to the new array that has the ID and the cat word.
  }
}

When done, $new_array is the exploded version that you asked for.完成后,$new_array 就是您要求的分解版本。

You can simply use a foreach to recreate a new array.您可以简单地使用 foreach 重新创建一个新数组。 For each category in the "CSV string", you can add the "ID"+"new CAT" in the output array:对于“CSV 字符串”中的每个类别,您可以在 output 数组中添加“ID”+“新 CAT”:

$array = [
    ['ID' => 50829, 'CAT' => 'furniture,home,garden'],
    ['ID' => 50832, 'CAT' => 'kids'],
    ['ID' => 50854, 'CAT' => 'toys'],
];

$out = [];                              // output array
foreach ($array as $item) {             // for each entry of the array
    $cats = explode(',', $item['CAT']); // split the categories
    foreach ($cats as $cat) {           // for each cat
        $out[] = [                      // create a new entry in output array
           'ID'  => $item['ID'],        // with same ID
           'CAT' => $cat,               // the "sub" category
        ];
    }
}

var_export(array_values($out));
array (
  0 => 
  array (
    'ID' => 50829,
    'CAT' => 'furniture',
  ),
  1 => 
  array (
    'ID' => 50829,
    'CAT' => 'home',
  ),
  2 => 
  array (
    'ID' => 50829,
    'CAT' => 'garden',
  ),
  3 => 
  array (
    'ID' => 50832,
    'CAT' => 'kids',
  ),
  4 => 
  array (
    'ID' => 50854,
    'CAT' => 'toys',
  ),
)

Or to create a same array without "CAT":或者创建一个没有“CAT”的相同数组:

$out = [];
foreach ($array as $item) {
    $cats = explode(',', $item['CAT']);
    unset($item['CAT']);                  // unset the original "CAT"
    foreach ($cats as $cat) {
        $out[] = $item + ['CAT' => $cat]; // copy $item and add "CAT"
    }
}

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM