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 个图像类型 一封电子邮件会发生什么?我建议您使用排列来生成不同的可能性,而不是仅仅填充元素。

我是什么意思?

$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 回答
0

很多假设:
假设总是有相同数量的图像类型,以及具有相同索引的链接,
假设总是有一封电子邮件,以及所有其他属性,

您可以只遍历图像类型(本质上就是您要拆分的内容)。

例如:

//using your original array as $input:
$result = array();
$result["images"] =  array();

$count = 0;
foreach($input["imagetype"] as $image_type) {
        $result["images"][] = array(
            "owner" => $input['owner'],
            "add_owner" => $input['add_owner'],
            "link" => $input['link'][$count],
            "imagetype" => $image_type,
            "email" => $input['email']
        );
    ++$count;
}

print_r($result);
于 2013-07-17T09:35:49.373 回答