0

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] => 
 )
asked Jan 15, 2020 at 22:45
4
  • 2
    Please always present your project's array data as json strings or var_export() -- this makes it easier for volunteers to help you. When you use var_dump() or print_r(), volunteers will need to manually rewrite your arrays so that they can test their own suggestions. Commented Jan 15, 2020 at 22:57
  • Your sample data needs to represent all expected difficulties with your data. If you have duplicated Brand values in the two arrays, set up your sample data to show this, then show your EXACT desired output. Commented Jan 15, 2020 at 23:02
  • use <pre> before your var_export() call so that readers don't need to horizontally scroll. Then you can remove the old print_r() data from your question. To be honest, I don't yet find your question requirements to be Clear. Commented Jan 15, 2020 at 23:03
  • Please provide data that represents "single and multiple values". Commented Jan 15, 2020 at 23:22

1 Answer 1

1

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

answered Jan 15, 2020 at 23:06

3 Comments

Please show an online demo. Does this generate any Notices?
You suggest inserting the code inside the else-statement, right?
Jasny, it seems the problem remains.

Your Answer

Draft saved
Draft discarded

Sign up or log in

Sign up using Google
Sign up using Email and Password

Post as a guest

Required, but never shown

Post as a guest

Required, but never shown

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.