2013-03-26 53 views
-2

我有一個字符串劈裂在PHP中的字符串作爲數組

pid1+2+price1+20+qty1+2+pid2+3+price2+20+qty2+1+

這是一個結果進行一些處理... 我需要創建數組這應該是像

pid1->2 
price1->20 
qty1->2(first array) 

pid2->2 
price2->20 
qty2->2(Second array) 

我試過使用爆炸,什麼都沒有實現... 謝謝

上面的字符串已被創建代碼

$a = var_export($_REQUEST); 
foreach ($_REQUEST as $key=>$value) 
{ 
    $a = $key . "+" . $value . "+"; 
    echo $a; 
} 
+0

應該'PID *'是關鍵?你知道你有多少價值嗎? – BenM 2013-03-26 12:14:42

+0

如果實際回答您的問題,請考慮接受答案 – michi 2013-04-14 12:39:00

回答

1

如果你可以改變的方式來創建$一個,這樣做:

foreach($_REQUEST as $key=>$value) 
    $a[$key]=$value; 

如果你有$a它是在上班的路上,這樣做:

$a = explode('+',$a); 
$max = count($a); 

for ($i = 0; $i < $max; $i += 2) 

    $b[$a[$i]] = $a[$i+1]; 

var_dump($b); 
1
$string = "pid1+2+price1+20+qty1+2+pid2+3+price2+20+qty2+1+"; 
$parts = explode ('+' , $string); 

$data = array(); 
$c = 0; 
for ($i = 0; $i < sizeof($parts); $i++) { 
$data[$c][$parts[$i]] = $parts[++$i]; 
$data[$c][$parts[++$i]] = $parts[++$i]; 
$data[$c][$parts[++$i]] = $parts[++$i]; 
$c++; 
} 
echo '<pre>'; 
var_dump($data); 
echo '</pre>'; 

可以這樣做。

輸出是: //使用RTRIM($字符串, '+'),以避免最後一個空數組

array(3) { 
    [0]=> 
    array(3) { 
    ["pid1"]=> 
    string(1) "2" 
    ["price1"]=> 
    string(2) "20" 
    ["qty1"]=> 
    string(1) "2" 
    } 
    [1]=> 
    array(3) { 
    ["pid2"]=> 
    string(1) "3" 
    ["price2"]=> 
    string(2) "20" 
    ["qty2"]=> 
    string(1) "1" 
    } 
    [2]=> 
    array(1) { 
    [""]=> 
    NULL 
    } 
} 
3

試試這個:

$str = "pid1+2+price1+20+qty1+2+pid2+3+price2+20+qty2+1+"; 

preg_match_all('/(?P<var>\w+)\+(?P<digit>\d+)/',$str,$macth); 
$res = array_chunk(array_combine($macth['var'],$macth['digit']),3,TRUE); 

echo "<pre>"; 
print_r($res); 

輸出:

Array 
(
    [0] => Array 
     (
      [pid1] => 2 
      [price1] => 20 
      [qty1] => 2 
     ) 

    [1] => Array 
     (
      [pid2] => 3 
      [price2] => 20 
      [qty2] => 1 
     ) 

) 
1

所以這裏是我的解決方案:您可以使用爆炸分割字符串由+字符r,然後交替每個值作爲新數組的關鍵字和值。

$str = 'pid1+2+price1+20+qty1+2+pid2+3+price2+20+qty2+1+'; 
$split = explode("+",$str); 
$final = array(); 
$lastKey = ''; 
foreach ($split as $index => $value) { 
    if ($value){ // don't store empty values 
    if ($index % 2 == 0){ // this is the alternation (odd/even index) 
     $lastKey = $value; // save the key for the next value 
     $final[$value] = ''; // initialize the element for the next value 
    }else{ 
     $final[$lastKey] = $value; // set the value for the previous key 
    } 
    } 
} 
1

你可以試試這個

<?php 
//$_REQUEST Array is here termed as $reqArr 
$reqArr['pid1'] = '2'; 
$reqArr['price1'] = '20'; 
$reqArr['qty1'] = '1'; 
$reqArr['pid2'] = '3'; 
$reqArr['price2'] = '40'; 
$reqArr['qty2'] = '2'; 

$loopCount = sizeof($reqArr)/3; // 3 is the count of items per array (ie pid, price & qty here) 
for($i = 1; $i <= $loopCount; $i++) { 
    $finalArr[$i]['pid'.$i] = $reqArr['pid'.$i]; 
    $finalArr[$i]['price'.$i] = $reqArr['price'.$i]; 
    $finalArr[$i]['qty'.$i] = $reqArr['qty'.$i]; 
} 
echo '<pre>'; 
print_r($finalArr); 
?> 

結果將是

Array 
(
    [1] => Array 
     (
      [pid1] => 2 
      [price1] => 20 
      [qty1] => 1 
     ) 

    [2] => Array 
     (
      [pid2] => 3 
      [price2] => 40 
      [qty2] => 2 
     ) 

)