Jeremy Love
Jeremy Love

Reputation: 179

Changing keys in new nested array

So I am trying to assign different keys to my array. I have the initial array and then I remove the duplicates and the concat the id's of the duplicates into the one array. What I dont understand is why the keys of my arrays are the same as my fields

This is the current output of my array. Also for some reason, it is adding an empty array at the end and I dont know why.

Array
(
    [0] => Array
        (
            [4.2GHz quad-core Intel Core i7 Turbo Boost up to 4.5GHz] => Array
                (
                    [field] => 4.2GHz quad-core Intel Core i7 Turbo Boost up to 4.5GHz
                    [product_id] => 156 157
                )

            [3.4GHz quad-core Intel Core i5 Turbo Boost up to 3.8GHz] => Array
                (
                    [field] => 3.4GHz quad-core Intel Core i5 Turbo Boost up to 3.8GHz
                    [product_id] => 158
                )

        )

    [1] => Array
        (
            [1TB SSD] => Array
                (
                    [field] => 1TB SSD
                    [product_id] => 156
                )

            [2TB SSD] => Array
                (
                    [field] => 2TB SSD
                    [product_id] => 157
                )

            [1TB Fusion Drive] => Array
                (
                    [field] => 1TB Fusion Drive
                    [product_id] => 158
                )

        )

    [2] => Array
        (
        )

)

This is my code.

$categories = array();
$new_values = array();

foreach($recs as $key => $rec){
    if($rec['processors']) {
        $categories['processors']['fields'][] = array('field' => $rec['processors'], 'product_id' => $rec['product_id']);
    }
    if($rec['storage']) {
        $categories['storage']['fields'][] = array('field' => $rec['storage'], 'product_id' => $rec['product_id']);
    }
    if($rec['memory']) {
        $categories['memory']['fields'][] = array('field' => $rec['memory'], 'product_id' => $rec['product_id']);
    }
    if($rec['graphics']) {
        $categories['graphics']['fields'][] = array('field' => $rec['graphics'], 'product_id' => $rec['product_id']);
    }
    if($rec['colors']) {
        $categories['colors']['fields'][] = array('field' => $rec['colors'], 'product_id' => $rec['product_id']);
    }
}

foreach($categories as $key => $fields) {
    foreach($fields['fields'] as $field) {
        if (isset($new_values[$key][$field['field']])) {
            $temp = $new_values[$key][$field['field']];
            $temp['product_id'] .= ' ' . $field['product_id'];
            $new_values[$key][$field['field']] = $temp;
        } else {
            if($field['field']) {
                $new_values[$key][$field['field']] = $field;
            }
        }
    }
}

$new_values = array_values($new_values);

This is the array I am trying to accomplish with what I am doing. I have some idea on doing it by using $key in my loop but I cannot quite narrow it down.

Array
(
    [processors] => Array
        (
            [0] => Array
                (
                    [field] => 4.2GHz quad-core Intel Core i7 Turbo Boost up to 4.5GHz
                    [product_id] => 156 157
                )

            [1] => Array
                (
                    [field] => 3.4GHz quad-core Intel Core i5 Turbo Boost up to 3.8GHz
                    [product_id] => 158
                )

        )

    [storage] => Array
        (
            [0] => Array
                (
                    [field] => 1TB SSD
                    [product_id] => 156
                    [type] => storage
                )

            [1] => Array
                (
                    [field] => 2TB SSD
                    [product_id] => 157
                )

            [2] => Array
                (
                    [field] => 1TB Fusion Drive
                    [product_id] => 158
                )

        )
)

Any suggestions is greatly appreciated.

Edit: Made a sandbox for testing. Made a sandbox for testing http://sandbox.onlinephpfunctions.com/code/2a515da95e88c5470a7fe850967d4a94920ccbcf

Upvotes: 0

Views: 54

Answers (1)

Nigel Ren
Nigel Ren

Reputation: 57141

You are using array_values() on the top level in...

$new_values = array_values($new_values);

and not in the level underneath (the actual item names)...

foreach($new_values as $key => $fields) {
    $new_values[$key] = array_values($fields);
}

Upvotes: 1

Related Questions