vincej's avatar
Level 15

Trying to merge two collections into one single collection with ONE array

Hi!

The title says it all. My problem is that the end result is a merged collection with TWO arrays. This does not work for me as the merged array is being returned to an Ajax function which I assume needs to see a only a single array.

So - how do I get this into one collection with one single key? Many Thanks !

Code So Far

 public function product_prices($term)
    {
        $product = DB::table('products')
            ->join('categories','categories.category_id','=','products.category_id')
            ->select('product_id', 'price', 'products.product_name', 'products.category_id','categories.parent_id')
            ->where([
            ['product_name', 'LIKE', "$term" . '%'],
            ['products.status', '=', 'active'],
            ])
            ->get();

        $parent =  DB::table('categories')
            ->select('parent_id','name')
            ->where('category_id','=', $product[0]->parent_id )
            ->get();

        $productParent = $product->merge($parent);
        $result = $productParent->flatten()->unique();
        $merged = $result->all();
        echo json_encode($merged);
    }

Delivers this Result

0 = {stdClass} 
 product_id = "abc6798"
 price = "100.00"
 product_name = "Install Rubber"
 category_id = {int} 60
 parent_id = "58"
1 = {stdClass} 
 parent_id = "57"
 name = "Installation"

0 likes
16 replies
tykus's avatar

How is that result two arrays?

vincej's avatar
Level 15

@tykus Sorry - you are right, I mangled that. What I mean is I want it under one single Key.

vincej's avatar
Level 15

@tykus I'm not using the correct vocabulary ... I don't mean "'key" exactly .... just everything under 0

tykus's avatar

@vincej I don't know what you mean???

Aside, whenever you are json encoding a PHP array; and you want a JSON array you must ensure that (i) the array is 0-indexed and (ii) the indexes are sequential 0, 1, 2, 3...

vincej's avatar
Level 15

@tykus Ok - no problem, I'm explaining myself very badly. I want the final result to look like this, all under the single index of 0:

0 = {stdClass} 
 product_id = "abc6798"
 price = "100.00"
 product_name = "Install Rubber"
 category_id = {int} 60
 parent_id = "58"
parent_id = "57"
 name = "Installation"
vincej's avatar
Level 15

@tykus if I can't achieve that, then somehow I need to get all the data in a form that I can pass back to an Ajax call.

tykus's avatar

@vincej that won't work; you have two Collections each with multiple items and items will have the same keys (which should be unique in the result), for example there are two parent_id keys in your desired result:

 parent_id = "58"
parent_id = "57"
vincej's avatar
Level 15

@tykus Ok understood. Let me rephrase the problem then.

If I pass back a result to AJAX, which is made up of two indexes will Ajax puke? and how can I get access to both indices ?

0 = {stdClass} 
 product_id = "abc6798"
 price = "100.00"
 product_name = "Install Rubber"
 category_id = {int} 60
 parent_id = "58"
1 = {stdClass} 
 parent_id = "57"
 name = "Installation"

tykus's avatar

@vincej if it is JSON array; then you can index into it as you would any array, or iterate???

I don't recognise the format of the code snippet; where do you see this?

vincej's avatar
Level 15

@tykus I'm not sure what you are asking: "I don't recognise the format of the code snippet; where do you see this?"

The above result came from this code below:

 public function product_prices($term)
    {
        $product = DB::table('products')
            ->join('categories','categories.category_id','=','products.category_id')
            ->select('product_id', 'price', 'products.product_name', 'products.category_id','categories.parent_id')
            ->where([
            ['product_name', 'LIKE', "$term" . '%'],
            ['products.status', '=', 'active'],
            ])
            ->get();

        $parent =  DB::table('categories')
            ->select('parent_id','name')
            ->where('category_id','=', $product[0]->parent_id )
            ->get();

        $productParent = $product->merge($parent);
        $result = $productParent->flatten()->unique();
        $merged = $result->all();
        echo json_encode($merged);
    }



tykus's avatar
tykus
Best Answer
Level 104

@vincej so echo json_encode($merged); produces this:

0 = {stdClass} 
 product_id = "abc6798"
 price = "100.00"
 product_name = "Install Rubber"
 category_id = {int} 60
 parent_id = "58"
1 = {stdClass} 
 parent_id = "57"
 name = "Installation"

and not this (which is JSON):

[
  {
      "product_id": "abc6798",
      "price": "100.00",
      "product_name": "Install Rubber",
      // etc...
  },
  {
     "parent_id": 57,
     // etc
  }
]
vincej's avatar
Level 15

@tykus Hi Tykus, Sorry - I really mangled my question. Thanks for sticking with it. At the outset, I made the foolish assumption that any json being returned to Ajax had to be built on a single index. Yes, I tried your answer and indeed everything works on two indices. Best reply reward. Cheers !

vincej's avatar
Level 15

@tykus - sorry I have to break for dinner. I live in Mountain Time .... Calgary Canada to be exact.

Please or to participate in this conversation.