0

I have array of object as

$a = [{"id":"20","invoice_id":"123"},{"id":"21","invoice_id":"123"},{"id":"22","invoice_id":"125"},{"id":"23","invoice_id":"125"},{"id":"24","invoice_id":"123"}];

here i want to create new array of abject in which duplicate object will not be there (invoice_id) as new array will be having first object of same invoice_id. i was doing like this.

foreach ($a as $key => $value) {
        if(isset($new)) {
            foreach ($new as $k => $val) {
                if($val->id != $value->id) {
                    $new[] = $value;
                }
            }
        }else{
            $new[] = $value;
        }
    }

my new array will be like

$new = [{"id":"20","invoice_id":"123"},{"id":"22","invoice_id":"125"}]

but it is not giving desired output . What should be done ?

2
  • What is $new? Commented Jun 22, 2017 at 11:41
  • $new will be that new array Commented Jun 22, 2017 at 11:44

4 Answers 4

3

Since you tagged this as Laravel question use collections.

Less code (one line!) and performance hit is non-existent.

$a = json_decode('[{"id":"20","invoice_id":"123"},{"id":"21","invoice_id":"123"},{"id":"22","invoice_id":"125"},{"id":"23","invoice_id":"125"},{"id":"24","invoice_id":"123"}]');

$result = collect($a)->groupBy('invoice_id');

After OP edited question:

$result = collect($a)->unique('invoice_id')->values()->toArray();

results in:

=> [
     {#826
       +"id": "20",
       +"invoice_id": "123",
     },
     {#824
       +"id": "22",
       +"invoice_id": "125",
     },
   ]

or using ->toJson() instead of ->toArray()

"[{"id":"20","invoice_id":"123"},{"id":"22","invoice_id":"125"}]"
Sign up to request clarification or add additional context in comments.

6 Comments

I do not understand your comment.
After your edit: use ->unique('invoice_id') instead of groupBy, and you can convert it to array via ->toArray()
well, my data has changed now but it is showing only one row..which is first one from $a.
@VishalBorade You need to describe exactly what you want, you provided me $a variable and output $new variable and they match with what I've given you.
your answer in perfect as per my question. Thanks but still it is not working for me.. something is wrong.
|
1

Please try the below code with simple logic,

$temp = $new = array();
$b = json_decode($a, true);
foreach ($b as $key => $val) {
  if(!in_array($val['invoice_id'], $temp)) {
    $temp[$val['id']] = $val['invoice_id'];
    $new[] = array('id' => $val['id'], 'invoice_id' => $val['invoice_id']);
  }
}

print_r($new);

I am just creating a temp array to store only the unique invoice_id to compare in a loop.

It gives the below result,

Array
(
  [0] => Array
    (
        [id] => 20
        [invoice_id] => 123
    )

  [1] => Array
    (
        [id] => 22
        [invoice_id] => 125
    )

)

Comments

0
$result = [];
foreach ($a as $data) {
    $result[$data->id] = $data;
}

var_dump(array_values($results));

1 Comment

it is taking last ocuurence of row or object
0

Try this

$a = json_decode($a); 

$invoiceid = [];
$unique = [];
foreach ($a as $key => $value) { 
    if(!in_array($value->invoice_id,$invoiceid)) {
        $invoiceid[] = $value->invoice_id;
        $unique[] = $value;
    }

}
    echo $a = json_encode($unique);  

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.