0

I have an array which looks like the following

array:2 [▼
  0 => array:1 [▼
    "input1" => "Something"
  ]
  1 => array:1 [▼
    "input2" => ""
  ]
]

Now the first element will always have some data. It is the second element I am interested in. At the moment, I am trying this

if(!empty($clientGroup[0][1]) || !empty($clientGroup[1][1]))
    var_dump("Some Data");
} else {
    var_dump("Both Empty");
}

The else should only be triggered if both elements are empty e.g.

array:2 [▼
  0 => array:1 [▼
    "input1" => ""
  ]
  1 => array:1 [▼
    "input2" => ""
  ]
]

If one of them have any data, the if should be triggered (so for the first array I showed, the if should be triggered).

How would I go about doing this, empty does not seem to work.

Thanks

2
  • What is going wrong? Does your first array generate "Some Data" or "Both Empty"? Commented Feb 5, 2016 at 17:18
  • Based on the answer you accepted, I assume you were always getting "Both Empty". Commented Feb 5, 2016 at 17:33

2 Answers 2

2

The 2nd level keys do not exist so you will always be told the values are empty. Change the line

if(!empty($clientGroup[0][1]) || !empty($clientGroup[1][1]))

to,

if(!empty($clientGroup[0]['input1']) || !empty($clientGroup[1]['input2']))

and you should get the results you're after.

Sign up to request clarification or add additional context in comments.

Comments

1

It's not realy 2D array because you have associative array inside an other array.

you must use key name (input1, input2) to access the value.

I recommend to use

if($retourdata[0]["input1"] !== "" || $retourdata[1]["input2"] !== "")

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.