PHP populate multi dimensional array using 2 methods and loop, 1 depending on the other -


i'm trying build first multidimensional array - understand ok using hardcoded values, trying fill array values getting me tied in knots.

i've got 2 resultsets i'm using build array.

  • i have method $hostnames returns 'hostnames' object

    $hostnames = $server_model->get_hostnames();

  • i have method $usernames returns 'users' of 'hostname' specified above.

    $usernames = $userlogons_model->get_users_by_hostname($hostname->hostname);

first, i'm building array:

$hostnames = array(             $host => $hostname,             $users => array(                 $key => $username             )         ); 

then populating arrays:

$hostnames = $server_model->get_hostnames_for_costs();          foreach ($hostnames $hostname) {              $usernames = $userlogons_model->get_users_by_hostname($hostname);              echo $hostname->hostname;              foreach ($usernames $user){                  echo $user->username;              }          } 

my editor showing undefined variables i'm declaring arrays in first block. i'm getting no output - surely syntax error , i'm struggling. appreciated !

cheers, mike

in php there's no need "build" array , populate it. can create array populate it.

in first block, throwing errors because doesn't know $host, $hostname, $users, $key or $username is, i.e. undefined.

i'm not sure structure want final array i'm not sure $key, $host, etc. meant , there's million ways create array depending on need, here's example of can do:

// hostnames $hostnames = $server_model->get_hostnames_for_costs(); // iterate through each hostname foreach ($hostnames $hostname)  {     // users of hostname     $usernames = $userlogons_model->get_users_by_hostname($hostname);     // iterate through each user , append user array     $user_arr = array();     foreach($usernames $user)     {         // append user user array         $user_arr[] = $user;     }     // add user array      $my_arr[] = array (         'host' => $hostname,         'users' => $user_arr     ); }  echo var_dump($my_arr); 

Comments

Popular posts from this blog

c# - Better 64-bit byte array hash -

webrtc - Which ICE candidate am I using and why? -

php - Zend Framework / Skeleton-Application / Composer install issue -