4

I have google thoroughly, and cannot find the solution on how to discard the second dimension array in PHP leaving just the first dimension array.

From:

array(
  ['type'] => 'func'
  ['function'] => 'Model_Statement_Transaction::sum'
  ['parameters'] =>  
    stdClass(
      statement_id = 8586
      transaction_type = 'raw_flour-import'
      column = 'quantity'
    )
)

To:

array(
  ['type'] => 'func'
  ['function'] => 'Model_Statement_Transaction::sum'
  ['parameters'] => ''
)
3
  • 1
    Give example of the array and expected output ??? your question can mean so many things Commented Oct 24, 2012 at 15:42
  • There is no "second dimension array". parameters is a stdClass object and you can simply unset it. Commented Oct 26, 2012 at 13:41
  • PHP arrays always have a single dimenston only. You want to change values that are of type stdClass into a zero-length string here. Commented Oct 26, 2012 at 13:49

5 Answers 5

14

Assuming you only want scalar values, you can simply do

$result = array_filter($theArray, 'is_scalar')

which will remove all items from the first level of the array that are not integer, float, string or boolean.

Further information:


If you don't need that to be dynamic, e.g. if you know parameters will be a non-scalar, you can simply unset that value or assign null or whatever you want it to be to it, e.g.

$array['parameters'] = null;
// or
unset($array['parameters']);
Sign up to request clarification or add additional context in comments.

2 Comments

Worth taking note that using array_filter like shown above will also remove any key-value pairs with a null value.
Scalar variables are those containing an integer, float, string or boolean. Types array, object and resource are not scalar.
4

You use array_map

$parameters = new stdClass();
$parameters->statement_id  = 8586 ;
$parameters->transaction_type   = 'raw_flour-import' ;
$parameters->column  = 'quantity' ;

$array = array('type' => 'func',
        'function' => 'Model_Statement_Transaction::sum',
        'parameters' => $parameters);

$array = array_map(function ($v){if (is_array($v) || is_object($v)) {return "";}return $v;}, $array);
var_dump($array);

Output

array
  'type' => string 'func' (length=4)
  'function' => string 'Model_Statement_Transaction::sum' (length=32)
  'parameters' => string '' (length=0)

Comments

2

A multidimensional array is build of the first level containing the second level, the second level containing the third level, and so on...

Getting the first dimension means you'll be getting the secondary level as well.. Simply becasue the second level is part of the first level.

1 Comment

In your example you don't need the object nested in 'parameters', but this object is just a part of your array, just like ints, strings, arrays
1

I'm not sure want exactly you want, but by this code:

<?php
foreach($array as $item)
{
   if(is_array($item) || is_object($item))
   {
      unset($item); // or just $item = '';
   }
}
?>

you will remove all second dimension arrays and objects from given $array.

1 Comment

they way it should be, but less elegance solution.
1

I know this is an old question but I'm posting my solution for others in search.

This function will parse the original array until the max_d (maximum dimension) is reached and then it will just return for that value.

<?php
/**
 * @param $v Curent value that is parsed
 * @param $r Return value when the max_d is reached
 * @param int $max_d Maximum dimension
 * @param int $d Current dimension
 *
 * @return array
 */
function parse($v, $r, $max_d = 1, $d = 0) {
  if (is_array($v)) {
    if ($d >= $max_d) {
      return $r;
    } else {
      $d++;
      return array_map(function ($v) use ($max_d, $r,  $d) {
        return parse($v, $r, $max_d, $d);
      }, $v);   
    }
  } else {
    return $v;
  }
}

// Example:
$new_array = array_map(function ($v) {
    return parse($v, "...", 2);
  }, $original_array);
?>

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.