2016-02-23 77 views
-2

我需要第二阵列的某些值追加到所述第一阵列分配一个数组的值到另一个不合并

即,

在第一阵列我创建两个元素,如nameprice和从第二个数组获取值,并给它第一阵列

这是我的第一个数组

[ 
    { 
    "id": 8, 
    "user_id": 21, 
    "category_id": 1, 
    "juice_id": 2, 
    "count": "100", 
    "status": "1", 
    "created_at": "2016-02-23 07:12:14", 
    "updated_at": "2016-02-23 07:12:14" 
    }, 
    { 
    "id": 9, 
    "user_id": 21, 
    "category_id": 1, 
    "juice_id": 1, 
    "count": "100", 
    "status": "1", 
    "created_at": "2016-02-23 07:15:47", 
    "updated_at": "2016-02-23 07:15:47" 
    } 
] 

和第二A rray是

{ 
    "1": { 
    "id": 2, 
    "store_id": 1, 
    "category_id": 1, 
    "name": "Cashew Butter Baby", 
    "image": "http://greenhoppingbucket.s3.amazonaws.com/juice/1455719144rebV3iRUlj.png", 
    "description": "Cashew, Butter and Milk", 
    "price": "12.00", 
    "status": 1, 
    "created_at": "2016-02-17 19:56:11", 
    "updated_at": "2016-02-17 19:56:11" 
    }, 
    "2": { 
    "id": 1, 
    "store_id": 1, 
    "category_id": 1, 
    "name": "Kalekolada", 
    "image": "http://greenhoppingbucket.s3.amazonaws.com/juice/1455719105WAVB3SGxT7.png", 
    "description": "Pulp of Kale", 
    "price": "10.00", 
    "status": 1, 
    "created_at": "2016-02-17 19:55:34", 
    "updated_at": "2016-02-17 19:55:34" 
    } 
} 

我的预期结果是

[ 
    { 
    "id": 8, 
    "user_id": 21, 
    "category_id": 1, 
    "juice_id": 2, 
    "count": "100", 
    "status": "1", 
    "created_at": "2016-02-23 07:12:14", 
    "updated_at": "2016-02-23 07:12:14" 
    "name": "Cashew Butter Baby", 
    "price": "12.00", 
    }, 
    { 
    "id": 9, 
    "user_id": 21, 
    "category_id": 1, 
    "juice_id": 1, 
    "count": "100", 
    "status": "1", 
    "created_at": "2016-02-23 07:15:47", 
    "updated_at": "2016-02-23 07:15:47", 
    "name": "Kalekolada", 
    "price": "10.00", 

    } 
] 

我试图做$newArray = array_merge($cartData, $juiceData);

但它只是合并两个数组。

什么是错误,我该怎么做?

+1

您如何知道第二个数组中的哪个部分与第一个数组中的条目一起使用?我没有看到任何将它们连接在一起的东西。 – jeroen

+0

在第一个数组中'juice_id'和在第二个数组''id是常见的..有这种关系是可以映射它 –

+0

不是根据您的预期结果... – jeroen

回答

0

下面是合并这些数组的一些代码。你应该有一个共同的索引,但(不同的顺序可能会导致意外的行为)。

<?php 

$a = array(
    array(
     "id" => 8, 
     "user_id" => 21, 
     "category_id" => 1, 
     "juice_id" => 2, 
     "count" => "100", 
     "status" => "1", 
     "created_at" => "2016-02-23 07:12:14", 
     "updated_at" => "2016-02-23 07:12:14" 
    ), 
    array(
     "id" => 9, 
     "user_id" => 21, 
     "category_id" => 1, 
     "juice_id" => 1, 
     "count" => "100", 
     "status" => "1", 
     "created_at" => "2016-02-23 07:15:47", 
     "updated_at" => "2016-02-23 07:15:47" 
    ) 
); 

$b = array(
    array(
     "id" => 2, 
     "store_id" => 1, 
     "category_id" => 1, 
     "name" => "Cashew Butter Baby", 
     "image" => "http://greenhoppingbucket.s3.amazonaws.com/juice/1455719144rebV3iRUlj.png", 
     "description" => "Cashew, Butter and Milk", 
     "price" => "12.00", 
     "status" => 1, 
     "created_at" => "2016-02-17 19:56:11", 
     "updated_at" => "2016-02-17 19:56:11" 
    ), 
    array(
     "id" => 1, 
     "store_id" => 1, 
     "category_id" => 1, 
     "name" => "Kalekolada", 
     "image" => "http://greenhoppingbucket.s3.amazonaws.com/juice/1455719105WAVB3SGxT7.png", 
     "description" => "Pulp of Kale", 
     "price" => "10.00", 
     "status" => 1, 
     "created_at" => "2016-02-17 19:55:34", 
     "updated_at" => "2016-02-17 19:55:34" 
    ) 
); 

// For this to work the arrays need to have the same string keys 
// $merge = array_merge_recursive($a, $b); 

$merge = array(); 
foreach($b as $key => $entry) { 
    if(isset($a[$key])) { 
     $entry += $a[$key]; 
    } 

    $merge[] = $entry; 
} 

var_dump($merge); 

编辑:

在评论新的信息可以给我们这样的:

$categories = array(); 
foreach($b as &$entry) { 
    $categories[$entry['id']] =& $entry; 
} 

$products = array(); 
foreach($a as &$entry) { 
    if(isset($categories[$entry['juice_id']])) { 
     $entry['category'] =& $categories[$entry['juice_id']]; 
    } 

    $products[] =& $entry; 
} 

var_dump($products); 

无需按引用传递他们,但应该节省一些内存。

+0

谢谢,两个数组之间的共同因素是...... First Array的'juice_id'和第二个数组的'id'是相同的。答案是否匹配? –

+0

如果应用程序使用关系数据库,则应该真正考虑使用JOIN。 –

相关问题