如何将数组1维分组为数组2维?

时间:2016-07-18 07:32:38

标签: php arrays

我的阵列是这样的:

Array
(
    [0] => Array
        (
            [total_transaction] => 2000000
            [month] => May
        )

    [1] => Array
        (
            [total_transaction] => 1000000
            [month] => June
        )
)

我的阵列二是这样的:

Array
(
    [0] => 4000000
    [1] => 5000000
)

我想像这样对数组进行分组:

   Array
    (
        [0] => Array
            (
                [target] => 4000000
                [total_transaction] => 2000000
                [month] => May
            )

        [1] => Array
            (
                [target] => 5000000
                [total_transaction] => 1000000
                [month] => June
            )
    )

我仍然感到困惑

解决我问题的任何解决方案?

非常感谢

5 个答案:

答案 0 :(得分:2)

使用Pythonarray_map函数的解决方案:

array_replace_recursive

输出:

// $arrOne is your first array
$arrTwo = [4000000, 5000000];  // it's your second array

$arrTwo = array_map(function($v){ return ['target' => $v]; }, $arrTwo);
$result = array_replace_recursive($arrOne, $arrTwo);

print_r($result);

答案 1 :(得分:1)

尝试:

foreach($array1 as $key1=>$arr1) {
   if(isset($array2[$key1])) $array1[$key1]['target'] = $array2[$key1];
}

答案 2 :(得分:0)

<div id="carousel-example" class="carousel slide hidden-xs" data-ride="carousel">
    <div class="carousel-inner">
        <div class="item active">
            <div class="row">
            <?php foreach ($this->getItems() as $key=>$_item): // add $key in for loop
                if($key != 0 && $key % 4 == 0) { // just add this code - it will check if 4 records has been displayed re create row div...
                    echo '</div><div class="row">';
                }
            ?>
                <div class="col-sm-3">
                    <a class="product-image" href="<?php echo $_item->getProductUrl() ?>" title="<?php echo $this->escapeHtml($_item->getName()) ?>"><img src="<?php echo $this->helper('catalog/image')->init($_item, 'thumbnail')->resize(75); ?>" width="75" height="75" alt="<?php echo $this->escapeHtml($_item->getName()) ?>" /></a>
                    <div class="product-details">
                        <h3 class="product-name"><a href="<?php echo $_item->getProductUrl() ?>"><?php echo $this->escapeHtml($_item->getName()) ?></a></h3>
                        <?php echo $this->getPriceHtml($_item, true) ?>
                        <button type="button" title="<?php echo Mage::helper('core')->quoteEscape($this->__('Add to Cart')) ?>" class="button btn-cart" onclick="setLocation('<?php echo $this->getAddToCartUrl($_item) ?>')"><span><span><?php echo $this->__('Add to Cart') ?></span></span></button>
                        <ul class="add-to-links">
                            <?php if ($this->helper('wishlist')->isAllow()) : ?>
                                <li><a href="<?php echo $this->getAddToWishlistUrl($_item) ?>" class="link-wishlist"><?php echo $this->__('Add to Wishlist') ?></a></li>
                            <?php endif; ?>
                            <?php if($_compareUrl=$this->getAddToCompareUrl($_item)): ?>
                                <li><span class="separator">|</span> <a href="<?php echo $_compareUrl ?>" class="link-compare"><?php echo $this->__('Add to Compare') ?></a></li>
                            <?php endif; ?>
                        </ul>
                    </div>
                </div>
            <?php endforeach; ?>
            </div>
        </div>
    </div>
</div>

试试这个..

答案 3 :(得分:0)

试试这个,

server {
    listen 80 default_server;
listen [::]:80 default_server ipv6only=on;

    listen 443 ssl;

    server_name 192.168.1.10;
    keepalive_timeout 30;

# Config SSL
ssl_certificate /etc/nginx/ssl/nginx.crt;
    ssl_certificate_key /etc/nginx/ssl/nginx.key;
ssl_session_timeout 5m; 
ssl_session_cache  shared:SSL:10m;
ssl_protocols TLSv1 TLSv1.1 TLSv1.2;
ssl_ciphers "ECDHE-RSA-AES256-GCM-SHA384:ECDHE-RSA-AES128-GCM-SHA256:DHE-RSA-AES256-GCM-SHA384:DHE-RSA-AES128-GCM-SHA256:ECDHE-RSA-AES256-SHA384:ECDHE-RSA-AES128-SHA256:ECDHE-RSA-AES256-SHA:ECDHE-RSA-AES128-SHA:DHE-RSA-AES256-SHA256:DHE-RSA-AES128-SHA256:DHE-RSA-AES256-SHA:DHE-RSA-AES128-SHA:ECDHE-RSA-DES-CBC3-SHA:EDH-RSA-DES-CBC3-SHA:AES256-GCM-SHA384:AES128-GCM-SHA256:AES256-SHA256:AES128-SHA256:AES256-SHA:AES128-SHA:DES-CBC3-SHA:HIGH:!aNULL:!eNULL:!EXPORT:!DES:!MD5:!PSK:!RC4";
ssl_prefer_server_ciphers on;
add_header Strict-Transport-Security "max-age=31536000; includeSubdomains;";
    ssl_stapling on;

# Allow upload video up to 100M
    client_max_body_size 100M;

location ~* /.*\.(xml)$      {
        root /var/www/videos.techmaster.vn/public;
        expires 7d;
    }

    location /stream/* {
        proxy_set_header Upgrade $http_upgrade;
        proxy_set_header Connection "upgrade";
        proxy_http_version 1.1;
        proxy_set_header X-Forwarded-For $remote_addr;
        proxy_set_header X-Real-IP $remote_addr;
        proxy_set_header Host $host;
        proxy_pass http://127.0.0.1:3334;

        set $cors '';

        if ($http_origin ~* (localhost|tech\.dev)) {
                set $cors 'true';
        }

        if ($cors = 'true') {
                add_header 'Access-Control-Allow-Origin' "$http_origin";
                add_header 'Access-Control-Allow-Credentials' 'true';
                add_header 'Access-Control-Allow-Methods' 'GET, POST, PUT, DELETE, OPTIONS';
            add_header 'Access-Control-Allow-Headers' 'Origin, X-Requested-With, Content-Type, Accept, Range';
        }
    }

    location / {
        proxy_set_header Upgrade $http_upgrade;
        proxy_set_header Connection "upgrade";
        proxy_http_version 1.1;
        proxy_set_header X-Forwarded-For $remote_addr;
        proxy_set_header X-Real-IP $remote_addr;
        proxy_set_header Host $host;
        proxy_pass http://127.0.0.1:3333;

    set $cors '';

        if ($http_origin ~* (localhost|tech\.dev)) {
                set $cors 'true';
        }

    if ($cors = 'true') {
                add_header 'Access-Control-Allow-Origin' "$http_origin";
                add_header 'Access-Control-Allow-Credentials' 'true';
                add_header 'Access-Control-Allow-Methods' 'GET, POST, PUT, DELETE, OPTIONS';
            add_header 'Access-Control-Allow-Headers' 'Origin, X-Requested-With, Content-Type, Accept, Range';
    }
    }
}

https://3v4l.org/kBJVB

我希望它会有所帮助。

答案 4 :(得分:0)

$array1 = array(
    array(
        'total_transaction' => 2000000,
        'month' => 'May'
    ),
    array(
        'total_transaction' => 1000000,
        'month' => 'June'
    ));

$array2 = array(
'0' => 4000000,
'1' => 5000000
);

foreach($array2 as $k=>$v){
    $array1["$k"]['target']=$v;
}