2

次のような配列があります。

  Array
(
    [owner] => user_id
    [add_owner] => imagetype
    [cache] => cc118e60798c3369f4cc0a544f671e9c
    [link] => Array
        (
            [0] => http://cibooo
            [1] => http://teamimage
        )

    [imagetype] => Array
        (
            [0] => 8
            [1] => 9
        )

    [email] => Array
        (
            [0] => cibooo@mai.com
        )

)

ご覧のとおり、一部のキーには複数の値があります。私がやりたいことは、配列に複数の値を持つキーが含まれている場合、次の配列が生成されることです。

Array
        (
     [images] => Array
                (
                    [owner] => user_id
                    [add_owner] => imagetype
                    [link]  => http://cibooo    
                    [imagetype] => 8
                    [email] => cibooo@mai.com

    )

    [images] => Array
                (
                    [owner] => user_id
                    [add_owner] => imagetype
                    [link]  => http://teamimage    
                    [imagetype] => 9
                    [email] => cibooo@mai.com

    )

)

今私がしたことは次のとおりです:

foreach ($updates as $update) {

    if (isset($data[$update['start_param']]) || $update['start_param'] == 'any') {
        /*
         * We check if the update input value is empty, if so
         * we replace it with an alternative value.
         * This alternative value can be the value of the input data
         * in the case that it's available, if not it is replaced
         * with the value of the input data that corresponds to the
         * start_param of each update input.
         * If the start_param is 'any' it gets ignored.
         */
        $update_value_alternative = isset($data[$update['name']]) ? $data[$update['name']] : isset($data[$update['start_param']]) ? $data[$update['start_param']] : NULL;
        $update_value = !$this->check->isEmpty($update['value']) ? $update['value'] : $update_value_alternative;
        /* 
         * owner and add_owner must be the same for the update inputs
         * of the same table, so we do not mind if the $vals['owner']
         * and $vals['add_owner'] is replaced on each loop for the update inputs
         * of the same table.
         */
        $vals['owner'] = $update['owner'];
        $vals['add_owner'] = $update['add_owner'];
        /* 
         * We add the cache on each loop, will be deleted for
         * those tables that do not contain a cache column.
         */
        $vals['cache'] = $this->generate('generate->hash');
        /*
         * The names of all the update inputs and their relative
         * values.
         * The values are passed through the generate() method
         * in order to generate a unique ID or a specific value
         * based on what has been inserted in each specific update
         * input value. Example: generate->hash
         */
        $vals[$update['name']][] = $this->generate($update_value, $update['owner'], $request, $data);
        $tables[$update['table']] = $vals;
    }
}

スクリプトの一番下に、配列を生成する方法が表示されます。問題は、配列の各キーに対してより多くの値が見つかったときに別の配列を作成するのではなく、最終的な配列が次のようになることです$updates。各キーにさらに多くの値が含まれている$tables[$update['table']]場合に、別の配列を作成する方法を理解する必要があります。$vals[$update['name']][]どうすればそれを達成できますか?

これは、コードで取得している配列です。

Array
(
    [images] => Array
        (
            [owner] => user_id
            [add_owner] => imagetype
            [cache] => 8669e31741b5d7c0f471167dca38cd4e
            [link] => Array
                (
                    [0] => http://cibooo
                    [1] => http://teamimage
                )

            [imagetype] => Array
                (
                    [0] => 8
                    [1] => 9
                )

            [email] => Array
                (
                    [0] => cibooo@mai.com
                )

        )
)
4

2 に答える 2

0

これは非常にトリッキーです。リンクが 3 つ、画像が 2 つ、メールが 1 つある場合はどうなるでしょうか。要素をパディングするだけでなく、代わりに順列を使用してさまざまな可能性を生成することをお勧めします。

どういう意味ですか?

$data = array(
        'owner' => 'user_id',
        'add_owner' => 'imagetype',
        'cache' => 'cc118e60798c3369f4cc0a544f671e9c',
        'link' => array(                       // 2 elements 
                'http://cibooo',
                'http://teamimage'
        ),
        'imagetype' => array(                  // 3 elements 
                8,
                9,
                10
        ),
        'email' => array(
                'cibooo@mai.com'               // 1 element 
        )
);

上記の配列をどのように組み合わせますか?

上記の形式を使用すると、一貫性のない可能性につながる可能性があります。ここで私がすべきことは次のとおりです。

unset($data['cache']);
$values = array_filter($data, "is_array");
$keys = array_keys($values);

$new = array();

foreach(permutations($values) as $v) {
    $new[] = array_merge($data, array_combine($keys, $v));
}

print_r($new);

出力

Array
(
    [0] => Array
        (
            [owner] => user_id
            [add_owner] => imagetype
            [link] => http://cibooo
            [imagetype] => 8
            [email] => cibooo@mai.com
        )

    [1] => Array
        (
            [owner] => user_id
            [add_owner] => imagetype
            [link] => http://teamimage
            [imagetype] => 8
            [email] => cibooo@mai.com
        )

    [2] => Array
        (
            [owner] => user_id
            [add_owner] => imagetype
            [link] => http://cibooo
            [imagetype] => 9
            [email] => cibooo@mai.com
        )

    [3] => Array
        (
            [owner] => user_id
            [add_owner] => imagetype
            [link] => http://teamimage
            [imagetype] => 9
            [email] => cibooo@mai.com
        )

    [4] => Array
        (
            [owner] => user_id
            [add_owner] => imagetype
            [link] => http://cibooo
            [imagetype] => 10
            [email] => cibooo@mai.com
        )

    [5] => Array
        (
            [owner] => user_id
            [add_owner] => imagetype
            [link] => http://teamimage
            [imagetype] => 10
            [email] => cibooo@mai.com
        )

)

使用する機能

function permutations($lists) {
    $permutations = array();
    $iter = 0;
    while(true) {
        $num = $iter ++;
        $pick = array();
        foreach($lists as $l) {
            $r = $num % count($l);
            $num = ($num - $r) / count($l);
            $pick[] = $l[$r];
        }
        if ($num > 0)
            break;
        $permutations[] = $pick;
    }
    return $permutations;
}

ライブデモを見る

于 2013-07-17T09:32:56.000 に答える