0

this is the output of my array

[["1"],["1"],["1"],["1"],["1"],["1"],["1"],["1"],["1"],["1"],["1"],["1"],["1"],["1"]]

but I would like this to look like

[1,1,1,1,1,1,1]

how would this be achieved in php, it seems everything I do gets put into an object in the array, when I don't want that. I tried using array_values and it succeeded in returning the values only, since I did have keys originally, but this is still not the completely desired outcome

CQM
  • 42,592
  • 75
  • 224
  • 366
  • Possible duplicate: http://stackoverflow.com/questions/526556/how-to-flatten-a-multi-dimensional-array-to-simple-one-in-php – Matt Mar 07 '13 at 16:22

2 Answers2

1
$yourArray = [["1"],["1"],["1"],["1"],["1"],["1"],["1"],["1"],["1"],["1"],["1"],["1"],["1"],["1"]];

use following code in PHP 5.3+

$newArray = array_map(function($v) {
    return (int)$v[0];
}, $yourArray);

OR use following code in other PHP version

function covertArray($v) {
    return (int)$v[0];
}

$newArray = array_map('covertArray', $yourArray)
hfcorriez
  • 112
  • 8
0

You could always do a simple loop...

$newArray = array();

// Assuming $myArray contains your multidimensional array
foreach($myArray as $value)
{
   $newArray[] = $value[0];
}

You could beef it up a little and avoid bad indexes by doing:

   if( isset($value[0]) ) {
      $newArray[] = $value[0];
   }

Or just use one of the many array functions to get the value such as array_pop(), array_slice(), end(), etc.

Jeremy Harris
  • 24,318
  • 13
  • 79
  • 133