How to get item based on array_search?

Related: Convert data from complex array, with dictionary that is also a complex array - #4 by droopsnoot

<?php
 
$need = 123;
 
$player["some"][0]["need"] = "this";
$player["some"][0]["need"]["locator"] = 1553;
 
$player["some"][1]["need"] = "this";
$player["some"][1]["need"]["locator"] = 152351523;
 
$player["some"][2]["need"] = "this";
$player["some"][2]["need"]["locator"] = 12663;
 
$player["some"][3]["need"] = "this";
$player["some"][3]["need"]["locator"] = 1923;
 
$get = array_search($need, array_column($player["some"], "locator"));
$output = $player["some"][$get]["need"];
 
echo "get $output";
?>

How would I make it print “get this”?

STDOUT: get 1his.
STDERR:

PHP Warning:  Illegal string offset 'locator' in /home/mcC3LE/prog.php on line 6
PHP Warning:  Illegal string offset 'locator' in /home/mcC3LE/prog.php on line 9
PHP Warning:  Illegal string offset 'locator' in /home/mcC3LE/prog.php on line 12
PHP Warning:  Illegal string offset 'locator' in /home/mcC3LE/prog.php on line 15

I tried using example in provided URL (from question I asked earlier). But I walk either against error or against bool(false) which shouldn’t exist.

$player["some"][0]["need"] = "this";
$player["some"][0]["need"]["locator"] = 1553;

here $player["some"][0]["need"] is a string. And next thing you do is accessing a (named) array key on that string …

1 Like

This topic was automatically closed 91 days after the last reply. New replies are no longer allowed.