【问题标题】:how to seperate an array with two different id's using PHP如何使用PHP分隔具有两个不同ID的数组
【发布时间】:2018-08-01 19:36:05
【问题描述】:

如何提取具有不同 id 的数组,请注意 css_id 点击图片以获取更多示例

[0] => Array(
        [cses_id] => 54
        [ccc_id] => 1157
        [csh_id] => 206
        [css_id] => 441,442
        [sai_id] => 12213

    )

expedted output below
*********************************************
[0] => Array(
        [cses_id] => 54
        [ccc_id] => 1157
        [csh_id] => 206
        [css_id] => 441
        [sai_id] => 12213

    )

[1] => Array(
        [cses_id] => 54
        [ccc_id] => 1157
        [csh_id] => 206
        [css_id] => 442
        [sai_id] => 12213

    )

click here for another sample

【问题讨论】:

  • 逗号爆炸?
  • 这是来自您的数据库吗?这就是为什么永远不要像这样保存数据,将它们放在不同的行中
  • 对不起,我会编辑它
  • @yes ghost 它来自 db 有没有办法在数组中操作它?
  • @rtfm 数组来自数据库

标签: php arrays csv explode


【解决方案1】:

可以通过多种方式执行此任务。这只是适用于您的示例输入数组的一种方法。有关说明,请参阅内联 cmets。

代码:(Demo)

$array=['cses_id'=>'54','ccc_id'=>'1157','csh_id'=>'206','css_id'=>'441,442','sai_id'=>'12213'];

$count=0;

// convert each comma-separated value to an array of values & store the max comma count
$multi=array_map(function($v)use(&$count){$count=max($count,substr_count($v,',')); return explode(',',$v);},$array);
++$count;  // increment to know how many "columns" of values exist
for($col=0; $col<$count; ++$col){
    $temp=[];
    foreach($multi as $key=>&$val){
        $temp[$key]=(sizeof($val)>1?array_shift($val):current($val));  // take off the front element until only one is left (and repeat the use of final value)
    }
    $result[]=$temp;  // store this batch of columnar data
}
var_export($result);

输出:

array (
  0 => 
  array (
    'cses_id' => '54',
    'ccc_id' => '1157',
    'csh_id' => '206',
    'css_id' => '441',
    'sai_id' => '12213',
  ),
  1 => 
  array (
    'cses_id' => '54',
    'ccc_id' => '1157',
    'csh_id' => '206',
    'css_id' => '442',
    'sai_id' => '12213',
  ),
)

这是实现相同结果的另一种方法:(Demo)

$array=['cses_id'=>'54','ccc_id'=>'1157','csh_id'=>'206','css_id'=>'441,442','sai_id'=>'12213'];
do {
    $stop=true;                      // default to stop after each iteration
    $temp=[];                        // clear the $temp variable
    foreach($array as $key=>&$val){  // iterate associate array, modify $val by reference
        $parts=explode(',',$val,2);  // break the value on the first comma only
        $temp[$key]=$parts[0];       // associatively store first value to the temp array
        if(sizeof($parts)>1){        // if there was a comma this iteration...
            $stop=false;             // tell the loop to do another iteration
            $val=$parts[1];          // modify $array by overwriting $val with the remaining comma-separated value
        }
    }
    $result[]=$temp;                 // this this iteration's temporary array in the result array
} while(!$stop);                     // iterate while there are any commas still in the any of the elements

var_export($result);                 // print to screen

【讨论】:

  • @christophercaburog 如果这不适用于您的实际项目数据,您将需要使用更准确的示例数据和预期输出来更新您的问题。
  • @christopher fyi,我刚刚为您添加了另一种方法。
猜你喜欢
  • 2021-03-23
  • 2011-11-24
  • 2019-10-15
  • 1970-01-01
  • 1970-01-01
  • 2015-03-21
  • 1970-01-01
  • 1970-01-01
  • 2018-10-30
相关资源
最近更新 更多