Jump to content

Array search problems


idaachi

Recommended Posts

 

I have an array containing a list of server names called $arPageServers

I have another array containing variables and keys passed using a form which also reflect server names, this one is called $arActive. These elements in this array have been set by user to active. I.e the checkboxes are clicked in the form.

 

First of all I want to set all the elements in $arPageServers to $arPageServers["active"]= false;

 

then, for each $Aractive as $key => $value, I want to go through the $arPageServers from the beginning to the end and search if the values in  $value["name"] gives a match.

If match is true, I want to set the corresponding element in $arPageServers to $arPageServers["active"]= true;

When this is done I want to write the  new file to reflect these changes.

This script just doesn't seen to work. Please help pinpointing a better way. Thanks.

 

 

<?php

  $arActive = array();

      if (array_key_exists("submit", $_GET))

      {

      if (!empty($_GET["active"]))

      {

        $arActive = $_GET["active"];

      }// end of(!empty($_GET["active"]))

      else

      {

      die(" I died here");

      }//end of else

   

      }//end of array_key_exists("submit", $_GET))

   

      foreach ($arActive as $key => $value)

      {

        $arActive[$key] = preg_replace("/^active_/", "", $value);

      }

     

      // Load page.conf

     

      $arPageConf = read_contents_pageconf("page.conf");

      $arPageServers = create_servernamelist_pageconf($arPageConf);

     

 

     

      foreach($arActive as $key => $value)

      {

        $i=0; $MaxPageServers = count($arPageServers);

        for ($i=0; $i<$MaxPageServers; $i++)

        {

        $arCurrentPageServer = $arPageServers[$i];

        } 

        if( $arCurrentPageServer["name"] = $value)

          {

          $arPageServer[$key]["active"]= true;

          } 

 

    //write new page.conf

   

      $nFileHandle = fopen("page.conf", "w");

      $i=0;

      $nMaxlength = count($arPageServers);

   

      for ($i=0; $i<$nMaxlength; $i++)

      {

      $arCurrentLine = $arPageServers[$i];

      $strNewLine ="";

     

      $bIsActive = $arCurrentLine["active"];

     

      if ($bIsActive==false)

      {

        $strNewLine = $strNewLine."#";

      }//end if ($bIsActive==false)

     

      //add URL to string

      $strNewLine = $strNewLine.$arCurrentLine["active"]."\t";

      fwrite($nFileHandle,$strNewLine);

     

      }//end for ($i=0; $i<$nMaxlength; $i++)

      fclose($nFileHandle);

 

 

 

Link to comment
https://forums.phpfreaks.com/topic/80633-array-search-problems/
Share on other sites

 

I had forgotten to setting all elements in $arPageServers["active"]=false;

Still doesn't work. Help please.

 

   

<?php

    $arActive = array();

    if (array_key_exists("submit", $_GET))

      {

      if (!empty($_GET["active"]))

      {

        $arActive = $_GET["active"];

      }// end of(!empty($_GET["active"]))

      else

      {

      die(" I died here");

      }//end of else

   

      }//end of array_key_exists("submit", $_GET))

   

      foreach ($arActive as $key => $value)

      {

        $arActive[$key] = preg_replace("/^active_/", "", $value);

      }

     

      // Load page.conf

     

      $arPageConf = read_contents_pageconf("page.conf");

      $arPageServers = create_servernamelist_pageconf($arPageConf);

     

      foreach($arPingServers as $key => $value)

      {

        if ($value["active"]= true)

            {

            $arPingServers[$key]["active"] = false;

            }

      }

 

     

      foreach($arActive as $key => $value)

      {

        $i=0; $MaxPageServers = count($arPageServers);

        for ($i=0; $i<$MaxPageServers; $i++)

        {

        $arCurrentPageServer = $arPageServers[$i];

        } 

        if( $arCurrentPageServer["name"] = $value)

          {

          $arPageServer[$key]["active"]= true;

          } 

 

    //write new page.conf

   

      $nFileHandle = fopen("page.conf", "w");

      $i=0;

      $nMaxlength = count($arPageServers);

   

      for ($i=0; $i<$nMaxlength; $i++)

      {

      $arCurrentLine = $arPageServers[$i];

      $strNewLine ="";

     

      $bIsActive = $arCurrentLine["active"];

     

      if ($bIsActive==false)

      {

        $strNewLine = $strNewLine."#";

      }//end if ($bIsActive==false)

     

      //add URL to string

      $strNewLine = $strNewLine.$arCurrentLine["active"]."\t";

      fwrite($nFileHandle,$strNewLine);

     

      }//end for ($i=0; $i<$nMaxlength; $i++)

      fclose($nFileHandle);

      ?>

Link to comment
https://forums.phpfreaks.com/topic/80633-array-search-problems/#findComment-408897
Share on other sites

Archived

This topic is now archived and is closed to further replies.

×
×
  • Create New...

Important Information

We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue.