Jump to content

Recommended Posts

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

Link to comment
https://forums.phpfreaks.com/topic/302751-check-array-indexes/#findComment-1540391
Share on other sites

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 } 
Link to comment
https://forums.phpfreaks.com/topic/302751-check-array-indexes/#findComment-1540403
Share on other sites

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.

Link to comment
https://forums.phpfreaks.com/topic/302751-check-array-indexes/#findComment-1540417
Share on other sites

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 by benanamen
Link to comment
https://forums.phpfreaks.com/topic/302751-check-array-indexes/#findComment-1540419
Share on other sites

This thread is more than a year old. Please don't revive it unless you have something important to add.

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.

Guest
Reply to this topic...

×   Pasted as rich text.   Restore formatting

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

×
×
  • Create New...

Important Information

We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue.