1

I am getting error Illegal offset type on next segment of code (the condition within foreach method is causing an error):

 $filter = [];
 foreach ($properties[array_keys($properties)] as $prop)
     array_add($filter, array_keys($prop), $prop->id);

And this is how $properties array look like. What I am trying to do is adding elements of array inside $properties into another array (etc. $filter['1'] = $properties['1']). The problem is that I never know how many elements (arrays) are going to be inside $properties and also I don't know what values are they going to be, so I can access them.

If there is an easier way to achieve this, feel free to write it down.

P.S. id is element of array in $properties.

1
  • Are you trying to copy the whole $properties variable into $filter? Commented Sep 10, 2018 at 12:27

1 Answer 1

2

This does not seem like valid code to me. array_keys returns a new array which you are trying to use as a key to get a value out of $properties inside your foreach

Try this:

$filter = [];
foreach ($properties as $key => $prop)
    $filter[$key] = $prop['id'];

EDIT

If you only want the ids, i would recommend to use array_pluck.
It's one of laravels amazing helper functions

It would then look like this:

$filter = array_pluck($properties, 'id');
Sign up to request clarification or add additional context in comments.

2 Comments

Actually, you are right about array_keys method. I am newbie at this, so it was my mistake. About code you have written in this answer, I am getting next error Trying to get property of non-object. This is probably because elements of $properties are not objects, but arrays? Also, I do not need whole $properties into $filter, I just need $properties[$key]->id (I know this is probably not the right syntax, but I guess you can understand what I am talking about).
Yepp, your guess should be right. there are no objects inside your properties array. See edit. PS. remember to accept the answer, if it solves your problem.

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.