Converting a query result into an key=>value pairs for a lookup table
Clash Royale CLAN TAG#URR8PPP
.everyoneloves__top-leaderboard:empty,.everyoneloves__mid-leaderboard:empty margin-bottom:0;
up vote
5
down vote
favorite
My app has a number of lookup tables stored in a MySQL database that are used for various purposes such as a select
element.
Unfortunately, the query results aren't in an easily-digestible format and need some massaging to be more useful to me.
For example, when you query one of the lookup tables for all values, you get an array like this:
0 => ['level_id' => 1, 'level' => 'Trivial'],
1 => ['level_id' => 2, 'level' => 'Moderate']
2 => ['level_id' => 3, 'level' => 'Challenging'],
3 => ['level_id' => 4, 'level' => 'Formidable']
But for my purposes, this is what I want:
1 => 'Trivial',
2 => 'Moderate',
3 => 'Challenging',
4 => 'Formidable'
(Note that the array keys are the same as the record id).
If the array has more than one additional column, the result will be contained in sub-arrays:
3 => ['level' => 'Challenging', 'description' => 'Foobar'],
4 => ['level' => 'Formidable', 'description' => 'Bazbat']
This is all very trivial, but I wrote this function that I'd like you to review to know if there is a better way to do this in PHP.
The function needs to be generic so it can use any lookup array without having to know the column names.
<?php
// Turn an array into a key=>value pair. Assumes the key is the first item in the sub-array.
public function column_into_keys(array $array): array
// get the name of the column that contains the record id
$key = key($array[0]);
foreach($array as $row)
// pop the new key off the top of the array
$id = array_shift($row);
// is there only one item left in the array?
if (count($row) == 1)
// get the first value
$result[$id] = current($row);
else
// get all of the values
$result[$id] = $row;
return $result;
php mysql lookup
 |Â
show 1 more comment
up vote
5
down vote
favorite
My app has a number of lookup tables stored in a MySQL database that are used for various purposes such as a select
element.
Unfortunately, the query results aren't in an easily-digestible format and need some massaging to be more useful to me.
For example, when you query one of the lookup tables for all values, you get an array like this:
0 => ['level_id' => 1, 'level' => 'Trivial'],
1 => ['level_id' => 2, 'level' => 'Moderate']
2 => ['level_id' => 3, 'level' => 'Challenging'],
3 => ['level_id' => 4, 'level' => 'Formidable']
But for my purposes, this is what I want:
1 => 'Trivial',
2 => 'Moderate',
3 => 'Challenging',
4 => 'Formidable'
(Note that the array keys are the same as the record id).
If the array has more than one additional column, the result will be contained in sub-arrays:
3 => ['level' => 'Challenging', 'description' => 'Foobar'],
4 => ['level' => 'Formidable', 'description' => 'Bazbat']
This is all very trivial, but I wrote this function that I'd like you to review to know if there is a better way to do this in PHP.
The function needs to be generic so it can use any lookup array without having to know the column names.
<?php
// Turn an array into a key=>value pair. Assumes the key is the first item in the sub-array.
public function column_into_keys(array $array): array
// get the name of the column that contains the record id
$key = key($array[0]);
foreach($array as $row)
// pop the new key off the top of the array
$id = array_shift($row);
// is there only one item left in the array?
if (count($row) == 1)
// get the first value
$result[$id] = current($row);
else
// get all of the values
$result[$id] = $row;
return $result;
php mysql lookup
array_column('level') will give you that php.net/manual/en/function.array-column.php. Check out array_keys also for reference
â Indra
Apr 5 at 20:15
@Indra, witharray_column()
I would need to explicitly indicate the column names, wouldn't I?
â pbarney
Apr 5 at 20:25
yeah, but that can be a variable. like in this case $column='level'. array_column($column) and you set that as needed
â Indra
Apr 5 at 20:28
Unfortunately, I would also need to specify the column that contains my row id's for the key. My method above doesn't care about the names of the columns.
â pbarney
Apr 5 at 20:29
array_combine(array_column($col1), array_column($col2)) ? Ah.. this would be so much easier using collections. Or a combo of array_keys and array_values or an array_map should work.
â Indra
Apr 5 at 20:33
 |Â
show 1 more comment
up vote
5
down vote
favorite
up vote
5
down vote
favorite
My app has a number of lookup tables stored in a MySQL database that are used for various purposes such as a select
element.
Unfortunately, the query results aren't in an easily-digestible format and need some massaging to be more useful to me.
For example, when you query one of the lookup tables for all values, you get an array like this:
0 => ['level_id' => 1, 'level' => 'Trivial'],
1 => ['level_id' => 2, 'level' => 'Moderate']
2 => ['level_id' => 3, 'level' => 'Challenging'],
3 => ['level_id' => 4, 'level' => 'Formidable']
But for my purposes, this is what I want:
1 => 'Trivial',
2 => 'Moderate',
3 => 'Challenging',
4 => 'Formidable'
(Note that the array keys are the same as the record id).
If the array has more than one additional column, the result will be contained in sub-arrays:
3 => ['level' => 'Challenging', 'description' => 'Foobar'],
4 => ['level' => 'Formidable', 'description' => 'Bazbat']
This is all very trivial, but I wrote this function that I'd like you to review to know if there is a better way to do this in PHP.
The function needs to be generic so it can use any lookup array without having to know the column names.
<?php
// Turn an array into a key=>value pair. Assumes the key is the first item in the sub-array.
public function column_into_keys(array $array): array
// get the name of the column that contains the record id
$key = key($array[0]);
foreach($array as $row)
// pop the new key off the top of the array
$id = array_shift($row);
// is there only one item left in the array?
if (count($row) == 1)
// get the first value
$result[$id] = current($row);
else
// get all of the values
$result[$id] = $row;
return $result;
php mysql lookup
My app has a number of lookup tables stored in a MySQL database that are used for various purposes such as a select
element.
Unfortunately, the query results aren't in an easily-digestible format and need some massaging to be more useful to me.
For example, when you query one of the lookup tables for all values, you get an array like this:
0 => ['level_id' => 1, 'level' => 'Trivial'],
1 => ['level_id' => 2, 'level' => 'Moderate']
2 => ['level_id' => 3, 'level' => 'Challenging'],
3 => ['level_id' => 4, 'level' => 'Formidable']
But for my purposes, this is what I want:
1 => 'Trivial',
2 => 'Moderate',
3 => 'Challenging',
4 => 'Formidable'
(Note that the array keys are the same as the record id).
If the array has more than one additional column, the result will be contained in sub-arrays:
3 => ['level' => 'Challenging', 'description' => 'Foobar'],
4 => ['level' => 'Formidable', 'description' => 'Bazbat']
This is all very trivial, but I wrote this function that I'd like you to review to know if there is a better way to do this in PHP.
The function needs to be generic so it can use any lookup array without having to know the column names.
<?php
// Turn an array into a key=>value pair. Assumes the key is the first item in the sub-array.
public function column_into_keys(array $array): array
// get the name of the column that contains the record id
$key = key($array[0]);
foreach($array as $row)
// pop the new key off the top of the array
$id = array_shift($row);
// is there only one item left in the array?
if (count($row) == 1)
// get the first value
$result[$id] = current($row);
else
// get all of the values
$result[$id] = $row;
return $result;
php mysql lookup
edited Apr 6 at 2:11
Jamalâ¦
30.1k11114225
30.1k11114225
asked Apr 5 at 19:20
pbarney
1306
1306
array_column('level') will give you that php.net/manual/en/function.array-column.php. Check out array_keys also for reference
â Indra
Apr 5 at 20:15
@Indra, witharray_column()
I would need to explicitly indicate the column names, wouldn't I?
â pbarney
Apr 5 at 20:25
yeah, but that can be a variable. like in this case $column='level'. array_column($column) and you set that as needed
â Indra
Apr 5 at 20:28
Unfortunately, I would also need to specify the column that contains my row id's for the key. My method above doesn't care about the names of the columns.
â pbarney
Apr 5 at 20:29
array_combine(array_column($col1), array_column($col2)) ? Ah.. this would be so much easier using collections. Or a combo of array_keys and array_values or an array_map should work.
â Indra
Apr 5 at 20:33
 |Â
show 1 more comment
array_column('level') will give you that php.net/manual/en/function.array-column.php. Check out array_keys also for reference
â Indra
Apr 5 at 20:15
@Indra, witharray_column()
I would need to explicitly indicate the column names, wouldn't I?
â pbarney
Apr 5 at 20:25
yeah, but that can be a variable. like in this case $column='level'. array_column($column) and you set that as needed
â Indra
Apr 5 at 20:28
Unfortunately, I would also need to specify the column that contains my row id's for the key. My method above doesn't care about the names of the columns.
â pbarney
Apr 5 at 20:29
array_combine(array_column($col1), array_column($col2)) ? Ah.. this would be so much easier using collections. Or a combo of array_keys and array_values or an array_map should work.
â Indra
Apr 5 at 20:33
array_column('level') will give you that php.net/manual/en/function.array-column.php. Check out array_keys also for reference
â Indra
Apr 5 at 20:15
array_column('level') will give you that php.net/manual/en/function.array-column.php. Check out array_keys also for reference
â Indra
Apr 5 at 20:15
@Indra, with
array_column()
I would need to explicitly indicate the column names, wouldn't I?â pbarney
Apr 5 at 20:25
@Indra, with
array_column()
I would need to explicitly indicate the column names, wouldn't I?â pbarney
Apr 5 at 20:25
yeah, but that can be a variable. like in this case $column='level'. array_column($column) and you set that as needed
â Indra
Apr 5 at 20:28
yeah, but that can be a variable. like in this case $column='level'. array_column($column) and you set that as needed
â Indra
Apr 5 at 20:28
Unfortunately, I would also need to specify the column that contains my row id's for the key. My method above doesn't care about the names of the columns.
â pbarney
Apr 5 at 20:29
Unfortunately, I would also need to specify the column that contains my row id's for the key. My method above doesn't care about the names of the columns.
â pbarney
Apr 5 at 20:29
array_combine(array_column($col1), array_column($col2)) ? Ah.. this would be so much easier using collections. Or a combo of array_keys and array_values or an array_map should work.
â Indra
Apr 5 at 20:33
array_combine(array_column($col1), array_column($col2)) ? Ah.. this would be so much easier using collections. Or a combo of array_keys and array_values or an array_map should work.
â Indra
Apr 5 at 20:33
 |Â
show 1 more comment
2 Answers
2
active
oldest
votes
up vote
2
down vote
accepted
Given you are working with PHP, there is no reason to neglect such a feature that already exists in the language.
For example when you query one of the lookup tables for all values,
$data = $pdo->query("SELECT * FROM lookup")->fetchAll(PDO::FETCH_KEY_PAIR);
you get an array like this:
1 => 'Trivial',
2 => 'Moderate',
3 => 'Challenging',
4 => 'Formidable'
all thanks to PDO::FETCH_KEY_PAIR fetch mode.
Of course it works only with PDO, but you are supposed to use this driver anyway.
Thank you, this is exactly the kind of help I was hoping for. In my current case, I'm unfortunately working with a legacy CodeIgniter codebase that doesn't have that as part of it's database abstraction. But I now have a new useful tool in my belt.
â pbarney
Apr 6 at 14:02
One quick question: does this handle my second use case, where the array values could be arrays themselves?
â pbarney
Apr 6 at 14:03
1
yes of course, PDO has PDO::FETCH_UNIQUE mode as well
â Your Common Sense
Apr 6 at 14:04
add a comment |Â
up vote
2
down vote
Try this:
$finalResult=;
array_map(function ($value) use (&$finalResult)
$result = array_values($value);
return $finalResult[$result[0]] = $result[1];
, $array);
return $finalResult;
I try avoiding fors as much as possible and rely on native functions. I started doing this since I read a book called refactoring to collections. This improved my code and the speed of my code.
Native functions are written in a memory effective way.
The function inside the array_map is called a closure. You can implement a closure anytime you need to do quick computations locally.
Your solution does the job right, but looks a little bit messy. I don't think there will be a speed issue with what you have though.
Edit:
$array = [
0 => ['level_id' => 1, 'level' => 'Trivial', 'description' => 'Foobar'],
1 => ['level_id' => 2, 'level' => 'Moderate', 'description' => 'Foobar'],
2 => ['level_id' => 3, 'level' => 'Challenging', 'description' => 'Foobar'],
3 => ['level_id' => 4, 'level' => 'Formidable'],
];
Using this:
$finalResult = ;
array_map(function ($value) use (&$finalResult)
$result = array_values($value);
return sizeof($result) == 2 ?
$finalResult[$result[0]] = $result[1]
: $finalResult[$result[0]] = array_slice($value, 0, sizeof($value) - 1);
, $array);
You will always get the right answer no matter how many columns you have or how many combinations you have.
- sizeof($result) == 2 ? $finalResult[$result[0]] = $result[1] if teh array has 2 entries use the first as id and second as value
- $finalResult[$result[0]] = array_slice($value, 0, sizeof($value) - 1); // if it has more than 2 entries then 2 entries remove the first (array_slice($value, 0, sizeof($value) - 1)). This does not cover the case where there is only one entry, but neither did yours. We should cover that as well like this:
return sizeof($result) == 2 ?
$finalResult[$result[0]] = $result[1]
: sizeof($result) > 2 ? $finalResult[$result[0]] = array_slice($value, 0, sizeof($value) - 1) : ;
Now for the for issue:
What a for does is basically iterating through every item of the array (no matter how long) and do something with it. Ideally, native functions use memory addresses to find one element at the time, do something with it and free the memory. I said ideally, because sometimes whoever write the code through a foe behind it. I will try to write a blog post about this since it sounds like a great issue to raise.
Mostly, what I can tell you after 12 years of programming is that you learn by doing. And you usually learn when something does not work out. I learned my lesson with for after a server failed with 503( was having timeout issues) when my db got to ~1 mil entries.
It is great that you want to learn and improve and I think that if you stick to what you're doing you're gonna get there. Just don't be afraid to test and try other things. Whenever I have a similar issue I go to the manual and check what I can use. If no such functions is in the manual I add it to my helpers file (that I import everywhere) and try it with lots of data. If it fails I go back and try other options.
I try avoiding fors as much as possible and rely on native functions. I think your result does the job nicely, these are just alternatives. I am pretty rusty with my php (using Laravel for 4 years), but when I did work in vanilla php I used to rely on these 4 functions like crazy: array_map, array_keys, array_values, array_column
â Indra
Apr 5 at 20:47
Thank you for responding. But this doesn't address the case where the array contains more than one additional element. See the 3rd code block in the question.
â pbarney
Apr 5 at 20:55
@pbarney you're right. let me check another solution
â Indra
Apr 5 at 20:57
Is your general thesis that using foreach loops are a bad idea? If so, why? I'm looking for wisdom to help me grow as a programmer.
â pbarney
Apr 5 at 21:05
check out my edit and feel free to give me feedback
â Indra
Apr 5 at 21:24
 |Â
show 2 more comments
2 Answers
2
active
oldest
votes
2 Answers
2
active
oldest
votes
active
oldest
votes
active
oldest
votes
up vote
2
down vote
accepted
Given you are working with PHP, there is no reason to neglect such a feature that already exists in the language.
For example when you query one of the lookup tables for all values,
$data = $pdo->query("SELECT * FROM lookup")->fetchAll(PDO::FETCH_KEY_PAIR);
you get an array like this:
1 => 'Trivial',
2 => 'Moderate',
3 => 'Challenging',
4 => 'Formidable'
all thanks to PDO::FETCH_KEY_PAIR fetch mode.
Of course it works only with PDO, but you are supposed to use this driver anyway.
Thank you, this is exactly the kind of help I was hoping for. In my current case, I'm unfortunately working with a legacy CodeIgniter codebase that doesn't have that as part of it's database abstraction. But I now have a new useful tool in my belt.
â pbarney
Apr 6 at 14:02
One quick question: does this handle my second use case, where the array values could be arrays themselves?
â pbarney
Apr 6 at 14:03
1
yes of course, PDO has PDO::FETCH_UNIQUE mode as well
â Your Common Sense
Apr 6 at 14:04
add a comment |Â
up vote
2
down vote
accepted
Given you are working with PHP, there is no reason to neglect such a feature that already exists in the language.
For example when you query one of the lookup tables for all values,
$data = $pdo->query("SELECT * FROM lookup")->fetchAll(PDO::FETCH_KEY_PAIR);
you get an array like this:
1 => 'Trivial',
2 => 'Moderate',
3 => 'Challenging',
4 => 'Formidable'
all thanks to PDO::FETCH_KEY_PAIR fetch mode.
Of course it works only with PDO, but you are supposed to use this driver anyway.
Thank you, this is exactly the kind of help I was hoping for. In my current case, I'm unfortunately working with a legacy CodeIgniter codebase that doesn't have that as part of it's database abstraction. But I now have a new useful tool in my belt.
â pbarney
Apr 6 at 14:02
One quick question: does this handle my second use case, where the array values could be arrays themselves?
â pbarney
Apr 6 at 14:03
1
yes of course, PDO has PDO::FETCH_UNIQUE mode as well
â Your Common Sense
Apr 6 at 14:04
add a comment |Â
up vote
2
down vote
accepted
up vote
2
down vote
accepted
Given you are working with PHP, there is no reason to neglect such a feature that already exists in the language.
For example when you query one of the lookup tables for all values,
$data = $pdo->query("SELECT * FROM lookup")->fetchAll(PDO::FETCH_KEY_PAIR);
you get an array like this:
1 => 'Trivial',
2 => 'Moderate',
3 => 'Challenging',
4 => 'Formidable'
all thanks to PDO::FETCH_KEY_PAIR fetch mode.
Of course it works only with PDO, but you are supposed to use this driver anyway.
Given you are working with PHP, there is no reason to neglect such a feature that already exists in the language.
For example when you query one of the lookup tables for all values,
$data = $pdo->query("SELECT * FROM lookup")->fetchAll(PDO::FETCH_KEY_PAIR);
you get an array like this:
1 => 'Trivial',
2 => 'Moderate',
3 => 'Challenging',
4 => 'Formidable'
all thanks to PDO::FETCH_KEY_PAIR fetch mode.
Of course it works only with PDO, but you are supposed to use this driver anyway.
edited Apr 6 at 13:58
answered Apr 6 at 13:50
Your Common Sense
2,415524
2,415524
Thank you, this is exactly the kind of help I was hoping for. In my current case, I'm unfortunately working with a legacy CodeIgniter codebase that doesn't have that as part of it's database abstraction. But I now have a new useful tool in my belt.
â pbarney
Apr 6 at 14:02
One quick question: does this handle my second use case, where the array values could be arrays themselves?
â pbarney
Apr 6 at 14:03
1
yes of course, PDO has PDO::FETCH_UNIQUE mode as well
â Your Common Sense
Apr 6 at 14:04
add a comment |Â
Thank you, this is exactly the kind of help I was hoping for. In my current case, I'm unfortunately working with a legacy CodeIgniter codebase that doesn't have that as part of it's database abstraction. But I now have a new useful tool in my belt.
â pbarney
Apr 6 at 14:02
One quick question: does this handle my second use case, where the array values could be arrays themselves?
â pbarney
Apr 6 at 14:03
1
yes of course, PDO has PDO::FETCH_UNIQUE mode as well
â Your Common Sense
Apr 6 at 14:04
Thank you, this is exactly the kind of help I was hoping for. In my current case, I'm unfortunately working with a legacy CodeIgniter codebase that doesn't have that as part of it's database abstraction. But I now have a new useful tool in my belt.
â pbarney
Apr 6 at 14:02
Thank you, this is exactly the kind of help I was hoping for. In my current case, I'm unfortunately working with a legacy CodeIgniter codebase that doesn't have that as part of it's database abstraction. But I now have a new useful tool in my belt.
â pbarney
Apr 6 at 14:02
One quick question: does this handle my second use case, where the array values could be arrays themselves?
â pbarney
Apr 6 at 14:03
One quick question: does this handle my second use case, where the array values could be arrays themselves?
â pbarney
Apr 6 at 14:03
1
1
yes of course, PDO has PDO::FETCH_UNIQUE mode as well
â Your Common Sense
Apr 6 at 14:04
yes of course, PDO has PDO::FETCH_UNIQUE mode as well
â Your Common Sense
Apr 6 at 14:04
add a comment |Â
up vote
2
down vote
Try this:
$finalResult=;
array_map(function ($value) use (&$finalResult)
$result = array_values($value);
return $finalResult[$result[0]] = $result[1];
, $array);
return $finalResult;
I try avoiding fors as much as possible and rely on native functions. I started doing this since I read a book called refactoring to collections. This improved my code and the speed of my code.
Native functions are written in a memory effective way.
The function inside the array_map is called a closure. You can implement a closure anytime you need to do quick computations locally.
Your solution does the job right, but looks a little bit messy. I don't think there will be a speed issue with what you have though.
Edit:
$array = [
0 => ['level_id' => 1, 'level' => 'Trivial', 'description' => 'Foobar'],
1 => ['level_id' => 2, 'level' => 'Moderate', 'description' => 'Foobar'],
2 => ['level_id' => 3, 'level' => 'Challenging', 'description' => 'Foobar'],
3 => ['level_id' => 4, 'level' => 'Formidable'],
];
Using this:
$finalResult = ;
array_map(function ($value) use (&$finalResult)
$result = array_values($value);
return sizeof($result) == 2 ?
$finalResult[$result[0]] = $result[1]
: $finalResult[$result[0]] = array_slice($value, 0, sizeof($value) - 1);
, $array);
You will always get the right answer no matter how many columns you have or how many combinations you have.
- sizeof($result) == 2 ? $finalResult[$result[0]] = $result[1] if teh array has 2 entries use the first as id and second as value
- $finalResult[$result[0]] = array_slice($value, 0, sizeof($value) - 1); // if it has more than 2 entries then 2 entries remove the first (array_slice($value, 0, sizeof($value) - 1)). This does not cover the case where there is only one entry, but neither did yours. We should cover that as well like this:
return sizeof($result) == 2 ?
$finalResult[$result[0]] = $result[1]
: sizeof($result) > 2 ? $finalResult[$result[0]] = array_slice($value, 0, sizeof($value) - 1) : ;
Now for the for issue:
What a for does is basically iterating through every item of the array (no matter how long) and do something with it. Ideally, native functions use memory addresses to find one element at the time, do something with it and free the memory. I said ideally, because sometimes whoever write the code through a foe behind it. I will try to write a blog post about this since it sounds like a great issue to raise.
Mostly, what I can tell you after 12 years of programming is that you learn by doing. And you usually learn when something does not work out. I learned my lesson with for after a server failed with 503( was having timeout issues) when my db got to ~1 mil entries.
It is great that you want to learn and improve and I think that if you stick to what you're doing you're gonna get there. Just don't be afraid to test and try other things. Whenever I have a similar issue I go to the manual and check what I can use. If no such functions is in the manual I add it to my helpers file (that I import everywhere) and try it with lots of data. If it fails I go back and try other options.
I try avoiding fors as much as possible and rely on native functions. I think your result does the job nicely, these are just alternatives. I am pretty rusty with my php (using Laravel for 4 years), but when I did work in vanilla php I used to rely on these 4 functions like crazy: array_map, array_keys, array_values, array_column
â Indra
Apr 5 at 20:47
Thank you for responding. But this doesn't address the case where the array contains more than one additional element. See the 3rd code block in the question.
â pbarney
Apr 5 at 20:55
@pbarney you're right. let me check another solution
â Indra
Apr 5 at 20:57
Is your general thesis that using foreach loops are a bad idea? If so, why? I'm looking for wisdom to help me grow as a programmer.
â pbarney
Apr 5 at 21:05
check out my edit and feel free to give me feedback
â Indra
Apr 5 at 21:24
 |Â
show 2 more comments
up vote
2
down vote
Try this:
$finalResult=;
array_map(function ($value) use (&$finalResult)
$result = array_values($value);
return $finalResult[$result[0]] = $result[1];
, $array);
return $finalResult;
I try avoiding fors as much as possible and rely on native functions. I started doing this since I read a book called refactoring to collections. This improved my code and the speed of my code.
Native functions are written in a memory effective way.
The function inside the array_map is called a closure. You can implement a closure anytime you need to do quick computations locally.
Your solution does the job right, but looks a little bit messy. I don't think there will be a speed issue with what you have though.
Edit:
$array = [
0 => ['level_id' => 1, 'level' => 'Trivial', 'description' => 'Foobar'],
1 => ['level_id' => 2, 'level' => 'Moderate', 'description' => 'Foobar'],
2 => ['level_id' => 3, 'level' => 'Challenging', 'description' => 'Foobar'],
3 => ['level_id' => 4, 'level' => 'Formidable'],
];
Using this:
$finalResult = ;
array_map(function ($value) use (&$finalResult)
$result = array_values($value);
return sizeof($result) == 2 ?
$finalResult[$result[0]] = $result[1]
: $finalResult[$result[0]] = array_slice($value, 0, sizeof($value) - 1);
, $array);
You will always get the right answer no matter how many columns you have or how many combinations you have.
- sizeof($result) == 2 ? $finalResult[$result[0]] = $result[1] if teh array has 2 entries use the first as id and second as value
- $finalResult[$result[0]] = array_slice($value, 0, sizeof($value) - 1); // if it has more than 2 entries then 2 entries remove the first (array_slice($value, 0, sizeof($value) - 1)). This does not cover the case where there is only one entry, but neither did yours. We should cover that as well like this:
return sizeof($result) == 2 ?
$finalResult[$result[0]] = $result[1]
: sizeof($result) > 2 ? $finalResult[$result[0]] = array_slice($value, 0, sizeof($value) - 1) : ;
Now for the for issue:
What a for does is basically iterating through every item of the array (no matter how long) and do something with it. Ideally, native functions use memory addresses to find one element at the time, do something with it and free the memory. I said ideally, because sometimes whoever write the code through a foe behind it. I will try to write a blog post about this since it sounds like a great issue to raise.
Mostly, what I can tell you after 12 years of programming is that you learn by doing. And you usually learn when something does not work out. I learned my lesson with for after a server failed with 503( was having timeout issues) when my db got to ~1 mil entries.
It is great that you want to learn and improve and I think that if you stick to what you're doing you're gonna get there. Just don't be afraid to test and try other things. Whenever I have a similar issue I go to the manual and check what I can use. If no such functions is in the manual I add it to my helpers file (that I import everywhere) and try it with lots of data. If it fails I go back and try other options.
I try avoiding fors as much as possible and rely on native functions. I think your result does the job nicely, these are just alternatives. I am pretty rusty with my php (using Laravel for 4 years), but when I did work in vanilla php I used to rely on these 4 functions like crazy: array_map, array_keys, array_values, array_column
â Indra
Apr 5 at 20:47
Thank you for responding. But this doesn't address the case where the array contains more than one additional element. See the 3rd code block in the question.
â pbarney
Apr 5 at 20:55
@pbarney you're right. let me check another solution
â Indra
Apr 5 at 20:57
Is your general thesis that using foreach loops are a bad idea? If so, why? I'm looking for wisdom to help me grow as a programmer.
â pbarney
Apr 5 at 21:05
check out my edit and feel free to give me feedback
â Indra
Apr 5 at 21:24
 |Â
show 2 more comments
up vote
2
down vote
up vote
2
down vote
Try this:
$finalResult=;
array_map(function ($value) use (&$finalResult)
$result = array_values($value);
return $finalResult[$result[0]] = $result[1];
, $array);
return $finalResult;
I try avoiding fors as much as possible and rely on native functions. I started doing this since I read a book called refactoring to collections. This improved my code and the speed of my code.
Native functions are written in a memory effective way.
The function inside the array_map is called a closure. You can implement a closure anytime you need to do quick computations locally.
Your solution does the job right, but looks a little bit messy. I don't think there will be a speed issue with what you have though.
Edit:
$array = [
0 => ['level_id' => 1, 'level' => 'Trivial', 'description' => 'Foobar'],
1 => ['level_id' => 2, 'level' => 'Moderate', 'description' => 'Foobar'],
2 => ['level_id' => 3, 'level' => 'Challenging', 'description' => 'Foobar'],
3 => ['level_id' => 4, 'level' => 'Formidable'],
];
Using this:
$finalResult = ;
array_map(function ($value) use (&$finalResult)
$result = array_values($value);
return sizeof($result) == 2 ?
$finalResult[$result[0]] = $result[1]
: $finalResult[$result[0]] = array_slice($value, 0, sizeof($value) - 1);
, $array);
You will always get the right answer no matter how many columns you have or how many combinations you have.
- sizeof($result) == 2 ? $finalResult[$result[0]] = $result[1] if teh array has 2 entries use the first as id and second as value
- $finalResult[$result[0]] = array_slice($value, 0, sizeof($value) - 1); // if it has more than 2 entries then 2 entries remove the first (array_slice($value, 0, sizeof($value) - 1)). This does not cover the case where there is only one entry, but neither did yours. We should cover that as well like this:
return sizeof($result) == 2 ?
$finalResult[$result[0]] = $result[1]
: sizeof($result) > 2 ? $finalResult[$result[0]] = array_slice($value, 0, sizeof($value) - 1) : ;
Now for the for issue:
What a for does is basically iterating through every item of the array (no matter how long) and do something with it. Ideally, native functions use memory addresses to find one element at the time, do something with it and free the memory. I said ideally, because sometimes whoever write the code through a foe behind it. I will try to write a blog post about this since it sounds like a great issue to raise.
Mostly, what I can tell you after 12 years of programming is that you learn by doing. And you usually learn when something does not work out. I learned my lesson with for after a server failed with 503( was having timeout issues) when my db got to ~1 mil entries.
It is great that you want to learn and improve and I think that if you stick to what you're doing you're gonna get there. Just don't be afraid to test and try other things. Whenever I have a similar issue I go to the manual and check what I can use. If no such functions is in the manual I add it to my helpers file (that I import everywhere) and try it with lots of data. If it fails I go back and try other options.
Try this:
$finalResult=;
array_map(function ($value) use (&$finalResult)
$result = array_values($value);
return $finalResult[$result[0]] = $result[1];
, $array);
return $finalResult;
I try avoiding fors as much as possible and rely on native functions. I started doing this since I read a book called refactoring to collections. This improved my code and the speed of my code.
Native functions are written in a memory effective way.
The function inside the array_map is called a closure. You can implement a closure anytime you need to do quick computations locally.
Your solution does the job right, but looks a little bit messy. I don't think there will be a speed issue with what you have though.
Edit:
$array = [
0 => ['level_id' => 1, 'level' => 'Trivial', 'description' => 'Foobar'],
1 => ['level_id' => 2, 'level' => 'Moderate', 'description' => 'Foobar'],
2 => ['level_id' => 3, 'level' => 'Challenging', 'description' => 'Foobar'],
3 => ['level_id' => 4, 'level' => 'Formidable'],
];
Using this:
$finalResult = ;
array_map(function ($value) use (&$finalResult)
$result = array_values($value);
return sizeof($result) == 2 ?
$finalResult[$result[0]] = $result[1]
: $finalResult[$result[0]] = array_slice($value, 0, sizeof($value) - 1);
, $array);
You will always get the right answer no matter how many columns you have or how many combinations you have.
- sizeof($result) == 2 ? $finalResult[$result[0]] = $result[1] if teh array has 2 entries use the first as id and second as value
- $finalResult[$result[0]] = array_slice($value, 0, sizeof($value) - 1); // if it has more than 2 entries then 2 entries remove the first (array_slice($value, 0, sizeof($value) - 1)). This does not cover the case where there is only one entry, but neither did yours. We should cover that as well like this:
return sizeof($result) == 2 ?
$finalResult[$result[0]] = $result[1]
: sizeof($result) > 2 ? $finalResult[$result[0]] = array_slice($value, 0, sizeof($value) - 1) : ;
Now for the for issue:
What a for does is basically iterating through every item of the array (no matter how long) and do something with it. Ideally, native functions use memory addresses to find one element at the time, do something with it and free the memory. I said ideally, because sometimes whoever write the code through a foe behind it. I will try to write a blog post about this since it sounds like a great issue to raise.
Mostly, what I can tell you after 12 years of programming is that you learn by doing. And you usually learn when something does not work out. I learned my lesson with for after a server failed with 503( was having timeout issues) when my db got to ~1 mil entries.
It is great that you want to learn and improve and I think that if you stick to what you're doing you're gonna get there. Just don't be afraid to test and try other things. Whenever I have a similar issue I go to the manual and check what I can use. If no such functions is in the manual I add it to my helpers file (that I import everywhere) and try it with lots of data. If it fails I go back and try other options.
edited Apr 5 at 21:25
answered Apr 5 at 20:45
Indra
1214
1214
I try avoiding fors as much as possible and rely on native functions. I think your result does the job nicely, these are just alternatives. I am pretty rusty with my php (using Laravel for 4 years), but when I did work in vanilla php I used to rely on these 4 functions like crazy: array_map, array_keys, array_values, array_column
â Indra
Apr 5 at 20:47
Thank you for responding. But this doesn't address the case where the array contains more than one additional element. See the 3rd code block in the question.
â pbarney
Apr 5 at 20:55
@pbarney you're right. let me check another solution
â Indra
Apr 5 at 20:57
Is your general thesis that using foreach loops are a bad idea? If so, why? I'm looking for wisdom to help me grow as a programmer.
â pbarney
Apr 5 at 21:05
check out my edit and feel free to give me feedback
â Indra
Apr 5 at 21:24
 |Â
show 2 more comments
I try avoiding fors as much as possible and rely on native functions. I think your result does the job nicely, these are just alternatives. I am pretty rusty with my php (using Laravel for 4 years), but when I did work in vanilla php I used to rely on these 4 functions like crazy: array_map, array_keys, array_values, array_column
â Indra
Apr 5 at 20:47
Thank you for responding. But this doesn't address the case where the array contains more than one additional element. See the 3rd code block in the question.
â pbarney
Apr 5 at 20:55
@pbarney you're right. let me check another solution
â Indra
Apr 5 at 20:57
Is your general thesis that using foreach loops are a bad idea? If so, why? I'm looking for wisdom to help me grow as a programmer.
â pbarney
Apr 5 at 21:05
check out my edit and feel free to give me feedback
â Indra
Apr 5 at 21:24
I try avoiding fors as much as possible and rely on native functions. I think your result does the job nicely, these are just alternatives. I am pretty rusty with my php (using Laravel for 4 years), but when I did work in vanilla php I used to rely on these 4 functions like crazy: array_map, array_keys, array_values, array_column
â Indra
Apr 5 at 20:47
I try avoiding fors as much as possible and rely on native functions. I think your result does the job nicely, these are just alternatives. I am pretty rusty with my php (using Laravel for 4 years), but when I did work in vanilla php I used to rely on these 4 functions like crazy: array_map, array_keys, array_values, array_column
â Indra
Apr 5 at 20:47
Thank you for responding. But this doesn't address the case where the array contains more than one additional element. See the 3rd code block in the question.
â pbarney
Apr 5 at 20:55
Thank you for responding. But this doesn't address the case where the array contains more than one additional element. See the 3rd code block in the question.
â pbarney
Apr 5 at 20:55
@pbarney you're right. let me check another solution
â Indra
Apr 5 at 20:57
@pbarney you're right. let me check another solution
â Indra
Apr 5 at 20:57
Is your general thesis that using foreach loops are a bad idea? If so, why? I'm looking for wisdom to help me grow as a programmer.
â pbarney
Apr 5 at 21:05
Is your general thesis that using foreach loops are a bad idea? If so, why? I'm looking for wisdom to help me grow as a programmer.
â pbarney
Apr 5 at 21:05
check out my edit and feel free to give me feedback
â Indra
Apr 5 at 21:24
check out my edit and feel free to give me feedback
â Indra
Apr 5 at 21:24
 |Â
show 2 more comments
Sign up or log in
StackExchange.ready(function ()
StackExchange.helpers.onClickDraftSave('#login-link');
);
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
StackExchange.ready(
function ()
StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2fcodereview.stackexchange.com%2fquestions%2f191348%2fconverting-a-query-result-into-an-key-value-pairs-for-a-lookup-table%23new-answer', 'question_page');
);
Post as a guest
Sign up or log in
StackExchange.ready(function ()
StackExchange.helpers.onClickDraftSave('#login-link');
);
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Sign up or log in
StackExchange.ready(function ()
StackExchange.helpers.onClickDraftSave('#login-link');
);
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Sign up or log in
StackExchange.ready(function ()
StackExchange.helpers.onClickDraftSave('#login-link');
);
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
array_column('level') will give you that php.net/manual/en/function.array-column.php. Check out array_keys also for reference
â Indra
Apr 5 at 20:15
@Indra, with
array_column()
I would need to explicitly indicate the column names, wouldn't I?â pbarney
Apr 5 at 20:25
yeah, but that can be a variable. like in this case $column='level'. array_column($column) and you set that as needed
â Indra
Apr 5 at 20:28
Unfortunately, I would also need to specify the column that contains my row id's for the key. My method above doesn't care about the names of the columns.
â pbarney
Apr 5 at 20:29
array_combine(array_column($col1), array_column($col2)) ? Ah.. this would be so much easier using collections. Or a combo of array_keys and array_values or an array_map should work.
â Indra
Apr 5 at 20:33