Last Updated: February 25, 2016
·
1.804K
· dubrod

Create Dynamic Nested Arrays

//CLIENT VARIABLES
$napFilter = "client name";
$client = "unique id";

//DISTINCT KEYWORD LIST FOR THIS CLIENT
$keywordsList = $db->select("SELECT DISTINCT keyword FROM local_keywords WHERE client = '".$client."' ORDER BY row_id DESC");

//CLEAN ARRAY THE OVERAL CLIENT ARRAY
$clientArr = array();

//LOOP THROUGH EACH DISTINCT KEYWORD
foreach($keywordsList as $row){     

//START THE KEYWORD ARRAY & NAME IT
$keywordArray = array('id'=>$row["keyword"]);

//GET THIS DATE RANGE RESULTS FOR THIS KEYWORD FOR THIS CLIENT   
$keywordLoop = $db->select("SELECT * FROM local_results WHERE keyword='".$row["keyword"]."' AND client = '".$client."' AND date='".$_SESSION['daterange']."' ORDER BY row_id DESC");  

//CLEAN THE RESULTS ARRAY   
$resultsArray = array();

//LOOP THROUGH EACH RESULT ROW
foreach($keywordLoop as $results){

    //FILTER RESULTS FOR THIS CLIENT. WE SCRAPE ALL 10 RESULTS FOR POSSIBLE COMPETITION TRACKING IN V2      
    $nameCheck = $results["name"];
    if(strpos($nameCheck, $napFilter) !== false){ 

        //CREATE THE RESULTS ARRAY
        $resultsArray[] = array('source' => $results["source"], 'rank' => $results["rank"], 'date' => $results["timestamp"]);
    }   
}

//ADD RESULTS ARRAY TO THE KEYWORD ARRAY
$keywordArray["results"] = $resultsArray;

//ADD THE KEYWORD ARRAY TO THE CLIENT ARRAY
$clientArr[] = $keywordArray;
}

$db->select through SADO

http://www.shayanderson.com/projects/sado-php-orm.htm