NotionCommotion Posted December 15, 2016 Share Posted December 15, 2016 Given either $arr1a or $arr1b, what is the best way to confirm that $arr2 has only the three indexes a, b, and c? $arr1a=['a','b','c']; $arr1b=['a'=>null,'b'=>null,'c'=>null]; $arr2=['a'=>1,'b'=>0,'c'=>0]; Quote Link to comment Share on other sites More sharing options...
cyberRobot Posted December 15, 2016 Share Posted December 15, 2016 Have you tried array_diff_key() with $arr1b? If the return value is empty, the keys are good. http://php.net/manual/en/function.array-diff-key.php With $arr1a, you would probably need to use array_keys() on $arr2. And then compare the return value to $arr1a. http://php.net/manual/en/function.array-keys.php Quote Link to comment Share on other sites More sharing options...
NotionCommotion Posted December 15, 2016 Author Share Posted December 15, 2016 Thanks CyberRobot. A requirement is has only the three indexes, and a single array_diff_key() will not work. I could then use a second array_diff_key() or compare the count, but expect one of the other countless array functions might be more suitable. var_dump(array_diff_key(['a'=>null,'b'=>null,'c'=>null], ['a'=>1,'b'=>0,'c'=>2])); //array(0) { } var_dump(array_diff_key(['a'=>null,'b'=>null], ['a'=>1,'b'=>0,'c'=>2])); //array(0) { } var_dump(array_diff_key(['a'=>null,'b'=>null,'c'=>null], ['a'=>1,'b'=>0])); //array(1) { ["c"]=> NULL } Quote Link to comment Share on other sites More sharing options...
Barand Posted December 15, 2016 Share Posted December 15, 2016 Perhaps check that (pseudocode) count(array_intersect_key) == 3 AND array_diff_key is empty Quote Link to comment Share on other sites More sharing options...
NotionCommotion Posted December 15, 2016 Author Share Posted December 15, 2016 Thanks Barand, Yes, that would work, and is what I meant by comparing the counts. Still seems there would be a better way. Quote Link to comment Share on other sites More sharing options...
benanamen Posted December 15, 2016 Share Posted December 15, 2016 Still seems there would be a better way. Where is the data coming from? Quote Link to comment Share on other sites More sharing options...
NotionCommotion Posted December 15, 2016 Author Share Posted December 15, 2016 Where is the data coming from? I receive JSON data via a socket. Certain data is expected based on the database. I wish to confirm that the JSON is not providing more or less data that is expected. I can easily enough do the array_diff_key along with confirm index counts are equal solution, but just felt there was a really slick way which would be a good thing to learn. Quote Link to comment Share on other sites More sharing options...
benanamen Posted December 15, 2016 Share Posted December 15, 2016 (edited) That is a lot more useful info than how to handle your attempt at it. Not sure if it is, but it was smelling of an XY Problem to me. Are you able to provide details on the "Certain Data" and exactly what it means regarding more or less data? Any other details may also be helpful. Are you in control of the source data? If not, what are your options in retrieving it? Are we talking about a third party API? Edit* Is this a third thread regarding the same project as your other two threads? https://forums.phpfreaks.com/topic/302727-multiple-updates/page-2 https://forums.phpfreaks.com/topic/302752-insert-on-duplicate-key-update-with-composite-key/ Edited December 15, 2016 by benanamen Quote Link to comment Share on other sites More sharing options...
Recommended Posts
Join the conversation
You can post now and register later. If you have an account, sign in now to post with your account.