【问题标题】:Expload Separator Array爆炸分隔符阵列
【发布时间】:2018-07-29 16:29:34
【问题描述】:

我有一个来自数据库的代码,例如

$exp = ukuran:33,34,35;warna:putih,hitam;

我想做一个像

这样的数组
$ukuran = array("33", "34", "35");
$warna = array("putih","hitam");

我曾尝试使用爆炸,但结果有问题。

explode(";",$exp);

结果像

 Array
(
    [0] => ukuran:33,34,35
    [1] => warna:putih,hitam
    [2] => 
)

谁能帮帮我,请问这个箱子怎么炸?

【问题讨论】:

  • 你的数据库标准化了吗?像这样保存数据听起来像是将数据塞进应该有单独表格的字段中。

标签: php arrays explode


【解决方案1】:
$string = 'ukuran:33,34,35;warna:putih,hitam;';
$string = str_replace(['ukuran:', 'warna:'], '', $string);
$exploded = explode(';', $string);
$ukuran = explode(',', $exploded[0]);
$warna = explode(',', $exploded[1]);

如果你想动态地做,你不能创建变量,但你可以创建一个以类型为键的数组:

$string = 'ukuran:33,34,35;warna:putih,hitam;';
$exploded = explode(';', $string);

$keysAndValues = [];

foreach($exploded as $exp) {
    if (strlen($exp) > 0) {
        $key = substr($exp, 0, strpos($exp, ':' ) );
        $values = substr($exp, strpos($exp, ':' ) + 1, strlen($exp) );
        $values = explode(',', $values);
        $keysAndValues[$key] = $values;
    }

}

这将输出:

array (size=2)
  'ukuran' => 
    array (size=3)
      0 => string '33' (length=2)
      1 => string '34' (length=2)
      2 => string '35' (length=2)
  'warna' => 
    array (size=2)
      0 => string 'putih' (length=5)
      1 => string 'hitam' (length=5)

这样称呼他们:

var_dump($keysAndValues['ukuran']);
var_dump($keysAndValues['warna']);

【讨论】:

  • 谢谢先生,这就像我想要的,但是如果像size:33,34,35;color:putih,hitam;material:linen,cotton;之前的自动检查变体这样的字符串是动态的
【解决方案2】:

我个人会说像你一样继续,然后在你的结果中使用你的数组,循环遍历数组中的每个项目并像这样存储它

$string = 'ukuran:33,34,35;warna:putih,hitam;';
$exploded = explode(';',$string);
$master = [];

foreach($exploded as $key => $foo){
    $namedKey = explode(':',$foo);
    $bar = substr($foo, strpos($foo, ":") + 1); //Get everything after the ; character
    $master[$namedKey[0]] = explode(",",$bar);
}

这应该返回一个类似于

的结果
array(3) {
  ["ukuran"]=>
  array(3) {
    [0]=>
    string(2) "33"
    [1]=>
    string(2) "34"
    [2]=>
    string(2) "35"
  }
  ["warna"]=>
  array(2) {
    [0]=>
    string(5) "putih"
    [1]=>
    string(5) "hitam"
  }
}

【讨论】:

  • ukuran:33,34,35;warna:putih,hitam 在字符串上,但我喜欢你的代码先生,如此动态。
  • @AndyAjhisRamadhan 谢谢!如果全部在一行,请先用您的代码explode(";",$exp); 将其分解,然后将结果存储在变量中,然后运行我的代码?
  • 使用 : 之前的位作为数据的索引会有用吗?所以你有ukuran =>[ 33, 34, 35], warna => ['putih'...
  • 当然谢谢先生,但我是如何得到ukuranwarna 的名称的,因为它们是动态名称。
  • 非常感谢@S_R 先生。 :)
【解决方案3】:

您可以使用正则表达式来分别匹配单词和数字。
然后使用 array_filter 删除空值。

$exp = "ukuran:33,34,35;warna:putih,hitam";

Preg_match_all("/(\d+)|([a-zA-Z]+)/", $exp, $matches);

Unset($matches[0]);
$matches[1] = array_filter($matches[1]);
$matches[2] = array_filter($matches[2]);
Var_dump($matches);

https://3v4l.org/cREn7

【讨论】:

    【解决方案4】:

    其实有很多方法可以做你想做的事,但如果我是你,我会尝试这种方式:)

    <?php
    $exp = 'ukuran:33,34,35;warna:putih,hitam';
    $result = explode(';',$exp);
    foreach($result as $k=>$v){
        $key_value = explode(':',$v);
        // this line will help your to treat your $ukuran and $warna as array variable
        ${$key_value[0]} = explode(',',$key_value[1]); 
    }
    print '<pre>';
    print_r($ukuran);
    print_r($warna);
    print '</pre>';
    ?>
    

    演示: https://3v4l.org/BAaCT

    【讨论】:

      猜你喜欢
      • 2011-02-21
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2021-11-09
      • 2011-06-24
      • 2018-05-22
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多