0

I have the following array in php:

array(12) {
  [0]=>
  array(2) {
    ["adress"]=>
    string(17) "Kungsvägen 118 A "
    ["dob"]=>
    string(10) "1969-06-17"
  }
  [1]=>
  array(2) {
    ["adress"]=>
    string(14) "Skolgatan 1 B "
    ["dob"]=>
    string(10) "1969-06-17"
  }
  [2]=>
  array(2) {
    ["adress"]=>
    string(14) "Skolgatan 1 B "
    ["dob"]=>
    string(10) "1980-05-22"
  }
  [3]=>
  array(2) {
    ["adress"]=>
    string(12) "Myntvägen 8 "
    ["dob"]=>
    string(10) "1980-05-22"
  }
  [4]=>
  array(2) {
    ["adress"]=>
    string(14) "Skolgatan 1 B "
    ["dob"]=>
    string(10) "1993-05-09"
  }
  [5]=>
  array(2) {
    ["adress"]=>
    string(14) "Skolgatan 1 B "
    ["dob"]=>
    string(10) "1989-06-28"
  }
  [6]=>
  array(2) {
    ["adress"]=>
    string(14) "Skolgatan 1 B "
    ["dob"]=>
    string(10) "1991-03-17"
  }
  [7]=>
  array(2) {
    ["adress"]=>
    string(14) "Skolgatan 1 B "
    ["dob"]=>
    string(10) "1989-10-30"
  }
  [8]=>
  array(2) {
    ["adress"]=>
    string(23) "Gasslanda VÄSTERGÅRD 2 "
    ["dob"]=>
    string(10) "1980-10-30"
  }
  [9]=>
  array(2) {
    ["adress"]=>
    string(14) "Skolgatan 1 B "
    ["dob"]=>
    string(10) "1980-10-30"
  }
  [10]=>
  array(2) {
    ["adress"]=>
    string(14) "Skolgatan 1 B "
    ["dob"]=>
    string(10) "1990-05-01"
  }
  [11]=>
  array(2) {
    ["adress"]=>
    string(11) "Ågatan 6 A "
    ["dob"]=>
    string(10) "1990-05-01"
  }
}

I want to print out the unique adress-values in this array when I'm doing an foreach. As you can see, Skolgatan 1 B appears several times in the array, so I want to print it out just once in my loop. How can I do this? I have tried

array_unique([$newArray['adress']);

but that does not work.

4

4 Answers 4

1
$unique = array();
foreach($array as $item)
    $unique[$item['adress']] = $item;

Then use $unique

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

Comments

0

Something like this should work:

$printed_vals = array();
foreach ($array as $item) {
    if (!in_array($item['adress'], $printed_vals)) {
        print_r($item);
        $printed_vals[] = $item['adress'];
    }
}

Comments

0

This is how I would have solved this problem. Hope it helps.

$non_duplicates = array();
$i=0;
foreach( $addresses AS $address ){
    if( !in_array( $address['adress'], $non_duplicates ) ){
        // Use one of the below array_push statements
        array_push( $non_duplicates, $i ); // if you want to get the array key position
        array_push( $non_duplicates, $address['adress'] ); // if you only want a new array of unique addresses
    }
    $i++;
}

If you have chosen to only return key positions to $non_duplicates yo can then retrieve your unique positions from you array by:

foreach( $non_duplicates AS $key ){
    echo $addresses[$key];
}

Comments

0

I guess you want to print address once but dob in multiple times.. in that case you can do something like followings

$printed_vals = array();

foreach ($array as $item) {
    $printed_vals[$item['adress']][] = $item['dob'];
}

foreach ($printed_vals as $address => $values) {
    echo $address . " => " . implode(",", $values);
}

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.