1

请帮我解决一个我真正陷入 PHP 的问题

我有一个大数组

$a = array(
    'foo_1' => 'Dog',
    'bar_1' => 'Cat',
    'baz_1' => 'Fish',
    'foo_2' => 'Frog',
    'bar_2' => 'Bug',
    'baz_2' => 'Ddd',
    ...
);

并且需要将其转换为多个数组:

$a_1 = array(
    'foo' => 'Dog',
    'bar' => 'Cat',
    'baz' => 'Fish'
);  

$a_2 = array(
    'foo' => 'Frog',
    'bar' => 'Bug',
    'baz' => 'Ddd'
);

正如你所注意到的,有一个小逻辑。大数组应该由键集( _1,_2,_3 ; _1,_2,_33 )分割

希望我足够清楚!

谢谢

4

4 回答 4

2

假设密钥总是每3 个项目重复一次并且它们是有序的,array_chunk那么会起作用:

$newArray = array_chunk($a, 3);
于 2012-11-12T23:52:16.137 回答
1

假设$a有这个结构(因为 OP 原始数组有重复的键):

$a = array(
'foo_1' => 'Dog',
'bar_1' => 'Cat',
'baz_1' => 'Fish',
'foo_2' => 'Frog',
'bar_2' => 'Bug',
'baz_2' => 'Ddd'
);

您可以通过这种方法转换数组:

$temp = array();
foreach($a as $key=>$val){
    $num = substr(strrchr($key, '_'), 1);
    $nkey = basename($key, '_'.$num);
    $temp[$num][$nkey] = $val;
}
// See what happens
print_r($temp); 
于 2012-11-12T23:57:08.880 回答
0

我确定多维数组更好,但这就是您所要求的,“_”之前具有相同字符串的值将被覆盖(丢失),因此您的逻辑可能存在问题。

$array1 = array();
$array2 = array();
$array3 = array();
$bigArray = array('av_1'=>'dghdfg','ab_2'=>'dgdfg');
foreach($bigArray as $key=>$value)
{
  $splitKey = explode('_',$key);
  $number = $splitKey[1];
  $newKey = $splitKey[0];
  $varName = "array".$number;
  ${$varName}[$newKey] = $value;
}

多维:

    $newBigArray = array();
    $bigArray = array('av_1'=>'dghdfg','ab_2'=>'dgdfg');

    foreach($bigArray as $key=>$value)
    {
      $splitKey = explode('_',$key);
      $newKey = $splitKey[0];
      $number = $splitKey[1];

      if(!isset($newBigArray[$newKey])) {
        $newBigArray[$newKey] = array();
      }
      $newBigArray[$newKey][$number] = $value;
    }
    var_dump($newBigArray);
于 2012-11-12T23:46:41.987 回答
0

获取所有数组条目的键,用下划线字符(_)分割它们,然后将它们放入一个数组中。

$a = array(
    'foo_1' => 'Dog',
    'bar_2' => 'Cat',
    'baz_3' => 'Fish',
    'foo_1' => 'Frog',
    'bar_2' => 'Bug',
    'baz_3' => 'Ddd',
    ...
);

$arrays = array(
    'foo' => array(),
    'bar' => array(),
    'baz' => array()
);

foreach ($a as $k => $v) {
    $s = explode("_", $k);
    if (!isset($arrays[$s[0]])) {
        $arrays[$s[0]] = array();
    }
    $arrays[$s[0]][$s[1]] = $v; // This line if you want to preserve the index (1, 2, ...)
    $arrays[$s[0]][] = $v; // Or this line if you want to reindex the arrays.
    // Comment or remove one of these two lines.
}

然后你有一个多维数组:

array(
    'foo' => array(
       "Dog",
       "Frog"
    ),
    'bar' => array(
        "Cat",
        "Bug"
    ),
    'baz' => array(
        "Fish",
        "Ddd"
    ),
    ...
)

我建议您使用上面的多维数组,但如果您想为每个“键”(如“foo”、“bar”等)创建一个新变量,请使用以下代码段:

$a = array(
    'foo_1' => 'Dog',
    'bar_2' => 'Cat',
    'baz_3' => 'Fish',
    'foo_1' => 'Frog',
    'bar_2' => 'Bug',
    'baz_3' => 'Ddd',
    ...
);

foreach ($a as $k => $v) {
    $s = explode("_", $k);
    ${$s[0]}[$s[1]] = $v; // This line if you want to preserve the index (1, 2, ...)
    ${$s[0]}[] = $v; // Or this line if you want to reindex the arrays.
    // Comment or remove one of these two lines.
}

请注意,您的数组$a有一些重复的键,这在 PHP 中是不可能的。
编辑:我看到你解决了。

于 2012-11-13T00:00:03.730 回答