PDA

View Full Version : php array issue



Nehm4
02-22-2010, 09:29 PM
Hey all,

In php I have an included file via "include_once". In that file there is another included file in the same way. In that included file I have a function that creates an array from another function. When I print_r the array, it's correct, and is_array returns true. However, when I run the array_keys function, it outputs an error saying that it's not an array. Anyone else have this issue?

Thanks!

-josh

PS:
The code


<?php

function arr_val_filt_2_var_pound_ret($v){
return '###' . $v . '###';
}
function var2child_replace_return($path, $text){
$return = '';
if(isset($text)){
$return = $text;
if(isset($path)){
$var_sig = '###';
$child = child_return($path);
$name = array_keys($child);
$name = array_filter($name, "arr_val_filt_2_var_pound_ret");
$text = str_replace($name, $child, $text);
$return = $text;
}
}
return $return;
}

function child_return($path){
global $cfh;
$return = array();
if(isset($path)){
$exceptions = array(
'.',
'..',
'asset',
'asset_share',
'edit'
);
$child = $cfh->contents($path);
foreach($child as $key => $value){
if($value['type'] == 'dir'){
if(!in_array($value['name'], $exceptions)){
$return[$value['name']] = include_once($value['dir'] . '/index.php');
}
}
}
}
return $return;
}

?>

JAY6390
02-22-2010, 11:10 PM
Can you show what the print_r had (I'm assuming it was for $child)?

Nehm4
02-23-2010, 01:15 AM
Jay,

Yes, and thankyou for posting.
That's what I don't get at all. I can print_r($child) and it prints out
Array ( [content_0] => 1 )
but here's the funny thing. After I put in the line print_r($child), the array is recognized, and works just fine for the php afterwards. Really funny insident. I'm thinking it's something about this being a return file once included.

-josh

Nehm4
02-23-2010, 04:39 AM
i found a fix to the problem. i dont' know why, but I have to change
$child = child_return($path);
to
$child = (array)child_return($path);

somehow the (array) fixes it like magic. i dont' know why I would have to call this, bc when the function itself is not in an outside included file, it works just fine. odd, very odd