0

Apologies if this question is close to others. I have tried every solution to accomplish something simple with nothing but failure. :-(

I want the keys from array one to be assigned as keys to array two.

$demos_keys = array_keys($demos);

//$c = array_combine($demos_keys, $csvdata); produces "FALSE"
//$c = $demos_keys +  $csvdata; simply adds the arrays but doesn't assign keys

So then I tried to loop through each element to assign the keys manually - to no avail! 

foreach ($csvdata as $row){
     for($i = 0; $i<count($demo_keys); $i++) {
        $csvdata[$demo_keys[$i]]=$row[$i];
     } 
}

demos_keys: lastname":"lastname","email":"email","d1":"phone","d2":"status"

csvdata: "Dryer,fdryer@email.com,Backfield,North\r","Harris,fharris@email.com,Corp,South\r",etc.

I feel the csvdata array is wonky somehow. Every thing say it is an array with about 1000 rows, but the carriage return at the end of the last element is troubling me. I thought I'd deal with it later.

What else can I try!? Thank you all for any contributions!

David Weisser
  • 117
  • 1
  • 10
  • Surely `$csvdata` is a set of rows and not just 1 row, so do you need to do the `array_combine()` in the `foreach()` (or at all as I'm not sure what you need to do with it)? – Nigel Ren Feb 13 '21 at 20:30
  • @NigelRen. It certainly appears to be separate rows. Length = 1000, one for each row in the csv. Nick's answer below is correct. But, I still don't understand it. Even Nick's answer iterates on separate rows within $csvdata. I need to trial/error with this until I fully grasp what is going on! Thank you for checking it out. – David Weisser Feb 13 '21 at 23:19

1 Answers1

1

It looks like each row of your CSV data has not been parsed into separate variables (are you reading it from a file using fgets or file instead of fgetcsv?). So you need to split it before you can combine it with the keys from $demos_keys. Something like this should work:

$demos_keys = array("lastname","email","d1","d2");
$csvdata = array("Dryer,fdryer@email.com,Backfield,North\r","Harris,fharris@email.com,Corp,South\r");

$result = array();
foreach ($csvdata as $row) {
    $data = explode(',', trim($row));
    $result[] = array_combine($demos_keys, $data);
}
print_r($result);

Output:

Array
(
    [0] => Array
        (
            [lastname] => Dryer
            [email] => fdryer@email.com
            [d1] => Backfield
            [d2] => North
        )
    [1] => Array
        (
            [lastname] => Harris
            [email] => fharris@email.com
            [d1] => Corp
            [d2] => South
        )
)

Demo on 3v4l.org

Nick
  • 138,499
  • 22
  • 57
  • 95
  • Perfect. Thank you! I produced the csv data like this: var csvrows = e.target.result.split("\n"); it seemed to be an array with four columns and 1000 rows which is inline with the csv itself. I don't understand...if it is one variable...how am I able to count the array and get it's length at a 1000...rather than 1? – David Weisser Feb 13 '21 at 23:13
  • @DavidWeisser once you've done the first split it is an array; if you'd just used `e.target.result` I think you would have got a count of 1. Without seeing all the code it's hard to be certain. Anyway, I'm glad this was helpful. – Nick Feb 14 '21 at 01:58