DalexHD
DalexHD

Reputation: 63

Combine foreach output data

I´m working on a project where depending on the situation, I need to combine some output data into a single variable.

*$array contains different user information

$array[]= array(
    'ts3_uuid' => $value['client_unique_identifier'],
    'channel_name' => $value['client_unique_identifier'],
    'steam_id' => $steam_id,
    'ts3_clid' => $value['clid'],
    'channel_id' => $value['cid'],
    'steam_name' => htmlspecialchars($steam_name),
    'csgo_rank' => $csgo_rank,
    'steam_status' => $steam_official_status,
    'last_steam_connection' => $timestamp,
    'steam_vac_status' => $result_steam_ban,
    'csgo_played_time' => $total_tiempo_jugado,
    'csgo_hs_porcentage' => $hs_porcent,
    'csgo_kdr' => $kdr
    );
foreach ($array as $data) {
    $channel_description = $data['steam_name'];
}

This is the structure that i have on my mind...

if (the channel_id of different users are EQUAL){
combine their $data['steam_name'] into the $channel_description variable and 
then, for example, echo it.
}

I hope you can help me :-)

Upvotes: 3

Views: 137

Answers (2)

Silentexpert
Silentexpert

Reputation: 31

You can process all data in the array:

  foreach($array as $user_index=>$user_array)
        {
        foreach($user_array as $array_index=>$array_data)
              {
              $channel_id_array[$user_index]=$array_data['channel_id'];
              if(in_array($array_data['channel_id'],$channel_id_array))
                     {
                     echo'this channel_id is not unique <br/>';
                     echo 'first array with equal chanel_id';
                     print_r($array[$user_index]);
                     echo 'second array with equal channel id';  
                     print_r($array_data);
                     }
              }
         }

Upvotes: 2

Jack jdeoel
Jack jdeoel

Reputation: 4584

Use one helper array and function to find same channel_id and store its steam_name ! In php array ,call same index is not create new array ! so try setting channel_id as index key .

$result = findSameChannelId($array);

foreach($result as $data) {
    echo $data["channel_description"]."<br>";
}

function findSameChannelId($array) {
    foreach ($array as $key => $value) {
        if(!isset($channel[$value["channel_id"]])) {
            $channel[$value["channel_id"]] = array("channel_description"=>"");  
        } 
        $channel[$value["channel_id"]]['channel_description'] .= $value["steam_name"];  
    }
    return $channel;
}

Upvotes: 1

Related Questions