3

I currently have the $keys array

array(5) {
  [0] =>
  string(9) "SessionID"
  [1] =>
  string(5) "Title"
  [2] =>
  string(11) "Description"
  [3] =>
  string(5) "Level"
  [4] =>
  string(4) "Type"
}
that I use as keys for the values of another array called $values.

I would like to make an associative array by mapping the other array to $keys.

Another way to say this is I would like to array_combine($keys, $values) whereas $keys has only 5 elements but $values has more than 3000 elements.

Edit 1: Sorry for not putting in a $values example. It would have the same order as the $keys:

+-----------+-------+-------------+---------+------+
| SESSIONID | TITLE | DESCRIPTION |  LEVEL  | TYPE |
+-----------+-------+-------------+---------+------+
|           |       |             |         |      |
| 1         | A     | Describe A  | Level 1 | Word |
|           |       |             |         |      |
| 2         | B     | Describe B  | Level 2 | Word |
+-----------+-------+-------------+---------+------+

or

$values = [
1, 'A', 'Describe A', 'Level 1', 'Word',
2, 'B', 'Describe B', 'Level 2', 'Word'
];

since I populate both arrays from a single CSV file.

6
  • 1
    "whereas $keys has only 5 elements but $values has more than 3000 elements." - please elaborate. Is $values an array of (3000+) arrays each having five elements? If not: please explain how the keys are supposed to be "mapped" on the 3000+ values - sample data and the expected result for that data woul do nicely. Commented Mar 1, 2016 at 1:45
  • 1
    So what is $values? Is it 5 values matching those keys and then another 5 values, and so on? Commented Mar 1, 2016 at 2:30
  • @VolkerK $values is an 1-dimensional array of 3000+ values, 5 values match 5 keys then next 5 values would match 5 keys and so on Commented Mar 1, 2016 at 8:44
  • Does the file actually look like the table you've posted? Commented Mar 1, 2016 at 15:10
  • @VolkerK the file does not look like that table, I made that just for illustrative purposes Commented Mar 1, 2016 at 17:47

2 Answers 2

3

Since you've left out an explanation of what $values is I've guessed a little. Here are two scenarios.

If your values are all at the same level like below, we can chunk them up:

$keys = [ "SessionID", "Title", "Description", "Level", "Type", ];
$values = [
    1,
    "Title A",
    "Desc A",
    "Monster",
    "car",
    2,
    "Title B",
    "Desc B",
    "Devil",
    "car",
];

Cut the data up in to arrays with length equal to the number of keys.

$chunks = array_chunk($values, count($keys));

Then map them using array_combine like you suggested.

$ass = array_map(function ($chunk) use ($keys) {
    return array_combine($keys, $chunk);
}, $chunks);

If your array is an array of arrays (or rows) we can skip the chunking part and pass it to the mapping function directly:

$values = [
    [ 1, "Title A", "Desc A", "Monster", "car" ],
    [ 2, "Title B", "Desc B", "Devil", "car" ]
];

$ass = array_map(function ($chunk) use ($keys) {
    return array_combine($keys, $chunk);
}, $values);
Sign up to request clarification or add additional context in comments.

2 Comments

What? No generator? Or LimitIterator? A bit more imagination while we're in Wild Guess Land, please ;-) Just kidding....
@VolkerK, LOL, sorry to disappoint you. Thanks for the edit ;)
2

Since if it's worth doing, it's worth overdoing, here's a slightly more needlessly complex version of the first part of Michael's answer

<?php
$keys = array("SessionID","Title","Description","Level","Type");

$it = new NoRewindIterator(gen_data());
do {
    $foo = array_combine(
        $keys,
        iterator_to_array(new LimitIterator($it, 0, 5))
    );
    var_export($foo); echo "\r\n";
}
while ( $it->valid() );

function gen_data() {   
    static $HOW_MANY_WOULD_YOU_LIKE = 100;

    $x = array("Session #","Title #","Desc #","Lvl #","Type #");
    for($i=0; $i<$HOW_MANY_WOULD_YOU_LIKE*5; $i++) {
        yield $x[$i%5].((int)($i/5)+1);
    }
}

see http://docs.php.net/language.generators , http://docs.php.net/class.norewinditerator and http://docs.php.net/class.limititerator

Arrays are so 2015 (darn iterator_to_array) :-D

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.