2017-12-01 3 views
0

뭔가 문제가 있습니다. Youtube에서 재생 목록 항목의 데이터를 가져 와서 노래 제목을 사용하여 재생 목록을 특정 아티스트 배열로 구성합니다.PHP - 배열을 통한 검색 및 값 저장

이것은 배열이

$playlistMaster = array (
    'foo fighters' => array(), 
    'queens of the stone age' => array(), 
    ... 
    ... 
    'misc' => array() 
) 

처럼 이것은 배열

희망
//For each playlist item 
    foreach($playlist->items AS $item): 
     // Stores the song title in a var and makes it lowercase 
     $str = $item->snippet->title; 
     $str = strtolower($str); 

     // Stores the last key of the array 
     $last = end($playlistMaster); 

     // Takes the first key of the array and checks to see if the song title matches the key name 
     $keyString = key($playlistMaster); 
     $pos = strpos($str, $keyString); 

     // If it is not a match, then move on to the next key, if it reaches the end of the arrays then store that song in the last array. 
     if ($pos === false) { 
      next($playlistMaster); 

      if (next($playlistMaster) === $last) { 
       $songId = $item->snippet->title; 
       array_push($playlistMaster[$keyString], $songId); 
       reset($playlistMaster); 
      }     

      $keyString = key($playlistMaster); 
      $pos = strpos($str, $keyString); 
     } 

     // Else if it is a match then store that song in the current array. 
     else { 
      $songId = $item->snippet->title; 
      array_push($playlistMaster[$keyString], $songId); 
      reset($playlistMaster); 
     } 
    endforeach; 

에 저장 내가 처리하고있는 코드는 무엇인가,이 감각의 일종 수 있습니까? 나는 현재 오류가 없지만 어떤 배열에도 저장되어 있지 않으며 각 단계에서 무슨 일이 일어나고 있는지 알기 위해 Java에서와 같이 '단계별로'PHP를 찾는 방법을 찾을 수 없었습니다. 조금이라도 피드백이 없어 좌절했다.

감사합니다.

+1

$ playlist 배열의 구조는 무엇입니까? –

+0

배열의 일부를 내 게시물 상단에 썼습니다. 감사합니다 –

+0

그건 $ playlistMaster 배열입니까? –

답변

0

아래 코드를 확인하면 요구 사항을 충족시킬 수 있습니다.

<?php 

$playlistMaster = array (
    'foo fighters' => array('aaa'), 
    'queens of the stone age' => array('bbb'), 
    'misc' => array('ccc') 
); 

$playlist = array('ar' => 'ddd','misc' => 'eee'); 
//For each playlist item 
//key is author and value is title/song 
    foreach($playlist AS $key=>$val) { 
     if(isset($playlistMaster[$key])) { 
      if(!in_array($val,$playlistMaster[$key])) { 
       array_push($playlistMaster[$key],$val); 
      } 
     } else { 
      $playlistMaster[$key] = array($val); 
     } 
    } 
echo '<pre>'; 
print_r($playlistMaster); 
print_r($playlist);