php数组到1D数组的数组没有按预期正常运行

I am reading value from CMD which is running a python program and my output as follows:

enter image description here

Let as assume those values as $A:

$A = [[1][2][3][4]....]

I want to make an array from that as:

$A = [1,2,3,4....]

I had tried as follows:

$val = str_replace("[","",$A);
$val = str_replace("]","",$val);
print_r($val);

I am getting output as:

Array ( [0] => 1 2 3 4 ... )

Please guide me

This function will work when you do indeed have a multidimensional array, which you stated you have, in stead of the String representation of a multidimensional array, which you seem to have.

function TwoDToOneDArray($TwoDArray) {
    $result = array();
    foreach ($TwoDArray as $value) {
        array_push($result, $value[0]);
    }
    return $result;
}

var_dump(TwoDToOneDArray([[0],[1]]));

If you have a string like you wrote in the first place you can try with regex:

$a = '[[1][2][3][4]]';

preg_match_all('/\[([0-9\.]*)\]/', $a, $matches);
$a = $matches[1];

var_dump($a);

try this

// your code goes here
$array = array(
    array("1"),
    array("2"),
    array("3"),
    array("4")
    );

$outputArray = array();

foreach($array as $key => $value)
{
    $outputArray[] = $value[0];     
}

print_r($outputArray);

Also check the example here https://ideone.com/qaxhGZ

This will work

array_reduce($a, 'array_merge', array());

Multidimensional array to single dimensional array,

$it = new RecursiveIteratorIterator(new RecursiveArrayIterator($A));
$A = iterator_to_array($it, false);

But, if $A is string

$A = '[[1][2][3][4]]';

$A = explode('][', $A);
$A = array_map(function($val){
        return trim($val,'[]');
    }, $A);

Both codes will get,

Array
(
    [0] => 1
    [1] => 2
    [2] => 3
    [3] => 4
)

You can transform $A = [[1],[2],[3],[4]] into $B = [1,2,3,4....] using this following one line solution:

$B = array_map('array_shift', $A);

PD: You could not handle an array of arrays ( a matrix ) the way you did. That way is only for managing strings. And your notation was wrong. An array of arrays (a matrix) is declared with commas.

If $A is a string that looks like an array, here's one way to get it:

$A = '[[1][2][3][4]]';
print "[".str_replace(array("[","]"),array("",","),substr($A,2,strlen($A)-4))."]";

It removes [ and replaces ] with ,. I just removed the end and start brackets before the replacement and added both of them after it finishes. This outputs: [1,2,3,4] as you can see in this link.