58

I have the following array in PHP:

[
  {
     "website": "example",
     "url": "example.com"
  },
  {
     "website": "example",
     "url": "example.com"
  }
]

Now I would like to convert this to a collection so I sort by the keys website or url. However when I do this:

$myArray = collect(websites);

I get this instead:

 {
      "0": {
         "website": "example",
         "url": "example.com"
      },
      "1": {
         "website": "example",
         "url": "example.com"
      }
    }

And the sorting does not work, I would like to know what I am doing wrong and how I can fix it so I have an array collection of objects I can easily sort.

Edit: I expect the output to be the same as this:

[
  {
     "website": "example",
     "url": "example.com"
  },
  {
     "website": "example",
     "url": "example.com"
  }
]

By "sorting does not work" I meant the items are not sorted.

10
  • What do you expect the output to be Commented Mar 3, 2018 at 11:19
  • And what do you mean by "the sorting does not work"? Commented Mar 3, 2018 at 11:22
  • What I posted on top, didn't expect it to change from the original Commented Mar 3, 2018 at 11:22
  • Your question makes no sense in its current form Commented Mar 3, 2018 at 11:22
  • It didn't change, though. Your normal array has indices too. Commented Mar 3, 2018 at 11:22

2 Answers 2

106

Edit; I understand this question is getting a lot of hits based on the title so the TLDR for those people is to use the collect() helper to create a Collection instance. In answer to the questioner's brief:

If you have

$collection = collect([
    (object) [
        'website' => 'twitter',
        'url' => 'twitter.com'
    ],
    (object) [
        'website' => 'google',
        'url' => 'google.com'
    ]
]);

You then have your array wrapped in an instance of the Collection class. That means it does not behave like a typical array (- it will be array-like, but don't treat it like it is one -) until you call all() or toArray() on it. To remove any added indices you need to use values().

$sorted = $collection->sortBy('website');

$sorted->values()->all();

The expected output:

[
     {#769
       +"website": "google",
       +"url": "google.com",
     },
     {#762
       +"website": "twitter",
       +"url": "twitter.com",
     },
]

See the docs https://laravel.com/docs/5.1/collections#available-methods

The toArray method converts the collection into a plain PHP array. If the collection's values are Eloquent models, the models will also be converted to arrays.

The all method returns the underlying array represented by the collection.

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

3 Comments

Exactly what I wanted! Thank you, I understand how it works now and it worked when I tested it. :)
Short answer: Use collect($arr).
Short answer extended: There are different collections. 1. collect(array $array) or 2. new \Illuminate\Database\Eloquent\Collection(array $array). First returned collection from type Illuminate\Database\Eloquent\Collection. In some cases, however, you need the Eloquent Database Collection, then you take the second one.
22

In my case I was making an collection to fake a service for test purpose so I use

$collection = new Collection();
    foreach($items as $item){
                $collection->push((object)['prod_id' => '99',
                                           'desc'=>'xyz',
                                           'price'=>'99',
                                           'discount'=>'7.35',

                ]);

            }

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.