开发者

Reindex and fill out array depending on another array

I'm trying to create a tables with information from two arrays. Here are the two arrays:

First array, for table headers

Array
(
    [0] => Color
    [1] => Length
    [2] => Waist
)

Second array, the one that needs modification

Array
(
    [0] => Ar开发者_C百科ray
        [0] => green [1] => Color

    [1] => Array
        [0] => 23 [1] => Length
)
Array
(
    [0] => 
)
Array
(
    [0] => Array
        [0] => 23 [1] => Length

    [1] => Array
        [0] => 24 [1] => Waist 
)       

Array needs to look like this:

Array
(
    [0] => Array
        [0] => green [1] => Color

    [1] => Array
        [0] => 23 [1] => Length
    
    [2] => Array
        [0] => [1] => Waist 

Array
(
    [0] => Array
        [0] => [1] => Color

    [1] => Array
        [0] => [1] => Length

    [2] => Array
        [0] => [1] => Waist  

Array
(
    [0] => Array
        [0] => [1] => Color

    [1] => Array
        [0] => 23 [1] => Length

    [2] => Array
        [0] => 24 [1] => Waist 

So the point is that the keys in the first level needs to match the keys in the array that makes the table headers, where [1] one the second level has the same value as the table header. Any ideas?


After some feedback, an alternative acceptable output array structure would be:

array(
    array(
        'Color' => 'green',
        'Length' => 23,
        'Waist' => null
    ),
    array(
        'Color' => null,
        'Length' => null,
        'Waist' => null
    ),
    array(
        'Color' => null,
        'Length' => 23,
        'Waist' => 24
    )
)


You have a complex array structure for an easy set of data. Could your final array work better like this?

$data = array(
    array(
        'Color' => 'green',
        'Length' => 23,
        'Waist' => NULL
    ),
    array(
        'Color' => NULL,
        'Length' => NULL,
        'Waist' => NULL
    ),
    array(
        'Color' => NULL,
        'Length' => 23,
        'Waist' => 24
    )
);

If you're dead set on your structure, though, this should work:

function format_my_array($keys, $malformed) {
    foreach ($malformed as $key => $fragments) {
        $temp = array(
            'Color' => NULL,
            'Length' => NULL,
            'Waist' => NULL
        );
        foreach ($fragments as $fragment) {
            if (isset($fragment[1])) {
                switch($fragment[1]) {
                    case 'Length':
                        $temp['Length'] = $fragment[1];
                        break;
                    case 'Waist':
                        $temp['Waist'] = $fragment[1];
                        break;
                    default:
                        $temp['Color'] = $fragment[1];
                        break;
                }
            }
        }
        $malformed[$key] = array(
            array($temp['Color'], 'Color'),
            array($temp['Length'], 'Length'),
            array($temp['Waist'], 'Waist')
        );
    }
    return $malformed;
}


Generate the default values (once) as a re-usable array, then iterate your input data and overwrite the default values with each item's set of "attributes".

Code: (Demo) (PHP7.4+ Demo)

$headers = ['Color', 'Length', 'Waist'];

$data = [
    [
        ['green', 'Color'],
        ['23', 'Length'],
    ],
    [],
    [
        ['23', 'Length'],
        ['24', 'Waist'],
    ],
];

$defaults = array_fill_keys($headers, null);
var_export(
    array_map(
        function($item) use($defaults) {
            return array_replace($defaults, array_column($item, 0, 1));
        },
        $data
    )
);
0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜