Я ниже строки и хочу создать многомерный массив, используя уникальное значение
$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
)
)
)
)
)
)
Других решений пока нет …