0

I have an array that contains 3 products with attributes of "color" and "size" and the products are identified by a number ( ['code'] ). My problem is that when i pull the data from the database I get this array that is in 6 pieces because "color" and "size" get stored in separate arrays.

My question is, how do I generate the data into an array of these 3 products with all their data in the same array.

Array(

   [0] => Array
       (
        [code] => 123
        [name] => box
        [stock] => 2.00
        [price] => 10.00
        [color] => brown
    )

[1] => Array
    (
        [code] => 123
        [name] => box
        [stock] => 2.00
        [price] => 10.00
        [size] => L
    )

[2] => Array
    (
        [code] => 1234
        [name] => box
        [stock] => 3.00
        [price] => 11.00
        [color] => brown
    )

[3] => Array
    (
        [code] => 1234
        [name] => box
        [stock] => 3.00
        [price] => 11.00
        [size] => XL
    )

[4] => Array
    (
        [code] => 12345
        [name] => box
        [stock] => 4.00
        [price] => 12.00
        [size] => XL
    )

[5] => Array
    (
        [code] => 12345
        [name] => box
        [stock] => 4.00
        [price] => 12.00
        [color] => gray
    )

)

Expected output:

[0] => Array
    (
    [code] => 123
    [name] => box
    [stock] => 4.00
    [price] => 12.00
    [color] => gray
    [size] => XL
)

What i'm looking to do is just combine the doubled array into one. Dont want to mess with SQL anymore - it gets attribute name in one table, attribute values from another table, code,stock,price from another table, name from another table. I know something can be done with just this array even if it will be just a temporary solution.

4
  • 3
    Please add your SQL query / result gathering, since the errors root lies in your query. Commented Jan 26, 2018 at 15:51
  • I'm asking strictly how to generate the data from the given array. No SQL. Commented Jan 26, 2018 at 15:55
  • 1
    That's a short-sighted way to look at this problem. It can probably be solved by modifying the query that is fetching the data, which would be the cleaner solution. Commented Jan 26, 2018 at 15:56
  • What is the expected output? What have you tried? Commented Jan 26, 2018 at 15:57

2 Answers 2

2

You can do this:

assuming your array is $products

$merged = array();

foreach($products as $product) {
    if (!isset($sorted[$product['code']])) {
        $merged[$product['code']] = $product;
    } else {
        $merged[$product['code']] = array_merge($sorted[$product['code']], $product);
    }
}

Use the product code as array key and then merge the array.

Sign up to request clarification or add additional context in comments.

1 Comment

This was the most helpful part. I used the first part of @Phillip Maurer s answer. Because the values were not in order I changed the ORDER BY sql clause and I got the arrays in order 123,1234,12345,123,1234,12345 combined the data with a modified version of the answer above and used your code to get the final result (array with 3 products and all data).
1

If the two rows are always direct successors, with the first one holding the color field, while the latter holes the size field, you can make it easily by iterating over them in a for loop:

$maxCount = count($array);
for ($i = 1; $i < $maxCount; $i += 2) {
    $array[$i - 1]['size'] = $array[$i]['size'];
    unset($array[i]);
}

This will iterate over each second element of the array and add the size field to the preceding field.

If you need to have succeeding array keys afterwards you can call $arry = array_values($array);.


In case the the associated rows might not be successors you need to map them based on their code field (in case thats the primary key). You can use array_reduce() for that:

$desiredOutput = array_reduce($array, function($output, $element) {
    if (!array_key_exists($element['code'], $output)) {
        $output[$element['code']] = $element;
    } elseif (array_key_exists('size', $element)) {
        $output[$element['code']]['size'] = $element['size'];
    } elseif (array_key_exists('color', $element)) {
        $output[$element['code']]['color'] = $element['color'];
    }
    return $output;
}, []);

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.