dongzhong1929 2018-01-09 10:50
浏览 23
已采纳

如何使用php匹配,合并和创建一个新数组

This is new for me(and arrays are not my kind of thing), but not sure if this is possible as I could not find anything examples online but I have 2 arrays with data, and these arrays contain a value called 'slug', which have to merge/combined(category/product). These slugs need to match with the value called 'language' so that we can build a new array with these new values.

// first array:

Array(
[0] => Array
    (
        [category_id] => 5
        [attribute] => slug
        [language] => de
        [translation] => blanko
    )

[1] => Array
    (
        [category_id] => 5
        [attribute] => slug
        [language] => en
        [translation] => white
    )

[2] => Array
    (
        [category_id] => 5
        [attribute] => slug
        [language] => es
        [translation] => blanco
    )

[3] => Array
    (
        [category_id] => 5
        [attribute] => slug
        [language] => fr
        [translation] => vierges
    )

[4] => Array
    (
        [category_id] => 5
        [attribute] => slug
        [language] => it
        [translation] => bianche
    )
)

// second array:

Array(
[0] => Array
    (
        [product_id] => 5
        [attribute] => slug
        [language] => de
        [translation] => ettiketten-xxx
    )

[1] => Array
    (
        [product_id] => 5
        [attribute] => slug
        [language] => en
        [translation] => labels-xxxx
    )

[2] => Array
    (
        [product_id] => 5
        [attribute] => slug
        [language] => es
        [translation] => etiquetas-xxxx
    )

[3] => Array
    (
        [product_id] => 5
        [attribute] => slug
        [language] => fr
        [translation] => etiquettes-xxx
    )

[4] => Array
    (
        [product_id] => 5
        [attribute] => slug
        [language] => it
        [translation] => etichette-xxxx
    )
)

I want to make a new array like

array(
   'de' => 'blanko/ettiketten-xxx',
   'fr' => 'vierges/etiquettes-xxx'
    // and so on
)
  • 写回答

1条回答 默认 最新

  • doutao4480 2018-01-09 11:27
    关注

    Here's a naive example of how to iterate through these and compile a third array with your desired values:

    $result = array();
    
    foreach ($categories as $category) {
        foreach ($products as $product){
            if ($category['language'] == $product['language']){
                $translationString = $category['translation']."/".$product['translation'];
                $result[$category['language']] = $translationString;
            }
        }
    }
    
    echo print_r($result);
    

    This will give you an array like:

    Array(
       'de' => 'blanko/ettiketten-xxx',
       'en' => 'white/labels-xxxx',
       'es' => 'blanco/etiquetas-xxxx,
       'fr' => 'vierges/etiquettes-xxx'
    )
    

    You won't win any points for style though, there is probably a better way to do this using array_map or array_walk so maybe something to have a look at if you need to scale out

    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论

报告相同问题?

悬赏问题

  • ¥15 CVRP 图论 物流运输优化
  • ¥15 Tableau online 嵌入ppt失败
  • ¥100 支付宝网页转账系统不识别账号
  • ¥15 基于单片机的靶位控制系统
  • ¥15 真我手机蓝牙传输进度消息被关闭了,怎么打开?(关键词-消息通知)
  • ¥15 下图接收小电路,谁知道原理
  • ¥15 装 pytorch 的时候出了好多问题,遇到这种情况怎么处理?
  • ¥20 IOS游览器某宝手机网页版自动立即购买JavaScript脚本
  • ¥15 手机接入宽带网线,如何释放宽带全部速度
  • ¥30 关于#r语言#的问题:如何对R语言中mfgarch包中构建的garch-midas模型进行样本内长期波动率预测和样本外长期波动率预测