-2

私は文字列を持っています

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;
}
4

5 に答える 5

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
        )

)
于 2013-03-26T12:21:44.137 に答える
1

$a の作成方法を変更できる場合は、次のようにします。

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);
于 2013-03-26T12:17:29.333 に答える
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($string, '+') を使用して、最後の空の配列を回避します

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
  }
}
于 2013-03-26T12:19:59.863 に答える
1

だからここに私の解決策があります.explodeを使用して文字列を文字で分割し+、新しい配列のキーと値である各値を交互に使用できます。

$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
    }
  }
}
于 2013-03-26T12:22:41.510 に答える
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
        )

)
于 2013-03-26T12:34:16.837 に答える