php - Illegal string offset in array_map


Try use function array_map

 array_map(function ($items) {
                    return $items[$this->relatedKey];
                }, $this->parseIds($ids))

$ids is array of item => value:

 $ids = array:1 [
      "parent_id" => "15"

Key what need to find:

$this->relatedKey = "parent_id"

And get error:

Illegal string offset 'parent_id'

What I do wrong?




Look at this example taken from the documentation (

function cube($n)
    return($n * $n * $n);

$a = array(1, 2, 3, 4, 5);
$b = array_map("cube", $a);

It will return an array with 1, 8, 27, etc ...

The name$items you used for the parameter is misleading, because the argument will be each single$item of your array, andarray_map() is supposed to transform it somehow.

I'm not sure about what you want to do, but apparently you won't needarray_map() for your purpose.

People are also looking for solutions to the problem: php - angular with mysql to fetch database


Didn't find the answer?

Our community is visited by hundreds of web development professionals every day. Ask your question and get a quick answer for free.

Ask a Question

Write quick answer

Do you know the answer to this question? Write a quick response to it. With your help, we will make our community stronger.

Similar questions

Find the answer in similar questions on our website.