0

I'm in need of some php superstars to convert this array :D Here's my current output.

Array(
[0] => Array(
    [name] => S, M, L
    [id] => 168, 169, 170
)

[1] => Array(
    [name] => M, L
    [id] => 169, 170
)

[2] => Array(
    [name] => S
    [id] => 168
)

[3] => Array(
    [name] => S, M
    [id] => 168, 169
)

)

The desired output from code above.

Array(
[0] => Array(
    [name] => S,
    [id] => 168
)

[1] => Array(
    [name] => M,
    [id] => 169
)

[2] => Array(
    [name] => L
    [id] => 170
)

)

Thank you in advanced!

3
  • What if one of the arrays has a different id for a given name? It looks like that data came from an SQL query, why not just fetch the id/name pairs using another query on the same database? Commented Oct 29, 2020 at 2:21
  • These values are GROUP_CONCAT(), but yes a SQL query. One product has multiple sizes/id to which I'm planning to use the converted array to filter products. Commented Oct 29, 2020 at 2:23
  • So then you must have a table which has those id and name pairs in them, and you should just select from that. Otherwise, if you had no products with name = 'M', you might miss a value in your output array. Commented Oct 29, 2020 at 2:24

2 Answers 2

1

Work with PHP 7.3.

source of test7.php

<?php
$items = [
    [
        'name' => 'S , M,   L',
        'id' => '168,169,170',
    ],
    [
        'name' => 'M,L,XL',
        'id' => '170,171,182',
    ],
];
$res = [];
foreach ($items as $itemOne) {
    // check
    if (!isset($itemOne['name']) && !isset($itemOne['id'])) {
        continue;
    }
    $names = explode(',', $itemOne['name']);
    $names = array_map('trim', $names);
    $ids = explode(',', $itemOne['id']);
    $ids = array_map('trim', $ids);
    foreach ($names as $index => $name) {
        // check
        if (!isset($ids[$index])) {
            continue;
        }
        // non uniq id
        // $res[] = [
        // uniq id, duplicate values will be overwritten
        $res[$ids[$index]] = [
            'name' => $name,
            'id' => $ids[$index],
        ];
    }
}
var_dump($res);

Output:

% php test7.php
array(5) {
  [168]=>
  array(2) {
    ["name"]=>
    string(1) "S"
    ["id"]=>
    string(3) "168"
  }
  [169]=>
  array(2) {
    ["name"]=>
    string(1) "M"
    ["id"]=>
    string(3) "169"
  }
  [170]=>
  array(2) {
    ["name"]=>
    string(1) "M"
    ["id"]=>
    string(3) "170"
  }
  [171]=>
  array(2) {
    ["name"]=>
    string(1) "L"
    ["id"]=>
    string(3) "171"
  }
  [182]=>
  array(2) {
    ["name"]=>
    string(2) "XL"
    ["id"]=>
    string(3) "182"
  }
}
Sign up to request clarification or add additional context in comments.

1 Comment

BRO! Much appreciated.
0

Just take all the exploded values in an array then array_unique will take care the duplicates.

$data = [
    [
        'name'=>'S, M, L',
        'id' => '168, 169, 170'
    ],

    [
        'name'=>'M, L',
        'id' => '169, 170'
    ],
    [
        'name'=>'S',
        'id' => '168'
    ],
    [
        'name'=>'S, M',
        'id' => '168, 169'
    ],
];

$result = [];

foreach ($data as $each ) {
    $names = explode( ',', str_replace(' ','',$each['name']));
    $ids = explode( ',', str_replace(' ', '', $each['id']) );
    
    array_walk( $names , function ( $name, $key ) use ( $ids, &$result )
    {
        $result[]  = [
            'name' => $name,
            'id' => $ids[ $key ]
        ];
    });
   
    
}

$result = array_unique( $result, SORT_REGULAR );
var_dump( $result);

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.