I have the following arrays:
Array (db_values)
(
[0] => Array
(
[system_name] => object_car_brand
[value] => Alfa Romeo
[id] => 136
)
[1] => Array
(
[system_name] => object_car_model
[value] => Spider
[id] => 137
)
)
Array (db_attributes)
(
[0] => Array
(
[id] => 105
[system_name] => object_car_brand
)
[1] => Array
(
[id] => 106
[system_name] => object_car_model
)
)
I combine these two using the following code:
foreach($db_attributes as $db_attribute){
foreach($db_values as $db_value){
if($db_value["system_name"] === $db_attribute["system_name"]){
$update[$db_attribute["id"]] = $db_value["value"];
}
}
}
I do not think that this is the most resource friendly way of doing it, is there a better way?
2 Answers 2
I have made the assumption that there is a 1:1 relationships between the $attributes
and $values
array elements. With that I mean the array key in the $attributes
array corresponds with an entry in $values
array.
If that is the case it can be reduced to one foreach
loop by using the key from the attributes array:
$combined = []; // Make sure the $combined array exists.
foreach($attributes as $key => $attribute) {
// First check if the array key exists and that the 'system_name' is the same
if(array_key_exists($key, $values) && $attribute['system_name'] == $values[$key]['system_name']) {
$combined[$attribute['id']] = $values[$key]['value'];
}
}
This should produce the following array with the data you have provided:
Array(combined)
(
[106] => 'Alfa Romeo'
)
If my assumption is incorrect, then just ignore my answer.
-
\$\begingroup\$ Your assumption is correct, the outcome of combined array is indeed what I'm looking for! Thank you, this looks alot resourcefriendlier :) \$\endgroup\$Chilion– Chilion2014年11月20日 15:25:20 +00:00Commented Nov 20, 2014 at 15:25
If the $db_attributes
array is very large, it would save quite a few cycles to normalize that array first.
// normalized attributes
$attributes = array();
// loop all attr results
foreach( $db_attributes as $db_attribute ) {
// use system name as key
$attributes[ $db_attribute['system_name'] ] = $db_attribute['id'];
}
// final results
$update = array();
// loop all value results
foreach( $db_values as $db_value ) {
// check if value's system name exists in normalized attributes
if ( isset( $attributes[ $db_value['system_name'] ] ) ){
// yes, grab the id
$attr_id = $attributes[ $db_value['system_name'] ];
// add to update array using attr id as key
$update[ $attr_id ] = $db_value['value'];
}
}
-
\$\begingroup\$ This answer is correct and indeed alot better than what I have in my opening post. The reason I'm not using it is because the attributes array will never get larger than it is right now. Thank you for your hints! \$\endgroup\$Chilion– Chilion2014年11月20日 15:27:28 +00:00Commented Nov 20, 2014 at 15:27
-
2\$\begingroup\$ The top level keys of your arrays might be matching up by coincidence. Usually the top level keys from database results are ambiguous, so be careful. The author of the accepted answer also hinted at this. Thanks for the kind words :) \$\endgroup\$MrMaz– MrMaz2014年11月20日 15:35:09 +00:00Commented Nov 20, 2014 at 15:35
RecursiveArrayIterator
class. (php.net/manual/en/class.recursivearrayiterator.php). I never used it, so, I can't help much. \$\endgroup\$