0

Here is the thing I trying deal with I have array which looks like this and have duplicates

    $products = [
[
    "product_name" => "Adidas1",
    "address" => "street 2"
],
[
    "product_name" => "Adidas2",
    "address" => "street 2"
],
[
    "product_name" => "Adidas3",
    "address" => "street 2"
],
[
    "product_name" => "Adidas4",
    "address" => "street 2"
],
[
    "product_name" => "Nike1",
    "address" => "street name1"
],
[
    "product_name" => "Nike2",
    "address" => "street name1"
]];

Result that I need to get is below . I did try different ways to do it but still can bring it to the finel result that have to come up

$final_result = [
[
    "address" => "street 2",
    "products" => [
        "addidas1",
        "addidas2",
        "addidas3",
        "addidas4",
        
    ]
],
[
    "address" => "street name1",
    "products" => [
        "Nike1",
        "Nike2",
       

    ]
]

any suggestion how to do it ?

here is my best solution that I tried

$stor_sorted = array();
foreach ($products as $product) {
    if (array_count_values($product) > 1) {
        $stor_sorted[] = ["address" => $product['address'], "items" => [$product['product_name']]];
    }
}
0

2 Answers 2

0

try this code

$products = [
        [
            "product_name" => "Adidas1",
            "address" => "street 2"
        ],
        [
            "product_name" => "Adidas2",
            "address" => "street 2"
        ],
        [
            "product_name" => "Adidas3",
            "address" => "street 2"
        ],
        [
            "product_name" => "Adidas4",
            "address" => "street 2"
        ],
        [
            "product_name" => "Nike1",
            "address" => "street name1"
        ],
        [
            "product_name" => "Nike2",
            "address" => "street name1"
        ]];

    $final_result = [];
    foreach ($products as $pr){
        $original_key = array_search($pr['address'], array_column($final_result, 'address'), true);
        if($original_key === false){
            $temp_array['address'] = $pr['address'];
            $temp_array['products'] =  [$pr['product_name']];
            $final_result[] =$temp_array;
        }else{
            $final_result[$original_key]['products'][] = $pr['product_name'];
        }
    }

your result will be in final_result array

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

1 Comment

Please never post code-only answers on Stack Overflow. Performing potentially full array scans in a loop is not best practice.
0

Use address values as temporary keys in your result array.

When an address is encountered for the first time, cast the product name as an array within the row and store the row.

On subsequent encounters, merely push the product name into the group's subarray.

When finished, re-index the result with array_values().

Code: (Demo)

$result = [];
foreach ($products as $row) {
    if (!isset($result[$row['address']])) {
        $row['product_name'] = (array)$row['product_name'];
        $result[$row['address']] = $row;
    } else {
        $result[$row['address']]['product_name'][] = $row['product_name'];
    }
}
var_export(array_values($result));

Same output with a body-less foreach() using array destructuring: (Demo)

$result = [];
foreach (
    $products
    as
    [
        'address' => $key,
        'address' => $result[$key]['address'],
        'product_name' => $result[$key]['product_name'][]
    ]
);
var_export(array_values($result));

Same output with functional style programming and no new globally-scoped variables: (Demo)

var_export(
    array_values(
        array_reduce(
            $products,
            function($result, $row) {
                $result[$row['address']]['product_name'] = $row['product_name'];
                $result[$row['address']]['address'][] = $row['address'];
                return $result;
            }
        )
    )
);

1 Comment

@some why have you accepted a less performant and unexplained answer? Do you not understand my answer?

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.