0

Okay, i have multidimensional array with keys and values:

[0] => Array
    (
        [0] => Consultant
        [1] => Inv. num.
        [2] => Order
        [3] => Due
        [12] => Currency
        [13] => File
        [21] => First name
        [22] => Last name
        [27] => ID
        [28] => Birthdate
        [29] => Postcode
        [30] => City
        [31] => Address
        [36] => Mobile
        [37] => Email
    )

[1] => Array
    (
        [0] => 18642
        [1] => 9376533321
        [2] => 27.05.2019.
        [3] => 28.06.2019.
        [12] => Currency1
        [13] => 109,43
        [21] => Name1
        [22] => Lastname1
        [27] => 18977
        [28] => Birhtdate1
        [29] => Postcode1
        [30] => City1
        [31] => Address1
        [36] => Mobile1
        [37] => [email protected]
    )

[2] => Array
    (
        [0] => 186625
        [1] => 5638871123
        [2] => 17.06.2019.
        [3] => 03.07.2019.
        [12] => Currency2
        [13] => 235,33
        [21] => Name2
        [22] => Lastname2
        [27] => 18954
        [28] => Birthdate2
        [29] => Postcode2
        [30] => City2
        [31] => Address2
        [36] => Mobile2
        [37] => mail@mail2
    )

I would like to change key order in 2nd level of array, according to pattern: 21, 22, 27, 28, 29, 30, 31, 36, 37, 0, 1, 2, 3, 13, 12, but also, make the values rearrange with them.

So far, ive been able to change key pattern accordingly, but values order stays the same as they were in original array, and i want them to be rearranged together with keys. Also, my code transforms only last piece of the first level array, i would like to apply it on all subarrays:

$new_keys = array(21 => '', 22 => '', 27 => '', 28 => '', 29 => '', 30 => '', 31 => '', 36 => '', 37 => '', 0 => '', 1 => '', 2 => '', 3 => '', 13 => '', 12 => '');

foreach ($csvArray as $secondkey => $secondval) {
    foreach ($secondval as $thirdkey => $thirdval) {
        $final = array_combine(array_keys($new_keys), $secondval);
    }   

}

And i get output like this:

Array
(
[21] => 113243
[22] => 9013435433
[27] => 21.06.2019.
[28] => 09.07.2019.
[29] => SomeCurrency
[30] => 182,86
[31] => Some Name
[36] => Some Last Name
[37] => 0534343
[0] => 28.12.1981.
[1] => Some Zip Code
[2] => Some City Name
[3] => Some Address
[13] => Some Number
[12] => [email protected]
)

and i would like to get something like this:

[0] => Array
     (
        [21] => First name
        [22] => Last name
        [27] => ID
        [28] => Birthdate
        [29] => Postcode
        [30] => City
        [31] => Address
        [36] => Mobile
        [37] => Email
        [0] => Consultant
        [1] => Inv. num.
        [2] => Order
        [3] => Due
        [13] => File
        [12] => Currency
    )

[1] => Array
    (
        [21] => Some name1
        [22] => Some Last name1
        [27] => 18732763
        [28] => 28.06.2019.
        [29] => Some post code1
        [30] => Cityname1
        [31] => SomeAddress1
        [36] => Mobilenumber1
        [37] => mail@mailaddress1
        [0] => 0238244
        [1] => 34345
        [2] => Order1
        [3] => 12.12.2019
        [13] => 264,42
        [12] => SomeCurrencyCode
    )

etc.

Thank you.

1

2 Answers 2

1

You can make the array associative with the key => value.
That way you don't need the [0] index as a "map" of wht the values are.

$order = [21, 22, 27, 28, 29, 30, 31, 36, 37, 0, 1, 2, 3, 13, 12];
// reorder the keys
foreach($order as $index => $key){
        $keys[$index] = $arr[0][$key]; 
}
// $keys = Array(First name, Last name, ID, Birthdate, Postcode, City, Address, Mobile, Email, Consultant, Inv. num., Order, Due, File, Currency)

// split out values from array
$values = array_slice($arr,1);


foreach($values as &$items){
    //reorder the keys in items
    foreach($order as $index => $key){
        $new[$index] = $items[$key]; 
    }
    // associate the array
    $items = array_combine($keys, $new);

}
var_dump($values);

this gives:

array(2) {
  [0]=>
  array(15) {
    ["First name"]=>
    string(5) "Name1"
    ["Last name"]=>
    string(9) "Lastname1"
    ["ID"]=>
    string(5) "18977"
    ["Birthdate"]=>
    string(10) "Birhtdate1"
    ["Postcode"]=>
    string(9) "Postcode1"
    ["City"]=>
    string(5) "City1"
    ["Address"]=>
    string(8) "Address1"
    ["Mobile"]=>
    string(7) "Mobile1"
    ["Email"]=>
    string(13) "[email protected]"
    ["Consultant"]=>
    string(5) "18642"
    ["Inv. num."]=>
    string(10) "9376533321"
    ["Order"]=>
    string(11) "27.05.2019."
    ["Due"]=>
    string(11) "28.06.2019."
    ["File"]=>
    string(6) "109,43"
    ["Currency"]=>
    string(9) "Currency1"
  }
  [1]=>
  &array(15) {
    ["First name"]=>
    string(5) "Name2"
    ["Last name"]=>
    string(9) "Lastname2"
    ["ID"]=>
    string(5) "18954"
    ["Birthdate"]=>
    string(10) "Birthdate2"
    ["Postcode"]=>
    string(9) "Postcode2"
    ["City"]=>
    string(5) "City2"
    ["Address"]=>
    string(8) "Address2"
    ["Mobile"]=>
    string(7) "Mobile2"
    ["Email"]=>
    string(10) "mail@mail2"
    ["Consultant"]=>
    string(6) "186625"
    ["Inv. num."]=>
    string(10) "5638871123"
    ["Order"]=>
    string(11) "17.06.2019."
    ["Due"]=>
    string(11) "03.07.2019."
    ["File"]=>
    string(6) "235,33"
    ["Currency"]=>
    string(9) "Currency2"
  }
}

https://3v4l.org/rBWYs


If you don't want it associative then you will have to reorder the array and change the index number.

$order = [21, 22, 27, 28, 29, 30, 31, 36, 37, 0, 1, 2, 3, 13, 12];
foreach($order as $index => $key){
        $keys[$index] = $arr[0][$key]; 
}


// associate
$result[]=$keys;
foreach(array_slice($arr,1) as $items){
    foreach($order as $index => $key){
        $new[$index] = $items[$key]; 
    }
    $result[] = $new;
}
var_dump($result);

https://3v4l.org/4rrn9

Sign up to request clarification or add additional context in comments.

1 Comment

Thank you, non-associative version is quite ok.
-1

There is no way to rearrange keys of an indexed array. You can use an associative array instead or have a separate array denoting the order of keys. I would suggest using another array denoting order:

$order = [21, 22, 27, 28, 29, 30, 31, 36, 37, 0, 1, 2, 3, 13, 12];
$data = [[ /* Field names array */], [/* Field values array*/]];
foreach($i in $order)
{
    echo $data[0][$i]."=".$data[1][$i];
}

Comments

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.