0

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?

1
  • 1
    if you do array_map on an array, you get the values in the var $items, so "15" in your case, and "15" has no index "parent_id", it's not an array. It seems you use array_map for the wrong purpose, it is used to apply a function to all items in an array and return resulting new array with affected values. To acces a specific key in PHP, simply use $ids[$this->relatedKey] Commented Mar 2, 2018 at 11:10

1 Answer 1

1

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, and array_map() is supposed to transform it somehow.

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

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

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.