0

I have a csv file. I need to read and format in array to be import. My header are look like this,

Array
(
    [0] => id
    [1] => name
    [2] => shortDescription
    [3] => description
    [4] => productType
    [5] => sku
    [6] => styleCode
)

And my values are look like this,

 Array
    (
        [0] => Array
            (
                [0] => 185
                [1] => T-shirts
                [2] => this is tshirt short desc
                [3] => This is tshirt desc
                [4] => simple
                [5] => 4585
                [6] => 5292++
            )
        [1] => Array
            (
                [0] => 186
                [1] => test name
                [2] => test short desc
                [3] => test desc
                [4] => configurable
                [5] => 525
                [6] => 555
            )
    )

Here I need to replace every key of my values corresponding header values. So my final array should be like this,

Array
    (
        [0] => Array
            (
                [id] => 185
                [name => T-shirts
                [shortdescription] => this is tshirt short desc
                [description] => This is tshirt desc
                [producttype] => simple
                [sku] => 4585
                [stylecode] => 5292++
            )
        [1] => Array
            (
                [id] => 186
                [name] => test name
                [shortdescription] => test short desc
                [description] => test desc
                [producttype] => configurable
                [sku] => 525
                [stylecode] => 555
            )
    )

I cannot find out good solution for this. Ca anybody help me to sort this out ?

5
  • Have you tried something or did some research ? Commented Mar 31, 2015 at 5:05
  • Yes, I have tried something but it is not working, so didn't posted it here.. Commented Mar 31, 2015 at 5:06
  • Learn about array_combine Commented Mar 31, 2015 at 5:06
  • 1
    @Elavarasan doesn't matter! Just post it here and show your effort you have done! I mean you are here to learn something and if it would work you wouldn't have to ask, so just show your code and we can show you how to fix your errors Commented Mar 31, 2015 at 5:09
  • Initially I have tried with foreach loop inside and inside and it was very crazy. I know I should use array functions but I'm not very familiar with arrays. So only I didnt post my code here. Commented Mar 31, 2015 at 5:33

1 Answer 1

4

This should work for you:

(Here I just go through each innerArray with array_walk() and return it combined with the $headers as keys and the innerArray as values, which I do with array_combine())

<?php

    array_walk($values, function(&$v, $k, $headers){
        $v = array_combine($headers, $v);
    }, $headers);

    print_r($values);

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

2 Comments

Thanks Rizier123 ..! It is worked.. Today I have learned lot ..Thanks again..!
@Elavarasan You're welcome! Have a nice day :D (BTW: If you want you can still add your attempt to you question)

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.