php - Illegal string offset in array_map

610

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?

893

Answer

Solution:

Look at this example taken from the documentation (http://php.net/manual/en/function.array-map.php):

<?php
function cube($n)
{
    return($n * $n * $n);
}

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

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

Source

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.