PHP array merging [on hold]

186
June 03, 2018, at 8:30 PM

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"
    )
)
Answer 1

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
        )
)
Answer 2

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.

Rent Charter Buses Company
READ ALSO
How to display missing data interval and group by column name if all intervals are missing

How to display missing data interval and group by column name if all intervals are missing

Please help meI have 3 tables, all records of my elements table must have a complete interval

165
Php function call from another function

Php function call from another function

I am trying to make a text game in PHP but i have problem since i am programing in php for few daysI need to call function attack (I need $_mainAttack since it is combination of $_baseAttack and special attack) from defend function so i can calculate...

210
Send custom e-mail in woocommerce checkout page

Send custom e-mail in woocommerce checkout page

I have published a website for a friend and customer who sells products for children

165