【问题标题】:Assign the values of one array to another not merging将一个数组的值分配给另一个不合并的数组
【发布时间】:2016-06-04 23:31:01
【问题描述】:

我需要将第二个数组的某些值附加到第一个数组

即,

在第一个数组中,我创建了两个元素,例如 name 和 price,并从第二个数组中获取值并将其提供给第一个数组

这是我的第一个数组

[
  {
    "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"
  }
]

第二个数组是

{
  "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);

但它只是合并两个数组。

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

【问题讨论】:

  • 您如何知道第二个数组中的哪个部分与第一个数组中的条目一起使用?我没有看到将它们联系在一起的任何东西。
  • 在第一个数组juice_id 和第二个数组id 中很常见.. 有了这种关系就可以映射它
  • 不符合您的预期结果...
  • @SulthanA,将您的数组格式化为有效形式,而不是对象数组或 json 内容。重新格式化并显示最终正确的数组
  • @jeroen 第一个数组有juice_id 等于第二个数组的id .. 这样我就关联了两个数组..

标签: php arrays laravel multidimensional-array array-merge


【解决方案1】:

这里有一些用于合并这些数组的代码。不过,您应该为它们提供一个通用索引(不同的顺序可能会导致意外行为)。

<?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 );

编辑:

cmets 中的新信息会为我们提供如下信息:

$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 );

不需要通过引用传递它们,但应该节省一些内存。

【讨论】:

  • 谢谢,两个数组的公因数是……第一个数组的juice_id 和第二个数组的id 是一样的……答案会匹配吗??
  • 如果应用程序使用的是关系数据库,您应该真正考虑使用 JOIN。
猜你喜欢
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2021-04-23
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2013-09-28
  • 1970-01-01
相关资源
最近更新 更多