Question Details

No question body available.

Tags

php multidimensional-array

Answers (2)

July 18, 2026 Score: 2 Rep: 81,997 Quality: Medium Completeness: 80%

Indeed, arraysearch will not find your arrays inside the result of arraycolumn, but your idea with slight modifications can work. Overall, any array can be converted into JSON, in which case it will be a String representing the array and you can do this conversion via jsonencode. So basically if your value is an array, you can jsonencode it so you will search for a string and you can adjust besides the search terms the search data using arraymap to match such JSON terms:

$tags = array(
        'title' =>
            [
            'name' => 'Title',
            'id3' => 'TIT2',
            'VMD' => 'TITLE',
            ],
        'totaldisc' =>
            [
            'name' => 'Total Disc',
            'id3' =>
                [
                'TXXX' => 'TotalDisc',
                ],
            'VMD' => 'TOTALDISC',
            ],
        'genre' =>
            [
            'name' => 'Genre',
            'id3' => 'TCON',
            'VMD' => 'GENRE',
            ],
        'albumartist' =>
            [
            'name' => 'Album Artist',
            'id3' => 'TPE2',
            'VMD' => 'ALBUMARTIST',
            ],
);          

$tag = 'TCON';

function getResult($tags, $key, $value) { if (isarray($value)) { $value = jsonencode($value); } return arraysearch($value, arraymap(function($value) { return isarray($value) ? jsonencode($value) : $value; }, arraycolumn($tags, $key))); } echo vardump(getResult($tags, 'id3', [ 'TXXX' => 'TotalDisc' ]));

In the above I created a function and named it, with lack of context of your project getResult, please feel free to rename it to a more appropriate name:

  • it gets $tags for the search data, $key for the key (id3) you are interested of and $value, that is, the search term
  • it converts $value to JSON if it happens to be an array
  • it calls arraysearch
    • passing $value, which is either modified into a JSON, or kept as is
    • and transforming the search data via arraymap
      • which converts the elements of the search data to JSON if needed
      • and does that among the result of the call for arraycolumn
July 18, 2026 Score: 2 Rep: 606 Quality: Low Completeness: 50%

arraycolumn() only returns the value of the id3 field. When id3 is an array (like ['TXXX' => 'TotalDisc']), arraysearch() compares your string against the whole array, so it won't match.

You'll need to handle both cases. For example:

foreach (static::$tags as $key => $tagInfo) {
    if (isarray($tagInfo['id3'])) {
        if (arraykeyexists($tag, $tagInfo['id3']) || inarray($tag, $tagInfo['id3'], true)) {
            echo $key;
            break;
        }
    } elseif ($tagInfo['id3'] === $tag) {
        echo $key;
        break;
    }
}

This checks both a simple string ('TIT2') and an array (['TXXX' => 'TotalDisc']). array_search() alone can't handle both cases automatically.