字符串到具有 PHP 中唯一值的多维数组
String to Multidimensional Array with Unique value in PHP
我有以下字符串,想使用唯一值创建多维数组
$string = 'test.jpg|test1.jpg|^22##29##p-test.jpg^22##29##test.video^22##30##p-test2.jpg^22##30##p-test3.jpg^22##30##test4.jpg^22##31##p-CCA_Nestea.jpg^22##31##p-test3.jpg|^24##32##p-test3.jpg^24##32##p-test3.jpg^24##33##p-test3.jpg|test1.jpg';
所以我尝试了下面的代码但没有用
$string = 'test.jpg|test1.jpg|^22##29##p-test.jpg^22##29##test.video^22##30##p-test2.jpg^22##30##p-test3.jpg^22##30##test4.jpg^22##31##p-test22.jpg^22##31##p-test3.jpg|^24##32##p-test3.jpg^24##32##p-test3.jpg^24##33##p-test3.jpg|test1.jpg';
$content_arr = explode("|", $string);
global $db;
$arr1 = array(); $p =0;$prev_layout ='';
foreach($content_arr as $arr){
if(strpos($arr, '^') !== false) {
$l_data = explode("^",$arr);
$larr = array();
foreach ($l_data as $l) {
if(strpos($l, '##') !== false) {
$p = explode("##",$l);
$l_name = 'test lay';
$p_name = 'test pame';
if(!in_array($prev_layout,$p_arr)){
$p_arr = array();
$p_arr['l_id'] =$p[0];
$p_arr['l_name'] =$l_name;
$p_arr['l_interval'] ='';
}
$p_arr['panel'][$p++] = array('p_id'=>$p[1],'p_name'=>$p_name,'p_interval'=>'','p_element'=>$p[2]);
if($p_arr['layout_id'] != $p[0])
//if(!in_array($prev_layout,$p_arr, true)){
array_push($arr1,$p_arr);
//}
if (substr($p[2], -6) != '.video') {
$c++;
}
$prev_layout = $p[0];
}
}
}
}
?>
输出将是
[0] => Array
(
[l_id] => 22
[l_name] => test Menu
[l_interval] =>
[panel] => Array
(
[0] => Array
(
[p_id] => 29
[p_name] => Left
[p_interval] =>
[p_element] => [0] => Array
(
[0] => test.jpg,
[1] => test1.jpg
)
)
)
)
我试过你的代码并根据你的估计输出修改它是否正确?
$string = 'test.jpg|test1.jpg|^22##29##p-test.jpg^22##29##test.video^22##30##p-test2.jpg^22##30##p-test3.jpg^22##30##test4.jpg^22##31##p-test22.jpg^22##31##p-test3.jpg|^24##32##p-test3.jpg^24##32##p-test3.jpg^24##33##p-test3.jpg|test1.jpg';
$arr1 =$p_arr= array(); $p =0;$prev_layout ='';
$content_arr = array_filter(explode("|", $string));
global $db;
foreach($content_arr as $arr){
if(strpos($arr, '^') !== false) {
$larr = array();
$l_data = array_filter(explode("^",$arr));
foreach ($l_data as $l) {
if(strpos($l, '##') !== false) {
$l_name = 'test lay'; $p_name = 'Left';//'test pame';
$p=array();
$p = explode("##",$l);
$p0 = $p[0];
$p1 = $p[1];
unset($p[0]);unset($p[1]);
if(!array_key_exists($p0,$p_arr)){
$p_arr[$p0] = array('l_id'=>$p0,
'l_name'=>$l_name,
'l_interval'=>'','panel'=>array());
}
if(!array_key_exists($p1,$p_arr[$p0]['panel'])){
$p_arr[$p0]['panel'][$p1] = array('p_id'=>$p1,'p_name'=>$p_name,'p_interval'=>'','p_element'=>array(array_values($p)));
}else{
$p_arr[$p0]['panel'][$p1]['p_element'][] = array_values($p);
//$p_arr[$p0]['panel'][$p1]['p_element'] = array_merge($p_arr[$p0]['panel'][$p1]['p_element'],array_values($p));//if you want to mearge array
}
}
}
}
}
print_r($p_arr);die;
输出结果是
Array
(
[22] => Array
(
[l_id] => 22
[l_name] => test lay
[l_interval] =>
[panel] => Array
(
[29] => Array
(
[p_id] => 29
[p_name] => Left
[p_interval] =>
[p_element] => Array
(
[0] => Array
(
[0] => p-test.jpg
)
[1] => Array
(
[0] => test.video
)
)
)
[30] => Array
(
[p_id] => 30
[p_name] => Left
[p_interval] =>
[p_element] => Array
(
[0] => Array
(
[0] => p-test2.jpg
)
[1] => Array
(
[0] => p-test3.jpg
)
[2] => Array
(
[0] => test4.jpg
)
)
)
[31] => Array
(
[p_id] => 31
[p_name] => Left
[p_interval] =>
[p_element] => Array
(
[0] => Array
(
[0] => p-test22.jpg
)
[1] => Array
(
[0] => p-test3.jpg
)
)
)
)
)
[24] => Array
(
[l_id] => 24
[l_name] => test lay
[l_interval] =>
[panel] => Array
(
[32] => Array
(
[p_id] => 32
[p_name] => Left
[p_interval] =>
[p_element] => Array
(
[0] => Array
(
[0] => p-test3.jpg
)
[1] => Array
(
[0] => p-test3.jpg
)
)
)
[33] => Array
(
[p_id] => 33
[p_name] => Left
[p_interval] =>
[p_element] => Array
(
[0] => Array
(
[0] => p-test3.jpg
)
)
)
)
)
)
我有以下字符串,想使用唯一值创建多维数组
$string = 'test.jpg|test1.jpg|^22##29##p-test.jpg^22##29##test.video^22##30##p-test2.jpg^22##30##p-test3.jpg^22##30##test4.jpg^22##31##p-CCA_Nestea.jpg^22##31##p-test3.jpg|^24##32##p-test3.jpg^24##32##p-test3.jpg^24##33##p-test3.jpg|test1.jpg';
所以我尝试了下面的代码但没有用
$string = 'test.jpg|test1.jpg|^22##29##p-test.jpg^22##29##test.video^22##30##p-test2.jpg^22##30##p-test3.jpg^22##30##test4.jpg^22##31##p-test22.jpg^22##31##p-test3.jpg|^24##32##p-test3.jpg^24##32##p-test3.jpg^24##33##p-test3.jpg|test1.jpg';
$content_arr = explode("|", $string);
global $db;
$arr1 = array(); $p =0;$prev_layout ='';
foreach($content_arr as $arr){
if(strpos($arr, '^') !== false) {
$l_data = explode("^",$arr);
$larr = array();
foreach ($l_data as $l) {
if(strpos($l, '##') !== false) {
$p = explode("##",$l);
$l_name = 'test lay';
$p_name = 'test pame';
if(!in_array($prev_layout,$p_arr)){
$p_arr = array();
$p_arr['l_id'] =$p[0];
$p_arr['l_name'] =$l_name;
$p_arr['l_interval'] ='';
}
$p_arr['panel'][$p++] = array('p_id'=>$p[1],'p_name'=>$p_name,'p_interval'=>'','p_element'=>$p[2]);
if($p_arr['layout_id'] != $p[0])
//if(!in_array($prev_layout,$p_arr, true)){
array_push($arr1,$p_arr);
//}
if (substr($p[2], -6) != '.video') {
$c++;
}
$prev_layout = $p[0];
}
}
}
}
?>
输出将是
[0] => Array
(
[l_id] => 22
[l_name] => test Menu
[l_interval] =>
[panel] => Array
(
[0] => Array
(
[p_id] => 29
[p_name] => Left
[p_interval] =>
[p_element] => [0] => Array
(
[0] => test.jpg,
[1] => test1.jpg
)
)
)
)
我试过你的代码并根据你的估计输出修改它是否正确?
$string = 'test.jpg|test1.jpg|^22##29##p-test.jpg^22##29##test.video^22##30##p-test2.jpg^22##30##p-test3.jpg^22##30##test4.jpg^22##31##p-test22.jpg^22##31##p-test3.jpg|^24##32##p-test3.jpg^24##32##p-test3.jpg^24##33##p-test3.jpg|test1.jpg';
$arr1 =$p_arr= array(); $p =0;$prev_layout ='';
$content_arr = array_filter(explode("|", $string));
global $db;
foreach($content_arr as $arr){
if(strpos($arr, '^') !== false) {
$larr = array();
$l_data = array_filter(explode("^",$arr));
foreach ($l_data as $l) {
if(strpos($l, '##') !== false) {
$l_name = 'test lay'; $p_name = 'Left';//'test pame';
$p=array();
$p = explode("##",$l);
$p0 = $p[0];
$p1 = $p[1];
unset($p[0]);unset($p[1]);
if(!array_key_exists($p0,$p_arr)){
$p_arr[$p0] = array('l_id'=>$p0,
'l_name'=>$l_name,
'l_interval'=>'','panel'=>array());
}
if(!array_key_exists($p1,$p_arr[$p0]['panel'])){
$p_arr[$p0]['panel'][$p1] = array('p_id'=>$p1,'p_name'=>$p_name,'p_interval'=>'','p_element'=>array(array_values($p)));
}else{
$p_arr[$p0]['panel'][$p1]['p_element'][] = array_values($p);
//$p_arr[$p0]['panel'][$p1]['p_element'] = array_merge($p_arr[$p0]['panel'][$p1]['p_element'],array_values($p));//if you want to mearge array
}
}
}
}
}
print_r($p_arr);die;
输出结果是
Array
(
[22] => Array
(
[l_id] => 22
[l_name] => test lay
[l_interval] =>
[panel] => Array
(
[29] => Array
(
[p_id] => 29
[p_name] => Left
[p_interval] =>
[p_element] => Array
(
[0] => Array
(
[0] => p-test.jpg
)
[1] => Array
(
[0] => test.video
)
)
)
[30] => Array
(
[p_id] => 30
[p_name] => Left
[p_interval] =>
[p_element] => Array
(
[0] => Array
(
[0] => p-test2.jpg
)
[1] => Array
(
[0] => p-test3.jpg
)
[2] => Array
(
[0] => test4.jpg
)
)
)
[31] => Array
(
[p_id] => 31
[p_name] => Left
[p_interval] =>
[p_element] => Array
(
[0] => Array
(
[0] => p-test22.jpg
)
[1] => Array
(
[0] => p-test3.jpg
)
)
)
)
)
[24] => Array
(
[l_id] => 24
[l_name] => test lay
[l_interval] =>
[panel] => Array
(
[32] => Array
(
[p_id] => 32
[p_name] => Left
[p_interval] =>
[p_element] => Array
(
[0] => Array
(
[0] => p-test3.jpg
)
[1] => Array
(
[0] => p-test3.jpg
)
)
)
[33] => Array
(
[p_id] => 33
[p_name] => Left
[p_interval] =>
[p_element] => Array
(
[0] => Array
(
[0] => p-test3.jpg
)
)
)
)
)
)