本当ですか?ゼリーに聞いて!


32

バックグラウンド

Octave(および、拡張によりMATL)の非常に便利な真偽/偽の行列の解釈に触発され、JellyはȦ(Octaveスタイルのすべて)アトムを取得しました。

Ȧ入力として配列を受け取り、配列が空ではなく、ツリー構造のどこにも数字0(整数、浮動小数点、または複素数)を含まない場合は1を返します。それ以外の場合は0を返します

たとえば、配列[[]]は空ではなくゼロを含まないため真理ですが、[[0]]は最も内側のレベルに0を含むため偽です。

仕事

選択したプログラミング言語で、完全なプログラムまたは関数を作成します。これは、入力として空の、場合によってはギザギザの整数配列を取り、Ȧがそれぞれ1または0を返すかどうかを示す真実または偽の値を出力または返します。

あなたの提出物は、以下の規則に従わなければなりません。

  • 真偽値と偽の値は、すべての入力一貫している必要があります。つまり、Ȧが1を返すすべての配列は同じ真偽値にマップし、Ȧが0を返すすべての配列は同じ偽値にマップする必要があります。

  • 完全なプログラムは入力として配列の文字列表現しか受け取れないため、これは許可されています。ただし、返されるreprまたは同様の言語のカノカル表現を使用する必要があります。

    特に、配列の最初の要素の前にスペースがあるとは想定できません。

  • ご使用の言語がジャグ配列をネイティブに表現できない場合(のみ)、既存のプログラミング言語の標準的な構文を使用して、入力の文字列表現を使用できます。

  • 言語にギザギザの配列(リストやタプルなど)を表す方法がいくつかある場合は、そのうちの1つをサポートするだけで済みます。

  • あなたの言語自体がこのチャレンジへの有効な提出物であるビルトインを持っている場合、あなたはあなたの答えでそれを使うことはできません。他のすべてのビルトインは許可されます。

  • 配列操作と文字列操作の両方を使用して回答を投稿することをお勧めします。一方が他方よりも著しく短い場合でも。

  • すべての標準規則が適用されます。

バイト単位の最短コードが勝つように!

真実のテストケース

[1]
[10]
[[]]
[[[[1]]]]
[[], [1], [1, 2]]
[[1], [1, [2]], [1, [2, [3]]]]
[[8], [8, [9]], [8, [9, [10]]]]

偽のテストケース

[]
[0]
[0, -1]
[-1, 0]
[[[[0]]]]
[[0], [1, 2], [3, 4, 5]]
[[8], [8, [9]], [8, [9, [1, 0]]]]
[-1, 0, 0, 0]

テストケースに基づいて、ツリー構造内の任意の場所を意味する「0を含む」という意味ですか?それは私がそれが意味すると推測したものではありません。
-xnor

はい、どこでも。それを明確にしようとします。
デニス

「文字列表現に特定の形式があるとは想定できない」とはどういう意味ですか?
ダダ

2
これらはギザギザの配列ではありません-ギザギザの配列は、要素の種類ではなくサイズのみが異なるため、同じ深さですべての数値を持ちます。
Ørjanヨハンセン

2
@Qwertiyそうですね、「すべて」が「ほとんど」の言語Objectです...私のお気に入りはHaskellです。また、少なくともCでは、配列とintを安全に混在させることはできません。これらの言語はどちらも完全にギザギザの配列を処理できますが、それでもこの問題には使用できません。
Ørjanヨハンセン

回答:


38

ゼリー、3バイト

ṭFẠ

F 入力リストを平坦化します。

元の入力リストに要素としてタックします。これは空の場合にのみ偽物です。

次に、フラット化されたリストの要素、または元のリスト自体が偽であるかどうかを確認します。


(元の答え)

FẠ^Ṇ

彼に合った解決策を見つけることを奨励してくれたデニスに感謝します。

FẠ 入力に任意の深さで偽の値が含まれる場合は0、それ以外の場合は1を返します。 Ȧ、空のリストを除いて行われます。

入力が偽の値の場合は1、それ以外の場合は0を返します。唯一の偽のリストは空のリストです。

2つのXORを実行すると答えが得られます。


F;WẠ

これはDennisの精神とほぼ同じですが、リストが空のときにリストにゼロを入れるF;LẠ代わりに、空のリストを自分自身に入れて(生成)、偽の要素を含めます。LW[[]]


30
私自身の挑戦と私自身の言語で打ち負かされました...よくやった!
デニス


12

ルビー、25 24 23 18 16バイト

p$_!~/\D0|^..$/

-nコマンドラインにフラグが必要です(+1バイト、-e-> -ne)。

オンラインでお試しください!

これは、STDINでRubyの標準配列形式の入力を受け取り、STDOUTで出力trueまたは出力する完全なプログラムですfalse

 $_              # line of input that was read automatically (-n)
   !~/        /  # does not match the regex...
      \D0        #   a non-digit followed by a 0
         |       #   or...
          ^..$   #   a 2-length string (which must be [], the empty array)
p                # output the result

23バイトの機能バージョン:

->a{"#{a}"!~/\D0|^..$/}

これは、テスト対象の配列という1つの引数を取るプロシージャです。

おかげマーティンエンダーバイト用とにVentero 2バイトのために!


フラグとともにp$_!~/\D0|^..$/(またはp ! ~/\D0|^..$/、重要な空白を)使用することで、さらに2バイトを節約できます-n
ヴェンテロ

8

ゼリー、4つのバイト

FẠ_Ṇ

オンラインでお試しください!

Ȧ利回り0入力が空であるか、またはAが含まれている場合0、それ以外の場合はあります1

FẠ収量0扁平入力が含まれている場合0(入力が配列であることが保証されているため)空の配列の唯一のエッジケースを残し。

モナドではなく非ベクトル化論理0である1ため、空でないリストと空のリストに対して返されます。そのため、これは単にFẠ使用した結果からサブトレースすることができます_ます。


あと1つ、少なくともあと1つです。
デニス

@DennisそうじゃないFẠạṆよね?
エリックアウトゴルファー

@EriktheOutgolferいいえ、そうではありません。私が念頭に置いている答えは、空の配列のエッジケースとは異なり、非配列の場合は異なる結果を生成します。
デニス

@Dennis AをTrueに、BをFalseに、Cを空に、Dを配列以外に返すのが好きですか?それは競合しないでしょう。私がやったのは、負のブール値がないため、差ではなく絶対差を使用することです。
エリックアウトゴルファー

@EriktheOutgolfer Bはチャレンジ仕様に準拠するためにCと等しくなければなりませんが、入力は配列であることが保証されているため、Dは何でもかまいません。
デニス

8

05AB1E9 8バイト

エミグナのおかげで-1バイト

)Q¹˜0å~_

説明:

)Q        Is the length of the input 0?
  ~    _  ... NOR ... (just for you, Dennis) 
   ¹˜     Input deep flattened
     0å   Contains 0

オンラインでお試しください!


失敗するようです[[]]
デニス

05AB1E では0は本当に真実ですか?
デニス

all arrays for which Ȧ returns 1 must map to the same truthy value, and all arrays for which Ȧ returns 0 must map to the same falsy value (emphasis mine)
Dennis

1
@Dennis Alright, chucked in a logical negation byte there.
Okx

1
Aw, just for me. :P
Dennis

7

Mathematica, 17 bytes

#!={}&&#~FreeQ~0&

FreeQ does the check against 0 for us, but of course it would return True for input {}, so we need to check that case separately.


7

APL (Dyalog), 21 12 7 bytes

Golfed 5 bytes thanks to Adám by using forks

⍬∘≡⍱0∊∊

Try it online!

This is my first try at Dyalog. Golfing tips are welcome!

Explanation

⍬∘≡                   Fork; Is the argument a null set
   ⍱                  Nor
    0∊∊               0 belongs to the enlisted form of the argument
                      For example, (1 (2 (3 (0)))) would become
                      1 2 3 0 using the ∊ monad
                      Then we check if zero belongs to this vector

+1 Note that you are combining the results of two tests. This is perfect for a fork. ⍬∘≡ is the left test (empty-set bound-to identical-to), and 0∊∊ is the right test (itself a fork; zero member-of enlisted-form). Put it together: ⍬∘≡⍱0∊∊. Try it online!
Adám

Also, you may want to use the name "APL (Dyalog)" so people can find what it is you are using.
Adám

@Adám Thanks for the tips!
Kritixi Lithos

6

Operation Flashpoint scripting language, 199 188 bytes

A={f={private["_i","_r"];_r=1;_i=0;while{_i<count _this}do{o=_this select _i;if(o in [o])then{if(o==0)then{_r=0}}else{_r=o call f};_i=_i+1};_r};if(count _this==0)then{0}else{_this call f}}

Call with:

[3,6,4,[4,6],[3,6,[],[2,4,[0],3]]] call A

or with:

hint format["%1", [3,6,4,[4,6],[3,6,[],[2,4,[0],3]]] call A]

Explanation:

In the game's scripting language, any string containing code can be called. The curly braces {} denote the beginning and the end of a string. (Quotation marks work too, but that gets messy when they are nested.) So, A={...} assigns a string to variable A, and the variable can then be called like a function with: <argument> call A. Basically any string can be treated as a block of code.

Then, inside the "function" A, we define another function f. private declares the two variables _i and _r local to function f. A local variable's name has to start with an underscore.

while {} do {} is a loop, where the first string (denoted by {}) contains the code for the loop condition and the second one for the loop body.

_this is the argument that was passed with the call function. _this can be of any type, but here we assume it is an array.

In the loop, o=_this select _i accesses the _i:th element of the array and assigns it to variable o. if (o in [o]) is a trick to determine if the o is another array or not. If o is a number (or anything other than an array), o in [o] will evaluate to true, because the in function finds a value matching o from the array [o]. If o is an array, the expression yields false, because the in refuses to compare arrays.

If o is not an array, we check if it equals zero, and if it does, we'll set the variable _r, which we'll use as the return value, to zero. Otherwise, if o is an array, we assign to _r the return value of the recursive call to f with the new array o as the argument.

After the loop, at the end of function f, we evaluate the expression _r, which yields the value of _r, and as this is the last expression to be evaluated, this is what the call to function f returns.

Now that we have defined f (f need not be inside A, but this way we could have declared it a local variable/function (no difference really) of A if we didn't want to save some bytes), let's get back A. if (count _this == 0) checks if A's input array is empty, and if it is, A returns 0. Otherwise the function f is called and its return value will be A's return value.

One might notice that it seems that a semicolon would be missing from a few of places, but this is not the case, because a semicolon is only needed after a statement if another statement follows it inside the same block of code (i.e. string).


Wait, what?! Operation Flashpoint?
Brain Guider

wat how? what??? confuzed
Christopher

@DownChristopher Added an explanation.
Steadybox

1
@AnderBiguri Yep, why not? The game's scripting language fits the definition of programming language given in the meta post linked in the question.
Steadybox

1
@Steadybox I am confused about the existence of the thing, not its validity!!
Brain Guider

5

Perl 5, 15 bytes

Saved 2 bytes by using the same technique as Doorknob's Ruby answer.

14 bytes of code + -p flag

$_=!/\b0|^..$/

Try it online!

/.../ ensures that the array isn't empty (it will match on any array but [].
/\b0/ will only match if there is a 0 in the array. (the \b ensures that the 0 isn't a part of another number but a whole number).


5

Haskell, 48 bytes

f x=or[elem c"[,"|c:'0':_<-scanr(:)[]x]<(x<"[]")

Try it online!

Thanks to Lynn for the test cases and the x<"[]" trick.

The outer inequality requires (x<"[]") to be True (nonempty list) and or[elem c"[,"|c:'0':_<-scanr(:)[]x] to be False (no zeroes).

Characters of 0 are detected as following a , or [, as opposed to a number like 20. The expression scanr(:)[]x generates all suffices of l, and c:'0':_<- captures those whose second character is '0'. Then, elem c"[," checks whether the first character is , or [.

I assume here that Haskell-style lists don't have spaces, but if so ',' can just be replaced by ' '.

Here's a more direct 48-byte method, though it produces 0's and 1's which aren't Truthy/Falsey in Haskell.

f"[]"=0
f(c:'0':_)|elem c"[,"=0
f(_:t)=f t
f _=1

5

Jelly, 4 bytes

F;LẠ

Try it online!

How it works

F;LẠ  Main link. Argument: A (array)

F     Flatten A.
  L   Yield A's length.
 ;    Concatenate.
   Ạ  All; Tell if all elements of the resulting 1D array are truthy.

Note that the Ạ atom behaves like Python's all and thus is rather different from the banned Ȧ.


8
Heads-up: This isn't the only 4-byte solution Jelly, apart from the obvious L;FẠ. Who can find another one?
Dennis

4

JavaScript (ES6), 34 bytes

a=>a.length&&+!~`,${a}`.search`,0`

Test cases


You can probably use !!a[0] instead of a.length. (You don't have to worry about a[0] being zero as the result must be false in this case anyway.)
Neil

Never mind, I saw Qwerty already got there.
Neil

4

Julia, 45 bytes

a(x)=all(a,x);a(x::Int)=x!=0;g(x)=x!=[]&&a(x)

This creates a function g that indicates whether Ȧ would be 1 or 0 by calling a recursive function a. To make a suitable a, we use multiple dispatch:

# A method for scalar values
a(x::Int) = x != 0

# A recursive fallback for arrays
a(x) = all(a, x)

The function all takes a function argument, so we're calling a on each element of the input. Then we simply define the function for the submission as

g(x) = x != [] && a(x)

Basically we just need a but with a check to correctly handle [].

Try it online!


can you define the function a(x) or g(x) as !x instead?
Cyoce

4

Grime, 16 14 11 bytes

Thanks to Zgarb for saving 5 bytes.

e`s\0v#|!..

Try it online!

The e tells Grime to try and match the entire input and print 0 or 1 depending on whether that's possible.

The |! is effectively a "neither" operator, because x|!y is shorthand for (x|y)!. So we make sure that the input neither contains a zero preceded by a symbol nor is a string of only two characters ([]).

A note about the second half: P# matches a rectangle that contains at least one match of P. However, in our case P consists of both s and \0 so that would normally require parentheses: (s\0)# (because the precedence of # is too high). But Grime has a really neat feature where you can modify the precedence of operators with ^ and v. So by using v# we lower #'s precedence so that it's lower than that of any other operator (including concatenation), which lets us save a byte on the parentheses.


3

Pip, 12 bytes

#Va&`\b0`NIa

Takes the array as a command-line argument in Pip's repr form, like [1;[2;3]]. Returns 1 for truthy, 0 for falsey. Try it online or verify all test cases.

Explanation

              a is 1st cmdline arg (implicit)
 Va            Eval a (converting from a string to a list)
#              Take the length (0 if empty, nonzero if nonempty)
   &          Logical AND
    `\b0`      Regex pattern: word boundary followed by 0 (avoids things like 10)
         NIa   Not in a (0 if `\b0` matches in a, 1 if it doesn't)
              Autoprint

Bonus answer, 12 bytes

Here's a function that takes a list instead:

#_>0=0N_Js^s

#_            Len(arg)
  >0          is greater than 0
    =         which also equals the following (comparison operators chain like Python's):
     0N       Count of 0's in
       _Js^s  arg, joined on space and then split on space (a hacky way to flatten)

TIO


3

Röda, 59 44 bytes

f a{[1]if{g(a)[[]!=a]}}g a{[a!=0];try a|g _}

Try it online!

f takes the input from its stream as a list that can contain other lists and integers. It returns 1 if a is truthy and nothing otherwise. The helper function g checks if a contains zeros.

Explanation:

f a{[1]if{g(a)[[]!=a]}}
f a{                  } /* Function declaration */
          g(a)          /* Call g -> pushes some booleans to the stream */
              [[]!=a]   /* Push value []!=a to the stream */
       if{           }  /* If all booleans in the stream are true: */
    [1]                 /*   Push 1 to the stream */
                        /* Otherwise return nothing */

g a{[a!=0];try a|g _}   /* Helper function */
g a{                }   /* Function declaration */
    [a!=0];             /* Push value a!=0 to the output stream */
           try          /* Ignore errors in the following if a is not a list */
               a        /* Push values in a to the stream */
                |g _    /* Pull values from the stream and */
                        /*   call g for each of them */
                        /*   (pushes boolean values to the output stream) */

A solution that makes use of regexes could very likely be shorter.

This answer could have been shorter if it were allowed to return multiple values. This has been discussed in one of my answers before, and it was concluded that it is allowed in the default rules to return different truthy and falsy values for different inputs, but for some reason OP forbid it here and there. :(


3

Wonder, 15 bytes

@&#0! iO0flat#0

Usage:

(@&#0! iO0flat#0)[1;2;3;[8;9;0]]

Flatten input, get all occurrences of 0, logical NOT, logical AND with input.


3

Haskell, 62 bytes

import Data.List
(%)=isInfixOf
f x=not(",0"%x||"[0"%x)&&x<"[]"

Try it online!

This is a function String -> Bool. Haskell’s lists are heterogenous, so there’s no built-in way to represent lists like [0, [0]].


Based on the re-worded rules, the inputs should not have spaces because Haskell arrays don't by default. At least, I think that's the interpretation even though Haskell doesn't allow jagged arrays. But it looks like your code would work the same with , for ` `.
xnor

2
As I'm quibbling up in the question comments, Haskell does have jagged arrays (and lists) - it's just that it's not really enough for what this question requires.
Ørjan Johansen

3

Python 2, 45 39 38 bytes

lambda a:(a>[])^(' 0'in`a`or'[0'in`a`)

Try it online!

-6 thanks to @BenFrankel


previous version, without converting list to string repr, 68 bytes:

lambda a:(len(a)and g(a))*1
g=lambda b:all(map(g,b))if b>[]else b!=0

This gives a false positive on []. The following saves 6 bytes and succeeds on []: lambda a:bool(a)^(' 0'in`a`or'[0'in`a`)
Ben Frankel

2

MATLAB, 49 bytes

As MATLAB (as well as Octave) does not allow these kind of nested arrays, we interpret it as a string.

First we replace all non-digit characters with a space. Then we use str2num to convert it to an (1D) array, on which we can apply all (which is allowed, as it does not completely solve this task by itself.)

s=input('');s(s<45|s>57)=32;disp(all(str2num(s)))

2

egrep, 7+3=10 bytes

\<0|^.]

+3 bytes for the required -v flag to invert the result.

Grep doesn't have any concept of arrays, so this uses a string representation as given in the question. Takes input on one line from stdin, returns via the exit code (ignore stdout).

(Now using a version which doesn't account for 01 and similar, since word-of-god is that it's OK)

Original bash/grep entry:

grep -Ev '\<0+\>|^.]'

Finds 0s anywhere (using the word boundary checks \< and \> to discount things like 10 or a1), or a whole string matching [], then inverts the match.

Breakdown:

grep
     -E \    # extended regular expression syntax
     -v \    # invert match
     \<0+\>  # a number of 0s with alphanumeric boundaries on both sides
     |^.\]   # or ']' as the second character (implies '[]')

Not cheating, just good golfing. :) Btw, grep is capable for primality testing, so it is a programming language as far as PPCG is concerned. \<0\|^.] plus -v would count as an 11 byte solution.
Dennis

1
@Dennis cool, thanks! (I switched to egrep rather than grep to save an additional byte; language name doesn't count towards byte-count!)
Dave

2

Javascript ES6, 24 chars

Works with array, returns 1 or 0:

a=>!!a[0]&!/\b0/.test(a)

Test:

f=a=>!!a[0]&!/\b0/.test(a)

console.log([
  [1],
  [10],
  [[]],
  [[[[1]]]],
  [[], [1], [1, 2]],
  [[1], [1, [2]], [1, [2, [3]]]],
  [[8], [8, [9]], [8, [9, [10]]]],
].every(x => f(x)===1))

console.log([
  [],
  [0],
  [0, -1],
  [-1, 0],
  [[[[0]]]],
  [[0], [1, 2], [3, 4, 5]],
  [[8], [8, [9]], [8, [9, [1, 0]]]],
].every(x => f(x)===0))


Since the return value can be truthy/falsy, you can drop the !! (though then you must change & to &&). Saves one byte.
Brian McCutchon

@BrianMcCutchon, no as there is a binary &. In case of && without !! consistent output will be broken: undefined for [],0 for [0] and [0,1,2] and false for others.
Qwertiy

I don't see how breaking consistent output is bad in this challenge. My point with switching to && is that you would need to if you take my first suggestion, since 2 & 1 == 0.
Brian McCutchon

@BrianMcCutchon, the first point of the question: "The truthy and falsy values must be consistent for all inputs, i.e, all arrays for which Ȧ returns 1 must map to the same truthy value, and all arrays for which Ȧ returns 0 must map to the same falsy value."
Qwertiy

Ah, I skimmed that too quickly. Never mind.
Brian McCutchon

2

√ å ı ¥ ® Ï Ø ¿ , 12 4 bytes

i0Bu

Explanation

i            › Take input as a list and automatically flatten it. If empty, push 0.
 0           › Push 0 to the stack
  B          › Pop 0 and push the number of times it appears
   u         › convert top value to its boolean 

If result needs to be outputted ...

i0Buo        › same as above; o outputs the top value on the stack

Previous solution

I had posted this before realising that stack based languages could leave the value on the stack as a form of output

i0B¿0oP?!¿o?

Explanation

i            › Take input as a list and automatically flatten it. If empty, push 0.
 0           › Push 0 to the stack
  B          › Pop 0 and push the number of times it appears
   ¿         › If the top value is true ...
    0        › Push 0
     o       › Output the top value on the stack
      P      › Pop the top value from the stack
       ?     › End if statement
        !    › Boolean opposite of top value
         ¿   › If the top value is true ...
          o  › Output the top value
           ? › End if statement

2

Haskell, 45

As Lynn and xnor remarked, Haskell does not come with a heterogeneously-nested list type. But it's easy to add them as a custom data type and let the function operate on that type, and this is much preferrable to operating on (urgh!) strings.

data L=L Int|T[L]
f(L n)=n/=0
f(T l)=all f l

To actually be able to write out such lists as literals with [1, [2]] syntax, you also need some typeclass fu. Full test case:

{-# LANGUAGE OverloadedLists, TypeFamilies #-}
import GHC.Exts (IsList(..))

instance Num L where
  fromInteger = L . fromInteger
  negate (L n) = L $ negate n
instance IsList L where
  type Item L = L
  fromList = T
main = mapM_ (print . f) (
                    [ [1]
                    , [[[[0]]]]
                    , [[8], [8, [9]], [8, [9, [1, 0]]]]
                    ] :: [L])

Try it online!


2

Vim, 23 bytes

:g/0\|^..$/d
:s/.\+/1/<CR>

Try it online!

Outputs an empty string for false, or 1 for true. This could be shorter if I can output an empty string or [] for false (both of which are falsy values in vim).



1

Lithp, 74 bytes

(def f #L::((foldl(flatten L)(?(>(length L)0)1 0)#N,A::((?(== N 0)0 A)))))

Try it online!

Well, this turned out longer than I'd hoped. The [] case tripped me up and added a few bytes. It simply flattens the list and does a fold left over it, and if it finds a 0 it sets the accumulator to 0.


1

Ruby, 24 22 bytes

->a{a[0]&&a*?!!~/\b0/}

Try it online!

Yes I know there's a better solution in Ruby but I wanted to find one taking the array in input instead of a string.


1

tinylisp, 70 64 bytes

(load library
(d _(q((X)(i(c()X)(all(map _ X))X
(q((L)(i L(_ L)0

The last line is an unnamed lambda function that takes a list and returns 1 for "truthy-under-Ȧ" and 0 for falsey. Try it online!

Ungolfed

(load library)

(def _Ȧ
 (lambda (val)
  (if (equal? (type val) List)
   (all (map _Ȧ val))
   val)))

(def Ȧ
 (lambda (ls)
  (if ls
   (_Ȧ ls)
   0)))

The recursive helper function does most of the work. If its argument is a list, we map to its elements and return 1 if they are all truthy, 0 if any are falsey. (Conveniently, all returns 1 when given the empty list.) Otherwise, the argument must be an integer; we return it as-is (0 is falsey and all other integers are truthy in tinylisp).

The main function Ȧ checks if the list is nonempty. If so, it calls ; if not, it returns 0.

The golfed version takes advantage of some undefined behavior: rather than using (e(type X)List) to test whether X is an integer or a list, it does (c()X), which attempts to cons (prepend) the empty list onto X. If X is a list, this results in a nonempty list, which is truthy. If X is an integer, tinylisp outputs an error message and returns an empty list, which is falsey. Since stderr is ignored, this approach is valid.


0

PHP, 63 54 bytes

9 bytes saved by @user63956

function a($a){return$a&&!strpos(print_r($a,1)," 0");}

takes an array as input; returns true or false: If $a is not empty,
check if print_r output contains a 0 value.

array solution, 83 bytes

function b($a,$r=0){$r|=$a;foreach($a as$v)$r|=is_array($v)?b($v,1):!!$v;return$r;}

recursive function returns 1 or 0.

breakdown

function b($a,$r=0)
{
    $r|=$a;         # if $a is not empty, set $r (no effect in recursion)
    foreach($a as$v)    # loop through elements:    
        $r&=is_array($v)    # 2. if test failed, clear $r
            ?b($v,1)        # 1. if array, recurse
            :!!$v;          #    else test element <>0
    return$r;           # return $r
}

1
You can save a few bytes with strpos(print_r($a,1)," 0") instead of preg_match(...).
user63956

@user63956 ... and it also solves the 0-index problem. I wasn´t aware of the second print_r parameter. Great!
Titus
弊社のサイトを使用することにより、あなたは弊社のクッキーポリシーおよびプライバシーポリシーを読み、理解したものとみなされます。
Licensed under cc by-sa 3.0 with attribution required.