1

I have two arrays and I need the value from the first one to become the key for each row of an associative array. I think I stated that correctly. I have two arrays:

Array
(
    [field_name0] => first_name
    [field_name1] => mobile
    [field_name2] => email
    [field_name3] => last_name
)

and

Array
(
[1] => Array
    (
        [First Name] => Peter
        [mobile] => 1234567890
        [email] => [email protected]
        [Last Name] => Griffin
    )

[2] => Array
    (
        [First Name] => Al
        [mobile] => 9874561230
        [email] => [email protected]
        [Last Name] => Bundy
    )

)

I need the values from the first array to replace the values in each of the second arrays to look like this:

Array
(
[1] => Array
    (
        [first_name] => Peter
        [mobile] => 1234567890
        [email] => [email protected]
        [last_name] => Griffin
    )

[2] => Array
    (
        [first_name] => Al
        [mobile] => 9874561230
        [email] => [email protected]
        [last_name] => Bundy
    )

)

I have tried some bad attempts at some foreach loops to accomplish this but it's getting a bit tricky for me. Please help. I am hoping I just overlooked a simple way to do this.

What I tried:-

foreach( $field_map as $origKey => $value ){ 
      foreach($csvdata as $csvrow){ 
           foreach($csvrow as $cKey => $cValue){ 
                  $newKey = $value; 
                  $newArray[$newKey] = $cValue; 
           } 
       } 
}
4
  • this is embarrassingly as far as I've gotten: foreach( $field_map as $origKey => $value ){ foreach($csvdata as $csvrow){ foreach($csvrow as $cKey => $cValue){ $newKey = $value; $newArray[$newKey] = $cValue; } } } Commented Mar 18, 2016 at 19:59
  • $field_map being the first array and $csvdata being the second Commented Mar 18, 2016 at 20:02
  • khtims75 please check my latest answer. Commented Mar 18, 2016 at 21:05
  • deleting my answer because no response. Commented Mar 20, 2016 at 10:44

2 Answers 2

2

This script:

    $users = [
    [
        'First Name' => 'Peter',
        'mobile' => 1234567890,
        'email' => '[email protected]',
        'Last Name' => 'Griffin',
    ],
    [
        'First Name' => 'Al',
        'mobile' => 9874561230,
        'email' => '[email protected]',
        'Last Name' => 'Bundy',
    ],
];

$fields = [
    'field_name0' => 'first_name',
    'field_name1' => 'mobile',
    'field_name2' => 'email',
    'field_name3' => 'last_name',
];

$fieldNames = array_values($fields);
foreach ($users as &$user) {
    $user = array_combine($fields, array_values($user));
}
print_r($users);

Gives you what you wanted.

Effectively we just discarding keys and relying on the sequence of those items.

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

4 Comments

it will also not work if your conditions applied what you asked to me.
Oh? I will test some examples in my code and see if it works out.
@khtims75, if you want this to work with cases when you don't have full array of values for each user, you could use sizeof() to determine how many actual fields are there and cut the $fields array for each case. I didn't do that since it made sense for me to have full record each time, even with empty values.
array_combine() will ignore the keys of its array parameters, so array_values() is not needed.
0

Here - without foreach :)

$result = array_map(
    function($person) use ($keys) { 
        return array_combine($keys, $person);
    },
    $source);

http://sandbox.onlinephpfunctions.com/code/68fe2c199ac47349d5391b6b87bef7779cd945ad

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.