Sunday 15 September 2013

php - Create an Array with a KEY if values exists in two arrays? -



php - Create an Array with a KEY if values exists in two arrays? -

i having problem handling arrays. want "compare" 2 arrays see if there's matching "usernames" in both arrays. not sure if using in_array() function properly

this how arrays like:

user array 1:

array ( [0] => array ( [username] => lndp [station] => d08 ) [1] => array ( [username] => acman [station] => d06 ) [2] => array ( [username] => vter [station] => d13 ) ) //the users have matched memo_code $user = array(); while($row = mysqli_fetch_assoc($get_user_result)){ $user[] = array( "username" => $row['username'], "station" => $row['station_number'] ); }

memo array 2:

array ( [0] => array ( [username] => vter[aht_value] => 333 ) [1] => array ( [username] => acman [aht_value] => 456 ) [2] => array ( [username] => acyl [aht_value] => 789 ) ) $memo = array(); while ($row = mysqli_fetch_assoc($dbh2_result)){ $memo[] = array( "username" => $row['memo_code'], "aht_value" => $row['avg_handle_time'] ); }

i want check every "username" in memo array match "username" in user array. if match, want create array username, station, aht_value so:

array ( [0] => array ( [username] => acman [aht_value] => 456 [station] => d06 ) ) //creating array 3 comparing 1 , 2 using key value of "username" array 2 $result = array(); //$m = key //$m['username'] = key value foreach($memo $m => $m['username']){ //if username in array 2 in array 1 if( in_array( $m, $user) ){ //if aht_value not null if( $memo['aht_value'] != null ){ $result[] = "username: " . $user['username'] . "aht_value: " .$m['aht_value'] . "position: " . $user['position']. "<br>"; } //else if aht_value null else{ $result[] = "username: " . $user['username'] . "aht_value: na position: " . $user['position'] . "<br>"; } } //if there no matching username something? else{ echo "no match"; } } $final_result = json_encode($result); echo $final_result;

error message

warning: cannot utilize scalar value array in line 97 : foreach($memo $m => $m['username']){

if need clarify things please ask. after creating 3rd array utilize json_encode create ajax phone call , utilize type get.

thanks in advance help!

instead of downvoting can please tell me best edit

first, people not need provide reason downvote, if did wouldn't constructive, "i disagree" - "the question poor".

second, question not 100% clear, code little rough around edges before editing , when got downvotes, , still unclear after edits.

you not state why current effort fails, does, supposed do, etc.

the issues

your arrays not seem have matching keys able pair up, given 2nd dimension, trying access cross section values have no matching keys complicated.

it's not impossible, given huge code block required seek create sense of data, it's not practical. re-think in how utilize info needed really.

eg array_1 has keys of "username" , "station", yet array_2 has keys of "memo_code" , "aht_value. there no way correlate 1 thing using keys because there no key pairs 1 array another.

when loop array, array_1, can matching value in array_2 current loop's value array_1, not have index of array_2's value matched array_1, have value, means cannot obtain additional info array_2, have value match, , not index.

it's messy, really, given scenario not complex plenty warrant such complex scenario - in fact no scenario should above, there's improve "approach" rather producing such poor "fix".

other solution

why in array_2 there key "memo_code" seems "username" array_1? must wanting marry "username" "memo_code". why not have "username" in array_2 well, if that's is?

going further, 1 time again wrong based on info in question - why not have "aht_value" in array_1 , have no array_2?

maybe there need 2 arrays, not shown in question, (confusing , little garbled) question, "could" fine:

$single_array = array ( "0" => array ( "username" => "lndp", "station" => "d08", "aht_value" => "whatever") "1" => array ( "username" => "acman", "station" => "d06", "aht_value" => "something" ) "2" => array ( "username" => "vter", "station" => "d13", "aht_value" => null ) );

you can values in 1 go relate 1 username, if on different tables bring together (i presume have foreign keys/relationships between 2 tables.

if need 2 arrays

if need maintain both arrays (for whatever reason scenario demands) , cannot alter approach, can @ to the lowest degree alter have kind of corresponding index in each array? can match 1 index another, take info matched pair.

such indexes usernames:

$user = array ( "lndp" => array ( "station" => "d08" ) "acman" => array ( "station" => "d06" ) "vter" => array ( "station" => "d13" ) ); $memo = array ( "vter" => array ( "aht_value" => "" ) "acman" => array ( "aht_value" => "456" ) "acyl" => array ( "aht_value" => "789" ) );

then loop 1 of arrays, , index (username) matches, can rest of info index , append new array.

if must way have asked

again, not impossible, you'd need huge block of code would:

loop array_1 , matching value pair array_2 current loop's value use identified value array_2 , access 2nd array separately , find matching value's index use found index additional info required append "new" array start going through loop again

hope of helps.

php arrays multidimensional-array mysqli associative-array

No comments:

Post a Comment