php compare two associative arrays using array_intersect() -
i have 2 associative arrays. in fact, both obtained after json decoding.i'm comparing presence of same info in both array , obtaining count. array_intersect method giving wrong result. count should have been 2 giving 4. please point wrong.
arrays:
array ( [question_0] => b [question_1] => b [question_2] => [question_3] => c )
array ( [question_0] => [question_1] => b [question_2] => b [question_3] => c [question_4] => c )
code:
$temp = '{"question_0": "b", "question_1": "b", "question_2": "a", "question_3": "c" }'; $a = json_decode($temp,true); print_r($a); require_once "classes/config.readonly.php"; $data['token'] = '61db6e01cdd809e65e0490158b569b69'; $sql = "select * quiz_logger token = " . "'" . $data['token'] . "'" ; $db->query($sql); while($row = $result->fetch_object() ){ $answers = $row->data; } $ans = json_decode($answers,true); print_r($ans['data']); echo count(array_intersect($a, $ans['data']));
array_intersect comparing values
try utilize array_intersect_assoc http://de2.php.net/manual/en/function.array-intersect-assoc.php
echo count(array_intersect_assoc($a, $ans['data'])); array_intersect_assoc comparing keys , values, think want
php arrays json associative-array
No comments:
Post a Comment