0

所以,我有一个数组,由于不相关的原因,它本身有一个内爆场。现在,我有兴趣在该字段中爆炸字符串,并用爆炸的结果替换该字段。我在这里有一个可行的解决方案,但它看起来很笨重,我对更有效的东西感兴趣。或者至少在美学上令人愉悦。

示例数组:

$items = array(
               'name' => 'shirt', 
               'kind' => 'blue|long|L', 
               'price' => 10, 
               'amount' => 5);

目标是用字段替换'kind'字段'color', 'lenght', 'size'

到目前为止,我有:

$attribs = array('color', 'lenght', 'size');  // future indices
$temp = explode("|", $items['kind']);  // exploding the field
foreach ($items as $key => $value) {  // iterating through old array
    if ($key == 'kind') {
        foreach ($temp as $k => $v) {  // iterating through exploded array
            $new_items[$attribs[$k]] = $v;  // assigning new array, exploded values
        }
    }
    else $new_items[$key] = $value;  // assigning new array, untouched values
}

应该(我是用心写的,无法访问我自己的代码,而且我无法验证我刚刚写的那个......所以如果有任何错误,我很抱歉)会导致一个新数组,看起来像这样:

$new_items = array(
               'name' => 'shirt', 
               'color' => 'blue', 
               'lenght' => 'long', 
               'size' => 'L', 
               'price' => 10, 
               'amount' => 5);

例如,我可以将这些值附加到$items数组 andunset($items['kind'])中,但这会破坏顺序,并且我有点需要它用于后续foreach循环。

那么,有没有更简单的方法呢?

编辑:(回复 Visage 和 Ignacio - 因为回复将代码弄乱了)如果我在 foreach 循环中调用一个,它会以特定顺序调用它们。如果我只是追加,我会弄乱表格显示所需的顺序。我必须使显示代码复杂化,这依赖于我获取初始数据的固定方式。目前,我使用(或等效)显示数据:

foreach($new_items as $v) echo "<td>$v</td>\n";

如果我只是追加,我必须做类似的事情:

echo "<td>$new_items['name']</td>\n";
foreach ($attribs as $v) echo "<td>$new_items[$v]</td>\n";
echo "<td>$new_items['price']</td>\n";
echo "<td>$new_items['amount']</td>\n";
4

5 回答 5

1

Associative arrays generally should not depend on order. I would consider modifying the later code to just index the array directly and forgo the loop.

于 2010-01-18T09:10:57.017 回答
0

Associative arrays do not have an order, so you can just unset the keys you no longer want and then simply assign the new values to new keys.

于 2010-01-18T09:12:39.430 回答
0

one way

$items = array(
               'name' => 'shirt',
               'kind' => 'blue|long|L',
               'price' => 10,
               'amount' => 5);
$attribs = array('color', 'lenght', 'size');
$temp = explode("|", $items['kind']);
$s = array_combine($attribs,$temp);
unset($items["kind"]);
print_r( array_merge( $items, $s) );
于 2010-01-18T09:13:10.210 回答
0

尝试这个:

$items = array( 'name' => 'shirt', 'kind' => 'blue|long|L', 'price' => 10, 'amount' => 5);
list($items['color'], $items['lenght'], $items['size'])=explode("|",$items['kind']);
unset $items['kind'];

我没有测试过它,但它应该可以工作。

于 2010-01-18T09:06:48.530 回答
0

这应该保留顺序,因为它将键和值拆分为两个按数字排序的数组并在最后组合它们。它不是更有效,但代码更容易阅读。

$kind_keys = array('color', 'length', 'size');
$kind_values = explode("|", $items['kind']);
$keys = array_keys($items);
$values = array_values($items);
$index = array_search('kind', $keys);

// Put the new keys/values in:
array_splice($keys, $index, 1, $kind_keys);
array_splice($values, $index, 1, $kind_values);

// combine the result into a new array:
$result = array_combine($keys, $values));
于 2014-03-18T05:51:25.453 回答