简体   繁体   中英

How can I use the split string in form of an array in PHP?

Here is the small program where I had split the string so does this works as I had use HEREDOC to delimit string and now I want to use it in form of array where I'm struck,

<?php
$str = <<<EOD
2
3
1 2 3
4
2 1 3 1
EOD;

So here as you can see I used heredoc to make a string. Basically what I want to do is that, I want to access this string as an array in a way like,

In string, starting with 2 which is the size of an array containing two sub array ie 3 and 4 and inside 3 is 1 2 3 and in 4 is 2 1 3 1 and now I want to access them in form of array so that I can do some Arithmetic Sums.

Try this code.

I have added some comments in the code.
Edited with support for one size subarray.

$str = <<<EOD
4
3
1 2 3
4
2 1 3 1
2
2 1
1
2
EOD;

$arr = explode(PHP_EOL, $str); // explode on new line

$item = 0;
$header =0;
foreach($arr as $key => $val){
    if($key == 0){
        $items = $val; // how large is the final array supposed to be?
        $res = range(0,$val-1); // create an array with final size with dummy values
    }elseif ($key % 2 != 0) { //not even number meaning it is "header" for subarray
        if($header == 0) echo "Header: OK\n"; // is the PREVIOUS subarray correct size? error checking
        $header = $val; // save value of subarray size

    }else{
        $res[$item] = explode(" ", $val); // explode the value and place it in result array $item key
        $header = $header - count($res[$item]); // subtract the number of items in subarray from $header, this is error checking
        $item++; // add to counter

    }
}

if($item == $items) echo "Complete array: OK\n"; // error checking make sure final array is correct size


var_dump($res);

You can test it here: https://3v4l.org/kENmb

So finally I got my answer. CHECK IT OUT...!!!

<?php
$str=<<<EOD
2
3
1 2 3
4
2 1 3 1
EOD;

    $input = trim($str);
    $raw_data=explode(PHP_EOL,$input);
    $T = (int)$raw_data[0]*2;
    $test_cases=[];

    for($i=1;$i<=$T;$i+=2) {
        $local=[];
        $local[]=(int)$raw_data[$i];
        $local[]=explode(' ',$raw_data[$i+1]);
        $test_cases[]=$local;
    }

    function prefix($n,$index) {
      $total=0;
      for($i=0;$i<$index;$i++) {
        $total += $n[$i];
      }
      return $total;
    }

    function suffix($p,$index) {
       $total=0;
       for($i=(count($p)-1);$i>=$index-1;$i--) {
          $total += $p[$i];
       }
       return $total;
    }

    for($i=0;$i<count($test_cases);$i++) {
    $temp = NULL;
    $temp_index = NULL;
    for($j=1;$j<=$test_cases[$i][0];$j++) {
    $sum = prefix($test_cases[$i][1], $j) + suffix($test_cases[$i][1], $j);

    if($temp) {
        if($temp > $sum) {
            $temp = $sum;
            $temp_index = $j;
        }
    } else {
        $temp_index = $j;
        $temp = $sum;
    }

 }
   var_dump($temp,$temp_index);
}

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM