3
\$\begingroup\$

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?

Nick Udell
5,2471 gold badge29 silver badges68 bronze badges
asked Nov 20, 2014 at 13:35
\$\endgroup\$
1

2 Answers 2

3
\$\begingroup\$

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.

Jamal
35.2k13 gold badges134 silver badges238 bronze badges
answered Nov 20, 2014 at 14:52
\$\endgroup\$
1
  • \$\begingroup\$ Your assumption is correct, the outcome of combined array is indeed what I'm looking for! Thank you, this looks alot resourcefriendlier :) \$\endgroup\$ Commented Nov 20, 2014 at 15:25
1
\$\begingroup\$

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'];
 }
}
answered Nov 20, 2014 at 15:11
\$\endgroup\$
2
  • \$\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\$ Commented 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\$ Commented Nov 20, 2014 at 15:35

Your Answer

Draft saved
Draft discarded

Sign up or log in

Sign up using Google
Sign up using Email and Password

Post as a guest

Required, but never shown

Post as a guest

Required, but never shown

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.