1 (edited by MattF 2007-06-01 14:26)

Topic: Arrays and in_array

Just wanting to check on what I'm missing here. Everything works as expected, but I'm getting this php warning:

PHP Warning:  in_array() [<a href='function.in-array'>function.in-array</a>]: Wrong datatype for second argument

The relevant parts that it refers to are:

the array:

$clock_array = array();
$clock_array[] = $pun_config['o_base_url'].'/forums.php';
$clock_array[] = $pun_config['o_base_url'].'/index.php';

the in_array check and variables:

$domain = $_SERVER['SERVER_NAME'];
$script_path = $_SERVER['SCRIPT_NAME'];
$page = 'http://'.$domain.$script_path;

if (@in_array($page, $clock_array)) {

I've put the @ in to suppress the warnings temporarily, but I'd prefer to get it working correctly if possible. I haven't been able to find any relevant info on the php docs site.


Cheers,

Matt

Re: Arrays and in_array

Could we see the full script?

3

Re: Arrays and in_array

Smartys wrote:

Could we see the full script?

No probs. big_smile I'll start with the arrays file. This is the one I was enquiring over yesterday as to whether I could include the lang files in include/common.php.

<?php


// Which pages the clock will be displayed on

$clock_array = array();
$clock_array[] = $pun_config['o_base_url'].'/forums.php';
$clock_array[] = $pun_config['o_base_url'].'/index.php';


// Language files that will be included in each script

$lang_dir = PUN_ROOT.'lang/'.$pun_user['language'].'/';

$lang_array = array();

$lang_array[] = $lang_dir.'common.php';
$lang_array[] = $lang_dir.'delete.php';
$lang_array[] = $lang_dir.'forum.php';
$lang_array[] = $lang_dir.'fus.php';
$lang_array[] = $lang_dir.'gus.php';
$lang_array[] = $lang_dir.'help.php';
$lang_array[] = $lang_dir.'index.php';
$lang_array[] = $lang_dir.'login.php';
$lang_array[] = $lang_dir.'misc.php';
$lang_array[] = $lang_dir.'online.php';
$lang_array[] = $lang_dir.'pms.php';
$lang_array[] = $lang_dir.'portal.php';
$lang_array[] = $lang_dir.'post.php';
$lang_array[] = $lang_dir.'prof_reg.php';
$lang_array[] = $lang_dir.'profile.php';
$lang_array[] = $lang_dir.'register.php';
$lang_array[] = $lang_dir.'search.php';
$lang_array[] = $lang_dir.'topic.php';
$lang_array[] = $lang_dir.'userlist.php';

        foreach ($lang_array as $langfile)
        {
                require $langfile;
        }


?>

This is how the above script is included in common.php, (with relevant snippet above for placement identification):

// Attempt to load the common language file
@include PUN_ROOT.'lang/'.$pun_user['language'].'/common.php';
if (!isset($lang_common))
        exit('There is no valid language pack \''.pun_htmlspecialchars($pun_user['language']).'\' installed. Please reinstall a language of that name.');

//------------------------------------------//

require_once PUN_ROOT.'include/arrays.php';

//------------------------------------------//

This is the code which is included in footer.php, (with relevant snippet above for placement identification):

// START SUBST - <pun_footer>
ob_start();

//----------------------------------------//

$domain = $_SERVER['SERVER_NAME'];
$script_path = $_SERVER['SCRIPT_NAME'];
$page = 'http://'.$domain.$script_path;

if (@in_array($page, $clock_array)) {

?>

<div id="clockfooter" class="block">
        <div class="box">
                <div class="inbox">
                <div align="center"><embed menu="false" width="150" height="35" src="<?php echo $pun_config['o_base_url'] ?>/mod-addons/clock.swf"/>
                </div>
                </div>
        </div>
</div>

<?php

}

//----------------------------------------//

Cheers,

Matt

Re: Arrays and in_array

I would assume that your include file is not getting included. In the footer try adding an isset for $clock_array and verifying that the data is actually there.

I enjoy pie :)

5 (edited by MattF 2007-06-01 17:23)

Re: Arrays and in_array

Mediator wrote:

I would assume that your include file is not getting included. In the footer try adding an isset for $clock_array and verifying that the data is actually there.

I thought initially something like that may be happening, but it would appear to be receiving the values correctly as the clock does only display on the two pages defined by the array. But, just to be on the safe side, I've added an isset as you suggested. smile The error message is no longer popping up in the logs, (but that is due to the layout of the new construct, is it not?), and the clock is still appearing where it should. I've altered the in_array line to:

if (isset ($clock_array) && in_array ($page, $clock_array)) {

That's why it's bugging me. I know it's functioning as it should, but it throws that warning.


Cheers,

Matt

Re: Arrays and in_array

Try doing a print_r($clock_array);

7

Re: Arrays and in_array

elbekko wrote:

Try doing a print_r($clock_array);

Just given that a try, worked perfectly. Put the print_r just above the in_array line in footer.php, and got the output:

Array ( [0] => http://forums.bauchan.org/toyota/forums.php [1] => http://forums.bauchan.org/toyota/index.php )

Those are the two pages that are defined. Could this just be a php version thing? I've got 4.4.4 running on this server.


Cheers,

Matt

Re: Arrays and in_array

I don't know of any bugs with it hmm Weird error...

9

Re: Arrays and in_array

Is the best option to just suppress the error, so it doesn't keep appearing in the logs? At least I'm glad to hear it ain't due to a cockup in my scripting. big_smile

Re: Arrays and in_array

The best option would be to track down the bug and fix it tongue But error supression works too I guess.

11

Re: Arrays and in_array

elbekko wrote:

The best option would be to track down the bug and fix it tongue But error supression works too I guess.

big_smile big_smile

The question, then, would be where? I assume this has to be within php itself? How would one start on the path of finding the glitch? (Pointers to documentation are fine if it's an indepth path I take). big_smile

12

Re: Arrays and in_array

Just to rule out one possible cause, I've compiled the latest 4 version, (4.4.7), of php and popped that on. No difference. So at least it ain't due to a bug/quirk in that specific version of php. big_smile

13

Re: Arrays and in_array

I knew I'd track this devil down one day. big_smile It was the redirect function causing it. I needed to add the clock_array var to the global line in the redirect function. What a bugger this one has been to track down. big_smile