-2

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

18
  • 2
    That's not a string. That's an array of arrays. Commented Sep 14, 2015 at 13:42
  • 1
    Show us your real code Commented Sep 14, 2015 at 13:42
  • 1
    @Rizier123 actually that value I am getting from phython and I am doing calculation here Commented Sep 14, 2015 at 13:44
  • 1
    @YUNOWORK can you help me to find a simple knife to cut. It will be really helpful!! Commented Sep 14, 2015 at 13:46
  • 1
    Yes its been a long time @Rizier123 but I've deleted mine. This is really been misinterpreted by everyone over here Commented Sep 14, 2015 at 14:16

7 Answers 7

0

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

Sign up to request clarification or add additional context in comments.

1 Comment

not working at all in my case. $A = array(); Im getting null array
0

This will work

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

Comments

0

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
)

1 Comment

Not working get warnings as Warning: explode() expects parameter 2 to be string, array and Warning: array_map(): Argument #2 should be an array
0

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]]));

2 Comments

I got output as array (size=1) 0 => string '[' (length=1). Not working
@eroydev my array looks like [[0][1]] not like[[0],[1]]. Do not have a comma seperated. So your code not working!!
0

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.

1 Comment

my case it is not comma seperated it is [[1] [2] [3] [4]] not [[1],[2],[3],[4]]
0

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);

Comments

0

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.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.