0

Is it possible to use conditions within an array:

Uses define('COMPANY_ADDRESS_1','something here'); << if empty don't want to be in the array
$invoice->setFrom(array(COMPANY_NAME,COMPANY_ADDRESS_1,COMPANY_ADDRESS_2,COMPANY_TOWN,COMPANY_COUNTY,COMPANY_POSTCODE));

For example COMPANY_ADDRESS_2 is not set and doesn't show in the array so at the moment the output ends up like:

  • company name
  • company address 1
  • <<<<<<<<<< leaves a gap here
  • company town
  • company county
  • company postcode

The output is fine but if nothing is set for example COMPANY_ADDRESS_2 I want to remove it from the array altogether as it's passed to a PDF generator and currently writing as a blank line.

5
  • Sorry fixed question, the output is fine but if nothing is set for example COMPANY_ADDRESS_2 i want to remove from the array all together as its passed to a PDF generator and writing as a blank line. Commented May 1, 2015 at 11:44
  • I done and same issue, shows a space in the address line: $invoiceFromArray = $invoice->setFrom(array(COMPANY_NAME,COMPANY_ADDRESS_1,COMPANY_ADDRESS_2,COMPANY_TOWN,COMPANY_COUNTY,COMPANY_POSTCODE)); $invoiceFromArray = array_filter($invoiceFromArray); Commented May 1, 2015 at 11:53
  • And are you sure if the result depends only of if the element present in array or not? Why you form manually array without this one element and see what will appear? Commented May 1, 2015 at 11:56
  • I removed one from the array and moved up Commented May 1, 2015 at 11:58
  • i get this back so its removing from the array: Array ( [0] => Name [1] => Old knows Factory [2] => Unit 5C, Office 14 [4] => Nottingham [5] => gh2 2gh ) Commented May 1, 2015 at 12:05

1 Answer 1

1

According to your question i think your array look like below:-

Array
(
    [0] => company name
    [1] => company address 1
    [2] => 
    [3] => company town
    [4] => company county
    [5] => company postcode
)

So you need to do in following manner:-

<?php

$result = array('company name','company address 1','','company town','company county','company postcode');//original array
$newArray = array();
foreach($result as $value){
    if($value != ''){
        $newArray[] = $value;
    }
}
echo "<pre/>";print_r($newArray);
echo "<pre/>";print_r(array_filter($result));
?> 

Output:-

Array //null value or empty value removed and array is re-indexed
(
    [0] => company name
    [1] => company address 1
    [2] => company town
    [3] => company county
    [4] => company postcode
)

Array //null value or empty value removed without re-indexed
(
    [0] => company name
    [1] => company address 1
    [3] => company town
    [4] => company county
    [5] => company postcode
)
Sign up to request clarification or add additional context in comments.

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.