3

I have 2 arrays:

array 1 I want it to be a keys(duplicate is ok) in array_combine:

Array
    (
        [0] => id
        [1] => user_id
        [2] => firstname
    )

And here's my array 2 that I wanted to be the values in array_combine:

Array
    (
        [0] => 363
        [1] => 363
        [2] => Omotayo
    )

Array
    (
        [0] => 167
        [1] => 167
        [2] => Shafraaz
    )

Now challenge is, I have 2 arrays the first one has only one array and the second array has 2 inside arrays. The first array that I wanted to be the keys(duplicate) in array_combine. My desire output like below:

    Array
    (
        [id] => 363
        [user_id] => 363
        [firstname] => Omotayo
    )
    Array
    (
        [id] => 167
        [user_id] => 167
        [firstname] => Shafraaz
    )

Just wonder is there way to achieve this task? Appreciated any advise!!

Thanks

5
  • stackoverflow.com/questions/240660/… Refer this Commented Jul 3, 2015 at 3:10
  • Not sure what you mean Hemnath?? Could you explain more details? Commented Jul 3, 2015 at 3:23
  • Is this output of preg_match? Commented Jul 3, 2015 at 3:45
  • No nhahtdh, just 2 arrays I extracted from csv array, and want to combine them together. Commented Jul 3, 2015 at 4:03
  • Are you sure you have equal number of elements within next array Commented Jul 3, 2015 at 4:48

2 Answers 2

3

Why not just run array_combine on each inner array of $array2?

$final = array();
foreach($array2 as $array) {
    $final[] = array_combine($array1, $array);
}

This'll leave $final as the expected array with proper key/value pairs.

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

6 Comments

I have tried it before, and it threw an errors: Warning: array_combine(): Both parameters should have an equal number of elements
What's the exact values of your variables? Like $array1 = ..., $array2 = ..., etc as the only reason you'd get that reason based on your question is if you were iterating over the individual elements of $array2 instead of each outer array maybe?
below are my 2 arrays $array1 = Array( id, user_id, firstname ) $array2 = Array([0] => array('363', '363', 'Omotayo'), [1] =>array('167', '167', 'Shafraaz'))
very strange, it fails in my environment with the loop :/
My PHP Version is 5.5.12. Were this a problem?
|
1

Please, test this method and see if it works on your environment:

$keys = array("id","user_id","firstname");
$values = array(
    array(363,363,"Omotayo"),
    array(167,167,"Shafraaz")
);
$out = array();
foreach($values as $ukey=>$user)
{
    foreach($user as $key=>$data)
    {
        $values[$ukey][$keys[$key]] = $data;
        unset($values[$ukey][$key]);
    }
}
print_r($values);

Comments

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.