-1

I have two array like this:

Array(
     [0] => (
         [id] => 1,
         [order_id] => 1,
         [image_url] => /resources/filename1
         ),
     [1] => (
         [id] => 2,
         [order_id] => 1,
         [image_url] => /resources/filename2
         )
)

I want to merge imge_url in single array because of order_id, one order_id have multiple images.

I am expecting array like this:

Array(
    [id] => 1,
    [order_id] => 1,
    [image_url] => (
        [0] => "/resources/filename1",
        [1] => "/resources/filename2"
    )
)
Pradeep
  • 9,667
  • 13
  • 27
  • 34
Saad Ahmed
  • 700
  • 1
  • 8
  • 15
  • 1
    This question lacks any code to show us that you've made any attempt to solve this yourself. If you've tried something, show us what you've tried, example of the expected output and what you're actually getting. If you _haven't_ tried anything, you need to do that before posting. We can help you with your _existing_ code, but we won't write it for you. Please read: [How to create a Minimal, Complete, and Verifiable example](http://stackoverflow.com/help/mcve) and also [How do I ask a good question?](http://stackoverflow.com/help/how-to-ask) – M. Eriksson Jun 03 '18 at 09:47
  • 1
    Possible duplicate of [Combine two arrays](https://stackoverflow.com/questions/6535444/combine-two-arrays) – Tom Jun 03 '18 at 09:51
  • Do you mean you want to modify the content of your array and not combine two arrays? – Tamas Szoke Jun 03 '18 at 09:56
  • yes @TamasSzoke i want to modify arrray – Saad Ahmed Jun 03 '18 at 10:09

2 Answers2

1

Ok, I am sorry my previous answer was not right, this one does, I assume a couple of things:

  • Your input can be more than two rows and can have more than one order ID
  • I am assigning to ID the first ID with that order ID (if not you can just only assign the ID key.

Here is the code:

$arrays = [
    [
        "id" => 1,
        "order_id" => 1,
        "image_url" => "/resources/filename1"
    ],
    [
        "id" => 2,
        "order_id" => 1,
        "image_url" => "/resources/filename2"
    ],
    [
        "id" => 3,
        "order_id" => 1,
        "image_url" => "/resources/filename3"
    ],
        [
        "id" => 4,
        "order_id" => 2,
        "image_url" => "/resources/filename4"
    ],
];

foreach($arrays as $array) {
    if(!isset($result[$array['order_id']]['id'])) {
        $result[$array['order_id']]['id']=$array['id'];
    }
    $result[$array['order_id']]['order_id']=$array['order_id'];
    $result[$array['order_id']]['image_url'][]=$array['image_url'];
}
print_r($result);

Tested here.

L. Amigo
  • 384
  • 1
  • 10
0

Hope this will help you :

Working demo : https://eval.in/1014350

$array = array(array(
     'id' => 1,
     'order_id' => 1,
     'image_url' => '/resources/filename1'
     ),array(
     'id' => 2,
     'order_id' => 1,
     'image_url' => '/resources/filename2'
     )
);

foreach ($array as $key => $item) {
  if ($item['id'] == 1)
  {
    $data['id'] = $item['id'];
  }
  $data['order_id'] = $item['order_id'];
  $data['image_url'][$key] = $item['image_url'];
}

print_r($data);

Program Output

Array
(
    [id] => 1
    [order_id] => 1
    [image_url] => Array
        (
            [0] => /resources/filename1
            [1] => /resources/filename2
        )

)
Pradeep
  • 9,667
  • 13
  • 27
  • 34
  • In my opinion, if the OP shows no attempt of trying to solve the issue themselves, the question is off-topic and should therefor not be answered. Doing that will make the OP and future visitors just keep asking and SO will become a free coding service (which it isn't). – M. Eriksson Jun 03 '18 at 10:09
  • 1
    @Magnus Eriksson because i gave an answer that is why you downvote me, i think its a question and answer site, next time i will not answer such question – Pradeep Jun 03 '18 at 10:20
  • Actually, I haven't down voted this answer since it _does_ answer the question. My comment was just to point out the risk of answering off-topic questions. Yes, it's a Q&A site, but it does have have guidelines that states that you should include your attempts when asking a question. The site is about helping people with issues with their (existing) code. It's not a site for "I want this" and we just give them code. – M. Eriksson Jun 03 '18 at 10:21
  • 2
    for downvoter its fault from askers who ask such questions so downvote them , pls don't downvote well given answers – Pradeep Jun 03 '18 at 10:26