2017-08-11 83 views
1

我有一個數組是這樣的:如何在兩個數組分割字符串在Php

$array='{b_price,9500,b_discount,10,mainPrice,95000,total,95000,title,obj1},{b_price,1500,b_discount,15,mainPrice,15000,total,22500,title,obj2}' 

我想先拆分兩個數組是這樣的:

$array[0]={b_price,9500,b_discount,10,mainPrice,95000,total,95000,title,obj1} 

而且

$array[1]={b_price,1500,b_discount,15,mainPrice,15000,total,22500,title,obj2} 

我用此代碼更改每個陣列

foreach ($b as $k => $m) { 

        if ($k % 2 == 0) { 
         $even[]= $m; 
        } 
        else { 
         $odd[] = $m; 
        } 
       } 

    $ff=array_combine($even,$odd); 

我想這樣

Array(Array[0] => ([b_price] => 9500 [b_discount] => 10 [mainPrice] => 95000 [total] => 95000 [title] =>obj1) 
Array[1] => ([b_price] => 1500 [b_discount] => 15 [mainPrice] => 15000 [total] => 22500 [title] => obj2)) 
+0

首先添加{B_PRICE之前,然後用爆炸它,因爲分隔符 –

+0

它不是一個數組,基本上它是一個字符串,$陣列='{B_PRICE,9500,b_discount,10,mainPrice,95000 ,總數,95000,title,obj1},{b_price,1500,b_discount,15,mainPrice,15000,total,22500,title,obj2}' –

+0

我用這段代碼展開了第一個數組$ b1 = explode(「{」,$數組); – Saeidhp

回答

2

兩種方法:

- 使用explodearray_map功能:

$str = '{b_price,9500,b_discount,10,mainPrice,95000,total,95000,title,obj1},{b_price,1500,b_discount,15,mainPrice,15000,total,22500,title,obj2}'; 

$result = array_map(function($v){ 
    $r = []; 
    $arr = explode(',', trim($v, '{}')); 
    foreach ($arr as $k => $v) { 
     if (!($k % 2)) $r[$v] = $arr[$k+1]; 
    } 
    return $r; 
}, explode('},{', $str)); 

print_r($result); 

- 使用附加preg_match_allarray_combine功能:

$str = '{b_price,9500,b_discount,10,mainPrice,95000,total,95000,title,obj1},{b_price,1500,b_discount,15,mainPrice,15000,total,22500,title,obj2}'; 

$result = array_map(function($v){ 
    preg_match_all('/([^,]+),([^,]+),?/', trim($v, '{}'), $m); 
    return array_combine($m[1], $m[2]); 
}, explode('},{', $str)); 

print_r($result); 

輸出:

Array 
(
    [0] => Array 
     (
      [b_price] => 9500 
      [b_discount] => 10 
      [mainPrice] => 95000 
      [total] => 95000 
      [title] => obj1 
     ) 

    [1] => Array 
     (
      [b_price] => 1500 
      [b_discount] => 15 
      [mainPrice] => 15000 
      [total] => 22500 
      [title] => obj2 
     ) 
) 
+0

非常感謝。 – Saeidhp

+0

@Saeidhp,不客氣 – RomanPerekhrest

+0

@Saeidhp,你也可以觀察我的附加做法(較短的一種) – RomanPerekhrest

0

輸出變化,你應該改變你的針,在你的陣列的字符串, 我有分號改變了它,

$arrayString='{b_price,9500,b_discount,10,mainPrice,95000,total,95000,title,obj1};{b_price,1500,b_discount,15,mainPrice,15000,total,22500,title,obj2}'; 
    echo $arrayString; 
    echo "<pre>"; print_r (explode(";",$arrayString)); 
$b=explode(";",$arrayString); 
foreach ($b as $k => $m) { 

        if ($k % 2 == 0) { 
         $even[]= $m; 
        } 
        else { 
         $odd[] = $m; 
        } 
       } 

    $ff=array_combine($even,$odd); 
+0

感謝您的迴應,但您的輸出與我的輸出不同。我希望像這樣的輸出Array(Array [0] =>([b_price] => 9500 [b_discount] => 10 [數組[1] =>([b_price] => 1500 [b_discount] => 15 [mainPrice] => 15000 [total] => 22500 [mainPrice] => 95000 [total] => 95000 [title] => obj1) [title] => obj2)) – Saeidhp

0

所以,我寫這篇文章的決定。也許它可以更清楚,但它的工作原理。

$array='{b_price,9500,b_discount,10,mainPrice,95000,total,95000,title,obj1},{b_price,1500,b_discount,15,mainPrice,15000,total,22500,title,obj2}'; 

/*Making array from string*/ 
$tmp_array = explode("},{", $array); 

/*Removing { symbols*/ 
$tmp_array[0] = substr($tmp_array[0],1); 
$tmp_array[1] = substr($tmp_array[1],0,-1); 

/*Making arrays from string [0] and [1]*/ 
$tmp_array[0] = explode(',',$tmp_array[0]); 
$tmp_array[1] = explode(',',$tmp_array[1]); 

$new_array = []; 

/*Creating associative arrays*/ 
for($a = 0; $a < count($tmp_array); $a++) { 
    $new_as_array = []; 
    for($i = 0; $i <= count($tmp_array[0]); $i+=2) { 
     if($i + 1 <= count($tmp_array[0])) { 
      $new_as_array[$tmp_array[$a][$i]] = $tmp_array[$a][$i + 1];   
     } 
    } 
    $new_array[] = $new_as_array; 
} 

print_r($new_array); 
+0

感謝您的回覆。 – Saeidhp