1

So, to simply put it, it looks like this:

Array1:

Array
(
    [0] => id
    [1] => name
    [2] => email
)

Array2:

Array
(
    [0] => 1
    [1] => paula
    [2] => [email protected]

    [3] => 2
    [4] => martin
    [5] => [email protected]

    [6] => 3
    [7] => kasandra
    [8] => [email protected]

    [9] => 4
    [10] => helena
    [11] => [email protected]

    [12] => 5
    [13] => sophia
    [14] => [email protected]

    [15] => 6
    [16] => denis
    [17] => [email protected]
)

How to make those values from Array1 as keys in Array2 accordingly so that the end result looks like this:

Array
(
    [id] => 1
    [name] => paula
    [email] => [email protected]

    [id] => 2
    [name] => martin
    [email] => [email protected]

    [id] => 3
    [name] => kasandra
    [email] => [email protected]

    [id] => 4
    [name] => helena
    [email] => [email protected]

    [id] => 5
    [name] => sophia
    [email] => [email protected]

    [id] => 6
    [name] => denis
    [email] => [email protected]
)
5
  • 1
    In your sample you are overwriting on name and email index .. Commented Apr 1, 2018 at 16:35
  • You can't have same keys in array. Commented Apr 1, 2018 at 16:35
  • I am not overwriting anything, I just have those two arrays....and i need to assoiciate the keys Commented Apr 1, 2018 at 16:36
  • Assume I have that third array. Now I'm writing $array['email']. Which one will it print? Commented Apr 1, 2018 at 16:37
  • Actually I need to have this as JSON objects {"id": 1, "name: "paula", "email": "[email protected]" } Commented Apr 1, 2018 at 16:38

2 Answers 2

2

you can use a for loop with step 3

  $numCoords  = count($array2)/3
  for ($i = 0; $i < $numCoords; $i++ ){
    $array[$i]['id'] = $array2[$i*3]; 
    $array[$i]['name'] = $array2[($i*3)+1]; 
    $array[$i]['email'] = $array2[($i*3)+2]; 
  }
Sign up to request clarification or add additional context in comments.

14 Comments

I will try that now
Is it true or false that I can't have same keys in PHP array?
@lewis4u True, you can't have the same key. The newer will overwrite the old one.
@lewis4u . i don't uderstand you comment
@scaisEdge which comment
|
1

I would go with array_chunk and get used to numeric indexes, but if you really want to have them as words you may map them:

$keys = [
    0 => 'id',
    1 => 'name',
    2 => 'email'
];

$chunked = array_chunk($array2, $length=3);
$result = array_map(function ($chunk) {
    global $keys;
    return array_combine($keys, $chunk);
}, $chunked);

2 Comments

where do you come from ptica?
How to make this more dynamic? For example what if I have different number of attributes every time? instead of id, name and email there could be also additional properties

Your Answer

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.