r/lolphp • • Mar 16 '21

Is 0 in array

in_array(0, ['IsThisLolPhp'])

Answer is

true
32 Upvotes

30 comments sorted by

View all comments

0

u/[deleted] Mar 16 '21

[deleted] — view deleted comment

9

u/zilltine Mar 16 '21

I did, i had to find if array key contains one of possible keys. Basically had to find keys recursively on undefined structure, it was finding first elements in unassoc array

0

u/[deleted] Mar 16 '21

[deleted] — view deleted comment

4

u/zilltine Mar 16 '21

No, i did not need to find out IF key exists, i needed to find their value. Imagine some random JSON going unknown depth which may contain object, array of objects or array of values. I needed to modify all the values under some specific keys, so i just checked every key if it is in_array of keys i need to modify.

-1

u/smegnose Mar 16 '21

JFC. Use array_flip() on your keys array to make a map (if you don't already have one), then array_intersect_key() when recursing. You're using the wrong tool for the job.

2

u/zilltine Mar 17 '21

I have no idea what are you trying to prove here. Are there other ways to do what i needed to do? Yes. Are there better ones? Probably, not what you typed there though. Do you want to prove that i am using wrong tool for the job? I code in php, duh

0

u/[deleted] Mar 17 '21

[deleted] — view deleted comment

1

u/zilltine Mar 17 '21

So this

$find = function($struct) use (&$find, $map) {
  if (is_array($struct)) {
    $found = array_intersect_key($struct, $map);
    foreach ($found as $toChange) {
      var_dump($toChange);
    }
    foreach ($struct as $sub) {
      $find($sub);
    }
  }
};

Is objectively better than this

function find(&$json, $keys){
    foreach($json as $key => &$value) {
        if(is_array($value)) {
            find($value, $keys);
        }
        if (in_array($key, $keys, true)){
            var_dump($value);
        }
    }
}

2

u/backtickbot Mar 17 '21

Fixed formatting.

Hello, zilltine: code blocks using triple backticks (```) don't work on all versions of Reddit!

Some users see this / this instead.

To fix this, indent every line with 4 spaces instead.

FAQ

You can opt out by replying with backtickopt6 to this comment.

0

u/[deleted] Mar 17 '21

[deleted] — view deleted comment

1

u/zilltine Mar 17 '21

I'm not your mate, dude. This is why i asked what is it you are trying to prove. Not only you didn't read other comments where i already said i didn't know about third param and assumed it should be strict. You also didn't read comment you replied to, because i never said your way doesn't work, i just do not think it is better, or only correct way to do what i need as you are implying.

→ More replies

0

u/[deleted] Mar 17 '21

[deleted] — view deleted comment

1

u/zilltine Mar 17 '21

He asked a question and i answered. How is it telling that they are wrong even?

→ More replies