简体   繁体   中英

combine 2 comma separated list into one in php

I have 2 arrays that are coming from the database, [options] column has comma-separated values.

Array
(
    [0] => Array
        (
            [id] => 143
            [menu_id] => 590
            [name] => bread
            [options] => small, large
        )

    [1] => Array
        (
            [id] => 144
            [menu_id] => 590
            [name] => jam
            [options] => mango, orange, grape
        )

)

Is there have any way to combine the list of [options] at [0] and [1] ?

Example : small, large, mango, orange, grape

Approach

 <?php foreach ($menu_options as $key => $menu_option) : ?>  
     <?PHP $exploded_variant_options = explode(',', $menu_option['options']);
           foreach ($exploded_variant_options as $exploded_variant_option) : ?>
                     <?php echo ucfirst(sanitize($exploded_variant_option)); ?> //print one by one
                             
          <?php endforeach; ?>
  <?php endforeach; ?>

My suggestion would be to use array_merge . The code would look like this:

<?php
    $all_options = [];
    foreach( $menu_options as $key => $menu_option ) {
        $all_options = array_merge($all_options, explode(", ", $menu_option["options"]));
    }
?>

if this is your array:

    $arrays = array (
                array( 'id' => 143, 'menu_id' => 590, 'name' => 'bread', 'options' => 'small,large'),
                array( 'id' => 144, 'menu_id' => 590, 'name' => 'jam', 'options' => 'mango,orange,grape')
         );

you can use array_map for achieving that:

    function spliter($key, $value){
       if($key == 'options')
          return $value =  explode(',', $value);
       else
          return $value;
    }          
    foreach($arrays as &$array)
        $array = array_map( 'spliter' , array_keys($array), $array);

then this will be a dump of your $arrays :

array(2) {
  [0]=>
  array(4) {
    [0]=>
    int(143)
    [1]=>
    int(590)
    [2]=>
    string(5) "bread"
    [3]=>
    array(2) {
      [0]=>
      string(5) "small"
      [1]=>
      string(5) "large"
    }
  }
  [1]=>
  &array(4) {
    [0]=>
    int(144)
    [1]=>
    int(590)
    [2]=>
    string(3) "jam"
    [3]=>
    array(3) {
      [0]=>
      string(5) "mango"
      [1]=>
      string(6) "orange"
      [2]=>
      string(5) "grape"
    }
  }
}

If you just want to print out all the options (insertion order) and not modify them further, you could also just call this:

<?php

$arr = [
    ['id' => 143, 'menu_id' => 590, 'name' => 'bread', 'options' => 'small, large'],
    ['id' => 144, 'menu_id' => 590, 'name' => 'jam', 'options' => 'mango, orange, grape']
];

var_dump(array_reduce(array_column($arr, 'options'), function ($c, $i) {
    return $c ? "${c}, ${i}" : $i;
}));

// prints: string(34) "small, large, mango, orange, grape"

?>

Otherwise, I suggest using the approach shown by "Teodor".

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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