1

I have a bunch of arrays in an array ex.

$array =
         array(
            array(/../),
            array(/../),
            array(/../),
            //upto 100-200 arrays
         );

After that, I will use foreach to echo all of them. There is some checking here whether the $key is is_numeric() or is_string(), for example:

array(
   'the_key_here_is_numeric',
   'string' => 'the key is string'
);

So I have a foreach like this:

foreach($array as $arr => $arrays) {
   foreach($arrays as $key => $value) {
      if(is_numeric($key)) {
         /.../
      }
      if(is_string($key)) {
         /../
      }
   }
   echo /../;
}

When I tested this using KCacheGrind, obviously the is_string() and is_numeric() will be used multiple times, my question is, will this affect the performance? If so, is there a better way to do this?

Dumb Question
  • 365
  • 1
  • 3
  • 14
  • Because some people here like giving down votes just like that. You must get used to it :( – nospor Oct 27 '16 at 10:40
  • Yeah thought so, at least, before they do this, say something, how can a newbie like me get a help when somebody like him/her roaming here. They're not even helping yet they do this. – Dumb Question Oct 27 '16 at 10:44
  • I know that. Tell that to them. – nospor Oct 27 '16 at 10:52
  • The problem with that is I can't fight back because I do not know who they are and can't report them. – Dumb Question Oct 27 '16 at 10:53
  • I had exactly the same problem at the beginning here. So I get used to it :/ – nospor Oct 27 '16 at 10:53
  • Yeah, thank you. Now I know I am not the only here getting bullied by the big shots. Haha, thanks by the way. – Dumb Question Oct 27 '16 at 11:00
  • You are welcome. It is good to know you are not alone ;) – nospor Oct 27 '16 at 11:02
  • Please read [Why am I downvoted?](http://meta.stackexchange.com/questions/121350/ive-just-been-downvoted-how-should-i-react). It is not bullying - there is no need to accuse anyone of it. – Pieter van den Ham Oct 27 '16 at 11:23
  • Yes `is_string` and `is_numeric` will be called multiple times but is that a problem? Btw. I assume you know of the `continue` statement to directly continue in the (for, while or do) loop. This shortcut might shave a few ms. of time. But having said that both `is_numeric` and `is_string` are lightingly fast,. Another way would be to find out if there is a way to build your array in such a way that `is_numeric` is not needed if _all_ content is numeric from the start. It gets more a design question. – theking2 Feb 02 '22 at 14:04

1 Answers1

0

Just use else. Then condition will be check only once

Not

if(is_numeric($key)) {
         /.../
      }
      if(is_string($key)) {
         /../
      }

But

  if(is_string($key)) {
     /../
  } else {
     /.../
  }
nospor
  • 4,190
  • 1
  • 16
  • 25
  • Okay I understand your point, but I do still have question(s) that are not answered, obviously in your code `is_string()` will be called many times, does the `is_string()` will affect the performance if it's called many times? – Dumb Question Oct 27 '16 at 10:37
  • There is only max 200 iteration so it will be fine. Now we reduced by half this IFs ;) Beside, what do you want with this keys? Maybe there is better solution – nospor Oct 27 '16 at 10:39
  • The arrays may go more than 200, yeah your answer seems to reduce something, I use this to generate `html tags` and `attributes`. – Dumb Question Oct 27 '16 at 10:41
  • Can you tell sth more about this. Right now it is hard to help you more than I did it. – nospor Oct 27 '16 at 10:43
  • What is sth? here is the example of the array `array('div', 'class' => 'div_class')`. – Dumb Question Oct 27 '16 at 10:46
  • sth = Can you tell something more about this? – nospor Oct 27 '16 at 10:51
  • I gave you an example. – Dumb Question Oct 27 '16 at 10:53
  • It is okay now, there is no problem now (I think), so I accepted your answer because it helps me. – Dumb Question Oct 27 '16 at 10:59
  • Nospor, I will ask an additional question, is this kind of code good practice for production? Right now I am producing a hundred to 500 arrays. – Dumb Question Oct 28 '16 at 12:03
  • @DumbQuestion I still do not know the all context of your problem so this is hard to say. For now 500 array seems a little to bit - but as I said, I do not know all context. – nospor Oct 31 '16 at 09:15
  • This is my example code: `$header/$body/$footer = array(array('div', 'class' => 'div_class'), array(/.contents goes here so on./), array('div' => 'end'))` `$display = array($header, $body, $footer);`. Then, I will use a 3 nested `foreach()` loop to output all of the html contents. – Dumb Question Oct 31 '16 at 10:17
  • But I need to know **why** do you have such structure and **why** do you need it. – nospor Oct 31 '16 at 10:26
  • That way there is no overwritten into key and I use it to generate html tags, attributes, etc. – Dumb Question Oct 31 '16 at 10:31
  • But why do you have such structure? Why do you build your html code in this way? – nospor Oct 31 '16 at 10:33
  • For me, it is more readable than just echo `
    ...
    ` all the time, and when you use `View Page Source`, the codes will be hard to read because the output will have no spaces or new line.
    – Dumb Question Oct 31 '16 at 10:37
  • Ouch... you've chosen really really bad way. Try to read about templates system like Twig or just simple template in php code (It is popular in ZF). What you are doing now it is really **bad** idea. – nospor Oct 31 '16 at 10:39
  • Oh okay, I am sorry I am not very a programmer guy, I just want to make my codes more faster for users. Is there any alternative for this? Before I asked this I search through here how to generate `Html` codes in `php` and most of them make a function that will use a `foreach` loop to output the `html` codes. Should I use this? – Dumb Question Oct 31 '16 at 10:45
  • I have no idea why someone would like to use foreach to generate html code. Well, except your idea with arrays of course ;) If you do not want to use templates, just use echo and add \n at the end if you really want to have nice source, eg: echo '
    blabla
    '."\n";
    – nospor Oct 31 '16 at 11:03
  • Oh, that's right. Thank you. I will use that for dynamic `html` and by the way I tried to use this and the response times reduced upto 1s – Dumb Question Oct 31 '16 at 11:16