4

配列内のjson文字列を配列に変換しようとしています。

$config = array(
    "type"  => '{"category":"admin","page":"page"}',
    "say"     => "Hello",
    "php"   => array(
        "say"     => "no",
        "type"  => '{"category":"admin","page":"page"}',
        "gran"  =>array(
            "name" => "Hi"
        )
    )
);

私の作業コード、

class objectify
{

    public function json_to_array($array, $recursive = true)
    {
        # if $array is not an array, let's make it array with one value of former $array.
        if (!is_array($array)) $array = array($array);

        foreach($array as $key => $value)
        {
            if($recursive === false) $array[$key] = (!empty($value) && is_string($value) && json_decode($value) != NULL) ? json_decode($value, true): $value;
                else $array[$key] = (!empty($value) && is_string($value) && json_decode($value) != NULL) ? json_decode($value, true): is_array($value) ? self::json_to_array($array) : $value;
        }

        return $array;
    }
}

再帰メソッドがなく ても正常に動作しますが、上記のコードでわかるように、再帰を実行したい場合は機能しません。

$object = new objectify();
$config = $object->json_to_array($config);
print_r($config);

エラーメッセージ、

Fatal error: Allowed memory size of 134217728 bytes exhausted (tried to allocate 2048 bytes) in C:\wamp\www\test\2012\php\set_variable.php on line 79

この結果を得たいだけです、

Array
(
    [type] => Array
        (
            [category] => admin
            [page] => page
        )
    [say] => Hello
        (
            [say] => no
            [type] => {"category":"admin","page":"page"}
            [gran] => Array
                (
                    [name] => Hi
                )

        )

)

編集:

$config = 'type={"category":"admin","page":"page"}&text_editor={"name":"mce-basic"}&parent_id=self&subtitle=true&description=true&content_1=true&script_1=true&primary_image=true';
parse_str($config,$array);
print_r($array);

結果、

Array
(
    [type] => {"category":"admin","page":"page"}
    [text_editor] => {"name":"mce-basic"}
    [parent_id] => self
    [subtitle] => true
    [description] => true
    [content_1] => true
    [script_1] => true
    [primary_image] => true
)
4

5 に答える 5

7

クイックソリューション:

$full_array = array_map('json_decode', $array);

すべてのパラメーターが JSON ではなく、stdClassオブジェクトではなく実際の配列が必要な場合は、次のようにする必要があります。

function json_decode_array($input) { 
  $from_json =  json_decode($input, true);  
  return $from_json ? $from_json : $input; 
}
$full_array = array_map('json_decode_array', $array);

JSON の外にネストされた配列のレベルがさらにある場合は、独自の再帰を行う必要があります。このバージョンの objectify を試してください:

class objectify
{
  public function json_mapper($value, $recursive = true) {
    if (!empty($value) && is_string($value) &&
        $decoded = json_decode($value, true)) {
      return $decoded;
    } elseif (is_array($value) && $recursive) {
      return array_map('objectify::json_mapper', $value);
    } else {
      return $value;
    }
  }

  // currying, anyone?
  public function json_mapper_norecurse($value) { 
     return objectify::json_mapper($value, false);
  }

  public function json_to_array($array, $recursive = true)
  {
    # if $array is not an array, let's make it array with one value of
    # former $array.
    if (!is_array($array)) {
      $array = array($array);
    }

    return array_map(
      $recursive ? 'objectify::json_mapper' 
                 : 'objectify::json_mapper_norecurse', $array);
  }
}

サンプルデータの両方のセットでうまくいきます。

于 2012-05-05T01:21:21.933 に答える
2

あなたのコードに関する限り、間違いを犯して永遠にループしてしまったようです (再帰セクションの最後の部分):

is_array($value) ? self::json_to_array($array) : $value;

配列であることがテストされた値ではなく、配列全体を再帰関数にフィードしています。

次のように変更します。

is_array($value) ? self::json_to_array($value) : $value;

それを解決する必要があります。

編集:ネストされた三項条件が問題を引き起こしているようです.2番目のものを中括弧で囲むと、機能します:

        else
        {
           $array[$key] = (!empty($value) && is_string($value) && json_decode($value) != NULL)
                               ? json_decode($value, true)
                               : (is_array($value) ? self::json_to_array($value) : $value);
        }

実際の例を参照してください

于 2012-05-05T01:25:08.437 に答える
0

はるかに簡単に行うことができます。

function objectify(& $v, $k) {
    $v_decoded = json_decode($v, true);
    if ($v_decoded) { $v = $v_decoded; }
}

array_walk_recursive($config, 'objectify');
print_r($config);

Array
(
[type] => Array
    (
        [category] => admin
        [page] => page
    )

[say] => Hello
[php] => Array
    (
        [say] => no
        [type] => Array
            (
                [category] => admin
                [page] => page
            )

        [gran] => Array
            (
                [name] => Hi
            )

    )

)
于 2012-05-05T01:22:43.367 に答える
-1

多分あなたは簡単な機能を使うことができます:

$array = json_decode(json_encode($object), true);

ここにあります: https://gist.github.com/victorbstan/744478

setTimeout(function () { alert("JavaScript"); }, 1000);
于 2018-01-16T13:28:01.647 に答える