1

UPDATE

I found an answer to this question for myself and I posted it, but i can only accept it in two days..


I create this multidimensional array:

array (
  0 => 
  array (
    'OrderDate' => '02.11.2018',
    'ClientNumber' => 5500
  )
)

array (
  0 => 
  array (
    'OrderDate' => '02.11.2018',
    'ClientNumber' => 5500
  )
)

array (
  0 => 
  array (
    'OrderDate' => '05.03.2018',
    'ClientNumber' => 5500
  )
)

array (
  0 => 
  array (
    'OrderDate' => '10.12.2018',
    'ClientNumber' => 2200
  )
)

array (
  0 => 
  array (
    'OrderDate' => '10.12.2018',
    'ClientNumber' => 2200
  )
)

The array shouldn't contain duplicate OrderDate's from each ClientNumber.

I want the output to look like this:

array (
  0 => 
  array (
    'OrderDate' => '02.11.2018',
    'ClientNumber' => 5500
  )
)

array (
  0 => 
  array (
    'OrderDate' => '05.03.2018',
    'ClientNumber' => 5500
  )
)

array (
  0 => 
  array (
    'OrderDate' => '10.12.2018',
    'ClientNumber' => 2200
  )
)

What if-condition should i use to make my output look like this? Any help will be appreciated, thank you!

This is my function that is called in a foreach. I tried my best to check with in_array and array_column but the output didn't look like the code above.

$array = array();
function createArray($clientNr, $orderDate){
 if(empty($array)){ // if array is empty, just add
  $array[] = array(
              "OrderDate"=>$orderDate,
              "ClientNumber"=>$clientNr
                 );
 } else {
    if(??????)<--------------------------- // check here if date from clientnumber exists
      $array[] = array(
                   "OrderDate"=>$orderDate,
                   "ClientNumber"=>$clientNr
                  );
    }
 }
}
Syno
  • 1,056
  • 10
  • 25

3 Answers3

0

You can try this :-

$data=array();

$data[0]['OrderDate']='02.11.2018';
$data[0]['ClientNumber']='5500';

$data[1]['OrderDate']='02.11.2018';
$data[1]['ClientNumber']='5500';

$new_arr = array_unique($data, SORT_REGULAR);
Yash Parekh
  • 1,513
  • 2
  • 20
  • 31
ashish kumar
  • 172
  • 7
0

Give a try with below, you can use array_map or array_unique() function

$array = array(
  array(
     'OrderDate' => '02.11.2018',
     'ClientNumber' => 5500
  ),
  array(
    'OrderDate' => '02.11.2018',
    'ClientNumber' => 5500
  ),
  array(
    'OrderDate' => '05.03.2018',
    'ClientNumber' => 5500
  ),
  array(
    'OrderDate' => '10.12.2018',
    'ClientNumber' => 2200
  ),
  array(
    'OrderDate' => '10.12.2018',
    'ClientNumber' => 2200
  )
);

$newArr = array_map("unserialize", array_unique(array_map("serialize", $array)));

print_r($newArr);

$newArr1 = array_unique($array, SORT_REGULAR);

print_r($newArr1);
shubhangee
  • 529
  • 3
  • 10
0

I did it! Thanks for your approaches, but these two didn't work for me:

array_unique($array, SORT_REGULAR);
array_map("unserialize", array_unique(array_map("serialize", $array)));

This is my solution, I added a unique key and checked, if the key already exists.

$id = date("Ymd", strtotime($orderDate)).$clientNr; // creates unique key

if(!array_key_exists($id, $array)){ // check if this key exists and add if not

 $array[$id] = array(
              "OrderDate"=>$orderDate,
              "ClientNumber"=>$clientNr
             );
}
Syno
  • 1,056
  • 10
  • 25