1

I'm outputting a json formatted array. Where I have the address fields and Market fields separated by commas I want to remove commas if a field doesn't exist so I don't end up with empty spaces with commas. I had a go below with implode but that doesn't seem to work... what is the correct way of doing this?

  $data = array(
    'ID' => $Member->memberID(),
    'Name' => $Member->first_name() . ' ' . $Member->last_name(),
    'Job Title' => $Member->expert_job_title(),
    'Organisation' => $Member->expert_org_name(),
    'Organisation Type' => $Member->expert_org_type(),
    'Website' => 'http://' . $Member->expert_org_website(),
    'Phone' => $Member->expert_org_phone(),
    'Expertise' => $Member->expert_org_desc(),
    'Markets' => implode(', ', array($Member->expert_org_market_medical(), $Member->expert_org_market_pharmaceuticals(), $Member->expert_org_market_agriculture(), $Member->expert_org_market_food(), )),
    'Address' => $Member->expert_address_one() . ', ' . $Member->expert_address_two() . ', ' . $Member->expert_address_town_city() . ', ' . $Member->expert_address_county_state() . ', ' . $Member->expert_org_country() . ', ' . $Member->expert_address_code(),
    'Latitude' => $Member->expert_org_latitude(),
    'Longitude' => $Member->expert_org_longitude()
  );



header('Content-Type: application/json');

echo json_encode($data, JSON_PRETTY_PRINT);

UPDATE WITH ANSWER

'Markets' => implode(', ', array_filter(array($Member->expert_org_market_medical(), $Member->expert_org_market_pharmaceuticals(), $Member->expert_org_market_agriculture(), $Member->expert_org_market_food(), ))),
'Address' => implode(', ', array_filter(array($Member->expert_address_one(), $Member->expert_address_two(), $Member->expert_address_town_city(), $Member->expert_address_county_state(), $Member->expert_org_country(), $Member->expert_address_code(), ))),
2
  • the quick and dirty way to do it is to trim the string of anything that looks like ', ,' replacing it with ',' Commented Jun 25, 2018 at 16:59
  • i think you can implode with just space implode(' ',..) Commented Jun 25, 2018 at 17:00

3 Answers 3

3

Use array_filter() like this for Address and Markets field

<?php
echo "without array filter = " . implode(', ', array('a', '', 'b', '', ));
echo "<br/>";
echo "with array filter = ". implode(', ', array_filter(array('a', '', 'b', '', )));
?>

DEMO : https://eval.in/1027921

I also urge you to see some example on php.net because it'll help you clearly understand how array_filter() works and what characters are filtered out.

<?php

$entry = array(
             0 => 'foo',
             1 => false,
             2 => -1,
             3 => null,
             4 => ''
          );

print_r(array_filter($entry));
?>

The above example will output:

Array
(
    [0] => foo
    [2] => -1
)
Sign up to request clarification or add additional context in comments.

Comments

0

Have some things that you do. But, you can try array_filter, in documentation.

$Markets = array($Member->expert_org_market_medical(), $Member->expert_org_market_pharmaceuticals(), $Member->expert_org_market_agriculture(),  $Member->expert_org_market_food());
$Markets = array_filter($Markets, function($item) {
    // return if that strings isn't null
    // maybe you want to add another restrictions here
    return strlen($item);
}, $Markets);
// all empty items was removed.|
$Markets = implode(',', $Markets);

Comments

0

Maybe with preg_replace

 $json = json_encode($data);
 $json = preg_replace('/(^, )|( ,)/mi', '', $json);
 $data = json_decode($json);

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.