How to reorganize PHP arrays reassemble them according to my format

this problem has been bothering me not to be able to Rest for several days! How to reassemble it into a new format?

the content is written at will. Mainly the format! The number after
sitePerEpisode is the number of sets

:

$ceshi="$$1$http://v.youku.com/$youku-sharp2$http://v.youku.com/v_show$youku-sharp3$http://v.youku.com/$$$$$1$http://vmguo.com$mgtv-sharp2$http://v.mangguo.com$mgtv-sharp3$http://v.mangguo.com$mgtv";

format to be changed

{
    "sitePerEpisode": {
        "1": [
            {
                "siteSource": "youku",
                "siteName": "",
                "siteLink": "http://v.youku.com/"
            },
            {
                "siteSource": "mgtv",
                "siteName": "",
                "siteLink": "/video/?34-1-0.html"
            }
        ],
        "2": [
            {
                "siteSource": "youku",
                "siteName": "",
                "siteLink": "/video/?34-0-1.html"
            },
            {
                "siteSource": "mgtv",
                "siteName": "",
                "siteLink": "/video/?34-1-1.html"
            }
        ],
        "3": [
            {
                "siteSource": "youku",
                "siteName": "",
                "siteLink": "/video/?34-0-2.html"
            },
            {
                "siteSource": "mgtv",
                "siteName": "",
                "siteLink": "/video/?34-1-2.html"
            }
        ]
    }
}

in the past, it was played by source a video 1 2 3 b video 1 2 3 c video 1 2 3
now it is required by the number of sets
1 a video b video c video 2 a video c video 3 a video b video c video

Mar.12,2021

Brother, your typesetting really makes people have no desire to help you. And the json you posted is also in the wrong format, so it's even more impossible to help, so you'd better correct the typesetting of the problem first.

the brothers above have already answered it. I have another way of thinking. It can be regarded as the cool techs of php. You can also refer to

.
function formatSite($site_str) {
    
        $sites_info = explode('$$$', $site_str);

        $result = [];
        foreach ($sites_info as $site_info) {
            list($site_name, $info) = explode('$$', $site_info);

            array_map(function ($item) use (&$result, $site_name) {
                preg_match('/.*(\d).*\$(.*)\$(.*)/', $item, $matched);
                list($number, $site_link, $site_source) = array_slice($matched, 1);
                $result['sitePerEpisode'][$number][] = [
                    'siteSource'=>$site_source,
                    'siteName'=>$site_name,
                    'siteLink'=>$site_link
                ];
            }, explode('-sharp', $info));
        }
        return $result;
}

$str = '$$1$http://v.youku.com/$youku-sharp2$http://v.youku.com/v_show$youku-sharp3$http://v.youku.com/$youku$$$$$1$http://vmguo.com$mgtv-sharp2$http://v.mangguo.com$mgtv-sharp3$http://v.mangguo.com$mgtv';
$result = formatSite($str);

the train of thought is the same, and the logical brother's answer is clearer. I just used some cool techs

.

your variable $ceshi seems to be wrong. I changed it. I don't know if it's correct

.
//
$ceshi='$$1$http://v.youku.com/$youku-sharp2$http://v.youku.com/v_show$youku-sharp3$http://v.youku.com/$$$$$1$http://vmguo.com$mgtv-sharp2$http://v.mangguo.com$mgtv-sharp3$http://v.mangguo.com$mgtv';

//$youku
$ceshi='$$1$http://v.youku.com/$youku-sharp2$http://v.youku.com/v_show$youku-sharp3$http://v.youku.com/$youku$$$$$1$http://vmguo.com$mgtv-sharp2$http://v.mangguo.com$mgtv-sharp3$http://v.mangguo.com$mgtv';

$result_array = array();

$temp = explode('$$$',$ceshi);
//var_dump($temp);
foreach($temp as $key=>$value){

    $temp_array = (explode('$$',$value));
    //
    $category = $temp_array[0];
    //
    $temp_array = explode('-sharp',$temp_array[1]);
    foreach($temp_array as $key=>$value){
        
        //
        $temp_value = explode('$',$value);
        $ep_number = preg_replace('/()||()/','',$temp_value[0]);
        
        //
        $temp_result['siteSource']= $temp_value[2];
        $temp_result['siteName']= $category;
        $temp_result['siteLink']= $temp_value[1];
    
        //
        $result_array["sitePerEpisode"][$ep_number][]=$temp_result;

    }
}

//$result_arrayvar_dump
//var_dump($result_array);

//json
$json_result = json_encode($result_array,true);
echo $json_result;

array results not converted to json

array(1) {
  ["sitePerEpisode"]=>
  array(3) {
    [1]=>
    array(2) {
      [0]=>
      array(3) {
        ["siteSource"]=>
        string(5) "youku"
        ["siteName"]=>
        string(12) ""
        ["siteLink"]=>
        string(19) "http://v.youku.com/"
      }
      [1]=>
      array(3) {
        ["siteSource"]=>
        string(4) "mgtv"
        ["siteName"]=>
        string(12) ""
        ["siteLink"]=>
        string(16) "http://vmguo.com"
      }
    }
    [2]=>
    array(2) {
      [0]=>
      array(3) {
        ["siteSource"]=>
        string(5) "youku"
        ["siteName"]=>
        string(12) ""
        ["siteLink"]=>
        string(25) "http://v.youku.com/v_show"
      }
      [1]=>
      array(3) {
        ["siteSource"]=>
        string(4) "mgtv"
        ["siteName"]=>
        string(12) ""
        ["siteLink"]=>
        string(20) "http://v.mangguo.com"
      }
    }
    [3]=>
    array(2) {
      [0]=>
      array(3) {
        ["siteSource"]=>
        string(5) "youku"
        ["siteName"]=>
        string(12) ""
        ["siteLink"]=>
        string(19) "http://v.youku.com/"
      }
      [1]=>
      array(3) {
        ["siteSource"]=>
        string(4) "mgtv"
        ["siteName"]=>
        string(12) ""
        ["siteLink"]=>
        string(20) "http://v.mangguo.com"
      }
    }
  }
}

traverses once is fine. You post the original PHP code you wrote.

Menu