负荷分离器阵列

时间:2018-07-29 16:29:34

标签: php arrays explode

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

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

我想制作一个类似

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

我尝试使用explode,但是结果有麻烦。

explode(";",$exp);

类似于

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

任何人都可以帮助我,如何爆炸这种情况?

4 个答案:

答案 0 :(得分:0)

我个人会说继续进行,然后将数组放入结果中,遍历数组中的每个项目并像这样存储它

$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"
  }
}

答案 1 :(得分:0)

$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']);

答案 2 :(得分:0)

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

guard error == nil else { print("get heart rate error");  dispatchGroup.leave() return ; }

guard let unwrappedResults = results as? [HKQuantitySample] else { print("get heart rate error"); dispatchGroup.leave(); return}

let heartRatesAsDouble = unwrappedResults.map {$0.quantity.doubleValue(for: heartRateUnit)}

guard let max = heartRatesAsDouble.max() else { dispatchGroup.leave();  return }

let maxAsCustomHistoricalSample = CustomHistoricalSample(value: max, date: workout.startDate)

heartRateMaxArrayAsCustomHistoricalSample.append(maxAsCustomHistoricalSample)

let average = heartRatesAsDouble.average

let averageAsCustomHistoricalSample = CustomHistoricalSample(value: average, date: workout.startDate)

dispatchGroup.leave()

heartRateAvgArrayAsCustomHistoricalSample.append(averageAsCustomHistoricalSample)

https://3v4l.org/cREn7

答案 3 :(得分:0)

实际上,有很多方法可以做您想要的事情,但是如果我是您,那么我会尝试这种方法:)

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