2

I have an array of database objects and I'm using foreach() to present the names and projects. Good, now the customer doesn't want duplicate names for when one person has multiple projects. This has to do with variable scope and this is my failed attempt to pull this stunt off. Here's a partial var_dump of the array of objects.

array [
0 => {
  ["lastName"]=>
  string(1) "w"
  ["projectName"]=>
  string(29) "Bone density scanner analysis"
}
1 => {
  ["lastName"]=>
  string(1) "w"
  ["projectName"]=>
  string(29) "analysis of foot"
}
]

What I want to end up with is:

array [
0 => {
  ["lastName"]=>
  string(1) "w"
  ["projectName"]=>
  string(29) "Bone density scanner analysis"
}
1 => {
  ["lastName"]=>
  string(1) ""
  ["projectName"]=>
  string(16) "analysis of foot"
}
]

Here's what I was thinking that doesn't seem to work:

function suppress_name($name){
  global $string; 
  return ($name == $string) ? '' : $string;
}

function overall() {
  //$result = database call to get objects
  foreach ($result as $item) {
  $string = $item->lastName;
  $rows = array('Name' => suppress_name($item->lastName), 'project' => $item->projectName);
}
}

Researching I see several references to array_unique() which I use for a flattened array, but I can't see that it would help me here. OK, I thought I could make a function, like the above, to handle duplicates and use the $global but think I'm not grasping how to use globals in this instance. I'm happy to be pointed to a better way, or better search terms. Does this make sense?

5
  • 1
    select distinct lastName from projects2names ... that's one reason why databases are superior to flat files and arrays Commented Oct 1, 2015 at 20:26
  • Does the customer want to see only the first project of a person or a list of projects for each person? Commented Oct 1, 2015 at 20:31
  • Yes, the customer wants to see all projects. Doesn't want to see the name of the project owner more than once. In my SQL call I sort by lastName so they're seeing multiples of the same lastName. Commented Oct 1, 2015 at 20:35
  • What about something like: select lastName, group_concat(project) as projects from proj2name group by lastName? You could loop over the names, then maybe explode the projects and loop over the resulting array for display cosmetics. Commented Oct 1, 2015 at 20:41
  • Yes, let me look up group_concat. Commented Oct 1, 2015 at 21:12

2 Answers 2

1

Here is a possible approach to your solution, where we store the last names in a one dimensional array then check against it through each iteration of the array. If the lastName is in the array then set the value to ''.

Note the use of the reference (&).

<?php
$arrays = array(
    array('lastName' => 'w', 'projectName' => 'Bone density scanner analysis'),
    array('lastName' => 'w', 'projectName' => 'analysis of foot')
);
$last_names = array();

foreach($arrays as &$array){
    if( in_array($array['lastName'],$last_names) ){
        $array['lastName'] = '';
    }else{
        $last_names[] = $array['lastName'];
    }
}

echo '<pre>',print_r($arrays),'</pre>';
Sign up to request clarification or add additional context in comments.

1 Comment

Yes, while a better SQL query would be more efficient, this works well in my more complex function.
0

It would be easier to work with nested arrays

array [
0 => {
     ["lastName"]=> string(1) "w"
     ["projects"]=> array [
             0 => {
             ["projectName"] => string(29) "Bone density scanner analysis"
             }
             1 => {
             ["projectName"]=> string(16) "analysis of foot"
     } 
1 => {
     ["lastName"] => string(1) "x"
     ["projects"] => array [ 
             0 => {
             ["projectName"] => string(16) "analysis of head"
             }      ]
      }
] 

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.