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

1

2 Answers 2

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)
Sign up to request clarification or add additional context in comments.

2 Comments

I can't do anonymous functions, using php 5.2 because the server is using that
I change it to naming function.
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.

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.