从数组val中选择所选名称

时间:2010-02-28 10:27:42

标签: php

Array (
  [4] => stdClass Object (
     [vid] => 4
     [name] => Movie
     [description] =>
     [help] =>
     [relations] => 1
     [hierarchy] => 0
     [multiple] => 0
     [required] => 1
     [tags] => 1
     [module] => taxonomy
     [weight] => 0
     [nodes] => Array ( [lyrics] => lyrics )
  )
  [1] => stdClass Object (
     [vid] => 1
     [name] => Music Director
     [description] =>
     [help] =>
     [relations] => 1
     [hierarchy] => 0
     [multiple] => 1
     [required] => 1
     [tags] => 1
     [module] => taxonomy
     [weight] => 0
     [nodes] => Array ( [lyrics] => lyrics )
  )
  [3] => stdClass Object (
     [vid] => 3
     [name] => Singer/s
     [description] =>
     [help] =>
     [relations] => 1
     [hierarchy] => 0
     [multiple] => 1
     [required] => 1
     [tags] => 1
     [module] => taxonomy
     [weight] => 1
     [nodes] => Array ( [lyrics] => lyrics )
  )
  [2] => stdClass Object (
     [vid] => 2
     [name] => Lyricist
     [description] =>
     [help] =>
     [relations] => 1
     [hierarchy] => 0
     [multiple] => 1
     [required] => 1
     [tags] => 1
     [module] => taxonomy
     [weight] => 2
     [nodes] => Array ( [lyrics] => lyrics )
  )
)
Array (
  [4] => stdClass Object (
     [vid] => 4
     [name] => Movie
     [description] =>
     [help] =>
     [relations] => 1
     [hierarchy] => 0
     [multiple] => 0
     [required] => 1
     [tags] => 1
     [module] => taxonomy
     [weight] => 0
     [nodes] => Array ( [lyrics] => lyrics )
  )
  [1] => stdClass Object (
     [vid] => 1
     [name] => Music Director
     [description] =>
     [help] =>
     [relations] => 1
     [hierarchy] => 0
     [multiple] => 1
     [required] => 1
     [tags] => 1
     [module] => taxonomy
     [weight] => 0
     [nodes] => Array ( [lyrics] => lyrics )
  )
  [3] => stdClass Object (
     [vid] => 3
     [name] => Singer/s
     [description] =>
     [help] =>
     [relations] => 1
     [hierarchy] => 0
     [multiple] => 1
     [required] => 1
     [tags] => 1
     [module] => taxonomy
     [weight] => 1
     [nodes] => Array ( [lyrics] => lyrics )
  )
  [2] => stdClass Object (
     [vid] => 2
     [name] => Lyricist
     [description] =>
     [help] =>
     [relations] => 1
     [hierarchy] => 0
     [multiple] => 1
     [required] => 1
     [tags] => 1
     [module] => taxonomy
     [weight] => 2
     [nodes] => Array ( [lyrics] => lyrics )
  )
)
Array (
  [4] => stdClass Object (
     [vid] => 4
     [name] => Movie
     [description] =>
     [help] =>
     [relations] => 1
     [hierarchy] => 0
     [multiple] => 0
     [required] => 1
     [tags] => 1
     [module] => taxonomy
     [weight] => 0
     [nodes] => Array ( [lyrics] => lyrics )
  )
  [1] => stdClass Object (
     [vid] => 1
     [name] => Music Director
     [description] =>
     [help] =>
     [relations] => 1
     [hierarchy] => 0
     [multiple] => 1
     [required] => 1
     [tags] => 1
     [module] => taxonomy
     [weight] => 0
     [nodes] => Array ( [lyrics] => lyrics )
  )
  [3] => stdClass Object (
     [vid] => 3
     [name] => Singer/s
     [description] =>
     [help] =>
     [relations] => 1
     [hierarchy] => 0
     [multiple] => 1
     [required] => 1
     [tags] => 1
     [module] => taxonomy
     [weight] => 1
     [nodes] => Array ( [lyrics] => lyrics )
  )
  [2] => stdClass Object (
     [vid] => 2
     [name] => Lyricist
     [description] =>
     [help] =>
     [relations] => 1
     [hierarchy] => 0
     [multiple] => 1
     [required] => 1
     [tags] => 1
     [module] => taxonomy
     [weight] => 2
     [nodes] => Array ( [lyrics] => lyrics )
  )
)
Array (
  [4] => stdClass Object (
     [vid] => 4
     [name] => Movie
     [description] =>
     [help] =>
     [relations] => 1
     [hierarchy] => 0
     [multiple] => 0
     [required] => 1
     [tags] => 1
     [module] => taxonomy
     [weight] => 0
     [nodes] => Array ( [lyrics] => lyrics )
  )
  [1] => stdClass Object (
     [vid] => 1
     [name] => Music Director
     [description] =>
     [help] =>
     [relations] => 1
     [hierarchy] => 0
     [multiple] => 1
     [required] => 1
     [tags] => 1
     [module] => taxonomy
     [weight] => 0
     [nodes] => Array ( [lyrics] => lyrics )
  )
  [3] => stdClass Object (
     [vid] => 3
     [name] => Singer/s
     [description] =>
     [help] =>
     [relations] => 1
     [hierarchy] => 0
     [multiple] => 1
     [required] => 1
     [tags] => 1
     [module] => taxonomy
     [weight] => 1
     [nodes] => Array ( [lyrics] => lyrics )
  )
  [2] => stdClass Object (
     [vid] => 2
     [name] => Lyricist
     [description] =>
     [help] =>
     [relations] => 1
     [hierarchy] => 0
     [multiple] => 1
     [required] => 1
     [tags] => 1
     [module] => taxonomy
     [weight] => 2
     [nodes] => Array ( [lyrics] => lyrics )
  )
)

从这里开始,我想选择下面的内容。

  • name(array)
  • 电影,音乐总监,歌手

2 个答案:

答案 0 :(得分:1)

我不太确定你想要什么,但如果我理解正确,那么这应该有效:

// Pass the array that you included in your question.
function getNames($inputArray) {
    $outputArray = array();
    foreach($inputArray as $entry) {
        $outputArray[] = $entry->name;
    }
    return $outputArray;
}

或者,如果您想要通过名称键入的原始对象,则可以使用:

// Pass the array that you included in your question.
function keyByName($inputArray) {
    $outputArray = array();
    foreach($inputArray as $entry) {
        $outputArray[$entry->name] = $entry;
    }
    return $outputArray;
}

如果您需要处理重复项,可以在outputArray中为包含重复项的条目包含数组:

// Pass the array that you included in your question.
function keyByNameAllowDuplicates($inputArray) {
    $outputArray = array();
    foreach($inputArray as $entry) {
        if(isset($outputArray[$entry->name])) {
            if(!is_array($outputArray[$entry->name])) {
                $outputArray[$entry->name] = array($outputArray[$entry->name]);
            }
            $outputArray[$entry->name][] = $entry;
        } 
        else 
        {
            $outputArray[$entry->name] = $entry;
        }
    }
    return $outputArray;
}

希望其中一个片段有所帮助。

答案 1 :(得分:0)

function displayTaxonomyWithVocabulary($nid) {
    $terms = taxonomy_node_get_terms($nid);
    $prevVid = 0;
    $taxonomies = array();
    foreach ($terms as $tid => $term) {
        if (array_key_exists($term->vid, $taxonomies) === false){
            $vocabulary = taxonomy_get_vocabularies('Lyrics');
            $term_id_is = $term->vid;
            $final_out = $vocabulary[$term_id_is];
            $taxonomies[$term->vid]['name'] = $final_out->name;
        }
        $taxonomies[$term->vid]['tags'][] = l($term->name, taxonomy_term_path($term), array('rel' => 'tag', 'title' => strip_tags($term->description)));
    }
    $tagsLink = '<table border=\'0\'>';
    foreach($taxonomies as $vocabularyTags) {
      $tagsLink .= "<tr>";
      $tagsLink .= '<td>' . $vocabularyTags['name'] . ': ';
      $tagsLink .= implode(", ", $vocabularyTags['tags'] );
      $tagsLink .= '</td>';
      $tagsLink .= "</tr>";
    }
    $tagsLink .= '</table>';
    return $tagsLink;
}

这是一个片段,它解决了我的问题。