nucky
nucky

Reputation: 348

Dynamic renaming multiple keys in multidimensional array

I am working on a code to import an array into another.

The array cars that would persist:

array (
  0 => 
  array (
    'Brand' => 'Volvo',
    'Stock Value' => 100,
    'Models' => 96,
  ),
  1 => 
  array (
    'Brand' => 'BMW',
    'Stock Value' => 60,
    'Models' => 59,
  ),
  2 => 
  array (
    'Brand' => 'Toyota',
    'Stock Value' => 110,
    'Models' => 100,
  ),
)

The array emobile I want to import:

array (
  0 => 
  array (
    'Maker' => 'Tesla',
    'Price' => 100,
    'Factories' => 96,
    'Employees' => 10,
  ),
  1 => 
  array (
    'Maker' => 'Nikola',
    'Price' => 60,
    'Factories' => 59,
    'Employees' => 10,
  ),
  2 => 
  array (
    'Maker' => 'Thor',
    'Price' => 110,
    'Factories' => 100,
    'Employees' => 10,
  ),
)

Post Data $pst_data (keys = aimed structure, values = current structure, --- not assigned):

array (
  'Brand' => 'Maker',
  'Stock_Value' => '---',
  'Models' => '---',
  'flag' => '---',
)

My Code. At its current stage, I can either make it happen to show a value from the imported array, e.g. the maker's name or I choose two ("Stock Value" as a second one) and there's the structure, but no values.

foreach ($kar as $xk => $vx) {
    if(!in_array($vx, $pst_data)){
        foreach ($emobile as &$xkone) {
            unset($xkone[$vx]);
        }
    }
    else {
        foreach ($pst_data as $pstd => $pstd_val) {
            foreach ($emobile as &$xkone) {
              $xkone[$pstd] = $xkone[$pstd_val];
              unset($xkone[$pstd_val]);
            }

        }
    }
}

And my aimed result:

Array
(
    [0] => Array
        (
            [Brand] => Volvo
            [Stock Value] => 100
            [Models] => 96
            [flag] => 1
        )

    [1] => Array
        (
            [Brand] => BMW
            [Stock Value] => 60
            [Models] => 59
            [flag] => 1
        )

    [2] => Array
        (
            [Brand] => Toyota
            [Stock Value] => 110
            [Models] => 100
            [flag] => 1
        )
    [3] => Array
        (
            [Brand] => Tesla
            [Stock Value] => 100
            [Models] => 
            [flag] => 
        )    
    [4] => Array
        (
            [Brand] => Nikola
            [Stock Value] => 60
            [Models] => 
            [flag] => 
        )
    [5] => Array
        (
            [Brand] => Thor
            [Stock Value] => 110
            [Models] => 
            [flag] => 
        )
)

So my goal is that I pick an array to import into another. In a form I assign keys from the imported array to the existing array and submit it to create an array with keys and assigned keys as values. If the assigned value, like Maker, exists, this key will remain in the imported array. All the other keys would be removed. Following would be the renaming of keys from, e.g. Maker to Brand, and Price to Stock Value. Then I would merge the two arrays with array_merge.

How can I fix the issue to work with single and multiple values from the imported array?

Edit Example for a single data assignment using $pst_data:

array (
  'Brand' => 'Maker',
  'Stock_Value' => '---',
  'Models' => '---',
  'flag' => '---',
)

and an example result for the first newly inserted array:

[3] => Array
    (
        [Brand] => Tesla
        [Stock_Value] => 
        [Models] => 
        [flag] => 
    )

Example for multiple data assigned using $pst_data:

array (
  'Brand' => 'Maker',
  'Stock_Value' => 'Price',
  'Models' => '---',
  'flag' => '---',
)

and an example result for the first newly inserted array:

[3] => Array
    (
        [Brand] => 
        [Stock_Value] => 
        [Models] => 
        [flag] => 
    )

Upvotes: 0

Views: 45

Answers (1)

Arnold Daniels
Arnold Daniels

Reputation: 16573

Switch the loops. In the outer loop, go through the data. In the inner loop, go through the properties.

Use a new variable for the mapped item. It can replace $xkone at the end of the loop to modify the existing array.

foreach ($emobile as &$xkone) {
    $car = [];

    foreach ($pst_data as $pstd => $pstd_val) {
        $car[$pstd] = $xkone[$pstd_val] ?? null;
    }

    $xkone = $car;
}

See online demo

Upvotes: 1

Related Questions