1

I want to apologize in advanced if this was already asked, I'm not exactly sure what this would be called.


I'm storing data from a form into a MongoDB database and I'd like to create defined key-value pairs to make sorting easier.

Using this code I am able to do this with a one-dimensional array, but it does not work with multidimensional arrays:

/* $array = The array */
$new_array = array();

foreach ($array as $key => $value) {
    array_push($new_array, array(
        'name' => $key,
        'value' => $value
    ));
}

Example:

Input array:

Array
(
    [email] => test@mail.com
    [name] => John
    [sports] => Array
        (
            [outdoor] => Array
                (
                    [0] => Football
                    [1] => Baseball
                )

            [indoor] => Array
                (
                    [0] => Basketball
                    [1] => Hockey
                )
        )
)

Output array:

Array
(
    [0] => Array
        (
            [name] => email
            [value] => test@mail.com
        )

    [1] => Array
        (
            [name] => name
            [value] => John
        )

    [2] => Array
        (
            [name] => sports
            [value] => Array
                (
                    [outdoor] => Array
                        (
                            [0] => Football
                            [1] => Baseball
                        )

                    [indoor] => Array
                        (
                            [0] => Basketball
                            [1] => Hockey
                        )
                )
        )
)

Notice how it stops at the sports value array and does not change the array within it. How can I continue this pattern throughout all the arrays within it?

4

3 回答 3

3

您可以使用递归:

function keyValue($array){
  $new_array = array();

  foreach ($array as $key => $value) {
      array_push($new_array, array(
          'name' => $key,
          'value' => is_array($value) ? keyValue($value) : $value
      ));
  }

  return $new_array;
}
于 2013-05-25T21:25:28.720 回答
2

试穿这个尺寸:

$array = array(
    'email' => 'test@email.com',
    'name' => 'John',
    'sport' => array('Soccor', 'Hockey')
);

$func = function($value, $key) {

     $return = array();
     $return['name'] = $key;
     $return['value'] = $value;


    return $return;
};

$parsed = array_map($func, $array, array_keys($array));

对我来说,这返回了:

   array(3) {
  [0]=>
  array(2) {
    ["name"]=>
    string(5) "email"
    ["value"]=>
    string(14) "test@email.com"
  }
  [1]=>
  array(2) {
    ["name"]=>
    string(4) "name"
    ["value"]=>
    string(4) "John"
  }
  [2]=>
  array(2) {
    ["name"]=>
    string(5) "sport"
    ["value"]=>
    array(2) {
      ["outdoor"]=>
      array(2) {
        [0]=>
        string(8) "Football"
        [1]=>
        string(8) "Baseball"
      }
      ["indoor"]=>
      array(2) {
        [0]=>
        string(10) "Basketball"
        [1]=>
        string(6) "Hockey"
      }
    }
  }
}
于 2013-05-25T21:16:17.713 回答
0

将关联数组转换为 JSON 对象(StdClass)非常简单,反之亦然,保留本机数据类型(int、float、bool)。这是我的尝试:

到键值数组

function toKeyValue($values)
{
    $result = [];

    foreach ($values as $key => $value)
    {
        if (is_array($value)) {
            $result[$key] = toKeyValue($value);
        } elseif (is_object($value)) {
            $result[$key] = toKeyValue((array) $value);
        } else {
            $result[$key] = $value;
        }
    }

    return $result;
}

到 JSON 对象

function toJson($values)
{
   return json_decode(json_encode($values));
}

$values应该始终是一个数组。

于 2015-02-07T11:13:35.110 回答