You are here: Re: [PHP] Help with a home-grown function « PHP « IT news, forums, messages
Re: [PHP] Help with a home-grown function

Posted by Andrι Medeiros on 07/22/05 14:58

On Thu, 2005-07-21 at 13:37 -0700, Dan Trainor wrote:
> Hello, all -
>
> I've been looking around for a function that would tell me if a $value
> in a $key=>$value array was empty, and I could not find one. So I
> decided to make my own. Even if I am re-inventing the wheel, I thought
> that the practice might be good for me.
>
> However, my function doesn't *quite* work, and I'm having a difficult
> time finding out why. The code is as follows:
>
> function findMissingVals($workingArray) {
> $newcount = count($workingArray);
> for ($i = 0; $i <= $newcount; $i++) {
> if (empty($workingArray['$i'])) {
> return 1;
> }
> }
> }
>
> So it takes in $workingArray as an array, runs a loop, checks $i, yada
> yada. The thing is, that sometimes the function does not return 1, even
> when it should.
>
> I was hoping some experienced eyes could take a gander at this and give
> me some pointers.
>
> Thanks!
> -dant
>
----------------------------8<-----------------------------
function findMissingVals( &$workingArray ) {
$arrayMissingVals = Array();
foreach( $arrayMissingVals as $key => $value ) {
if( is_array( $value ) ) {
array_merge( $arrayMissingVals, findMissingVals( &$value ) );
}
else if( empty( $value ) ) {
array_push( $arrayMissingVals, &$workingArray[$key] );
}

}

return( $arrayMissingVals );
}
----------------------------8<-----------------------------

Recursive function that will check arrays and return references to the
empty keys. Then, you can do whatever you like.

If you need something simpler, do

----------------------------8<-----------------------------
if( in_array( $workingArray, null ) ) {
// Do something
}
----------------------------8<-----------------------------

Same goes for 0, '', etc.

Good luck!

 

Navigation:

[Reply to this message]


УдалСнная Ρ€Π°Π±ΠΎΡ‚Π° для программистов  •  Как Π·Π°Ρ€Π°Π±ΠΎΡ‚Π°Ρ‚ΡŒ Π½Π° Google AdSense  •  England, UK  •  ΡΡ‚Π°Ρ‚ΡŒΠΈ Π½Π° английском  •  PHP MySQL CMS Apache Oscommerce  •  Online Business Knowledge Base  •  DVD MP3 AVI MP4 players codecs conversion help
Home  •  Search  •  Site Map  •  Set as Homepage  •  Add to Favourites

Copyright © 2005-2006 Powered by Custom PHP Programming

Π‘Π°ΠΉΡ‚ ΠΈΠ·Π³ΠΎΡ‚ΠΎΠ²Π»Π΅Π½ Π² Π‘Ρ‚ΡƒΠ΄ΠΈΠΈ Π’Π°Π»Π΅Π½Ρ‚ΠΈΠ½Π° ΠŸΠ΅Ρ‚Ρ€ΡƒΡ‡Π΅ΠΊΠ°
ΠΈΠ·Π³ΠΎΡ‚ΠΎΠ²Π»Π΅Π½ΠΈΠ΅ ΠΈ ΠΏΠΎΠ΄Π΄Π΅Ρ€ΠΆΠΊΠ° Π²Π΅Π±-сайтов, Ρ€Π°Π·Ρ€Π°Π±ΠΎΡ‚ΠΊΠ° ΠΏΡ€ΠΎΠ³Ρ€Π°ΠΌΠΌΠ½ΠΎΠ³ΠΎ обСспСчСния, поисковая оптимизация