-2

I need to append certain values of Second array to the first array

I.e.,

In the First Array i create two elements such as name and price and get the values from Second Array and give it to first array

Here is my First 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"
  },
  {
    "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"
  }
]

And the Second array is

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

My Expected result is

[
  {
    "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",

  }
]

I tried to do $newArray = array_merge($cartData, $juiceData);

But it is simply merging two arrays.

What is the mistake and how can i do that ?

SA__
  • 437
  • 3
  • 7
  • 13
  • 1
    How would you know which section in the second array to use with an entry in the first array? I don't see anything that links them together. – jeroen Feb 23 '16 at 08:25
  • in the First array `juice_id` and in the Second Array `id` are common .. With that relation is that possible to map it – SA__ Feb 23 '16 at 08:30
  • Not according to your expected result... – jeroen Feb 23 '16 at 08:32
  • @SulthanA, format your arrays to a valid form, not array of objects or json content. Reformat and show the final proper arrays – RomanPerekhrest Feb 23 '16 at 08:32
  • @jeroen The First array have `juice_id` which is equal to Second Array's `id` .. in that way i am relating both array.. – SA__ Feb 23 '16 at 08:36
  • @RomanPerekhrest Okay, i will update it now – SA__ Feb 23 '16 at 08:36

1 Answers1

0

Here's some code for merging those arrays. You should have a common index for them though (a different order may lead to unexpected behavior).

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

EDIT:

New info in comments would give us something like:

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

No need to pass them by reference but should save some memory.

Niclas Larsson
  • 1,317
  • 8
  • 13
  • Thanks, the common factor between two arrays are.. First Array's `juice_id` and Second Array's `id` are same.. Will the answer match that ?? – SA__ Feb 23 '16 at 08:38
  • If the application is using a relational database you should really consider a JOIN instead. – Niclas Larsson Feb 23 '16 at 08:40