Array als Variable abspeichern

Hallo Community,

ist es möglich, einen mehrdimensionalen Array zu erstellen und als IPS-Variable abzuspeichern?

Hintergrund:
Ich möchte in diesem Array alle wichtigen Variablen-IDs ablegen, damit diese von verschiedenen Skripten ausgelesen werden können.

Anbei das Skript zum Erstellen des Arrays.

Vielen Dank für Tipps.

Viele Grüße
Wickie


<?
$ID_array = 44171;

$ii = array ();
$jj = array ();

$jj = array ( "nb","nb","nb","nb","nb","nb",
      "nb","nb","nb","nb","nb",
      "nb","nb","nb","nb","nb",
		"nb","nb");

/////////////////////////////////
$Parameter = ("Raum");
$ii [0] = array (  $Parameter        => "FlurOG");
$ii [1] = array (  $Parameter        => "Ankleide");
$ii [2] = array (  $Parameter        => "BadOG");
$ii [3] = array (  $Parameter        => "KindOst");
$ii [4] = array (  $Parameter        => "KindWest");
$ii [5] = array (  $Parameter        => "Eltern");

$ii [6] = array (  $Parameter        => "Eingang");
$ii [7] = array (  $Parameter        => "Speis");
$ii [8] = array (  $Parameter        => "Wohnen");
$ii [9] = array (  $Parameter        => "Arbeit");
$ii [10] = array (  $Parameter        => "BadEG");

$ii [11] = array (  $Parameter        => "FlugKG");
$ii [12] = array (  $Parameter        => "Keller");
$ii [13] = array (  $Parameter        => "Waschen");
$ii [14] = array (  $Parameter        => "Hobby");
$ii [15] = array (  $Parameter        => "Technik");

$ii [16] = array (  $Parameter        => "Aussen");
$ii [17] = array (  $Parameter        => "Garage");


/////////////////////////////////
// T_Ist $ii[$j][0]
$jj = array ();
$jj = array ( "nb","nb",12345,"nb","nb","nb",
      "nb","nb","nb","nb","nb",
      "nb","nb",23456,"nb","nb",
		"nb","nb");
for($j=0; $j<count($ii) ; $j++)	//i Anzahl Raume
	{
		$Parameter = ("ID_T_Ist");
		if($jj[$j]!= "nb")
			{
				$ii[$j][0] = array ( $Parameter      => $jj[$j]);
			}
	}
/////////////////////////////////
// FBH   $ii[$j][1]
$jj = array ();
$jj = array ( "nb","nb","nb","nb","nb","nb",
      "nb",36598,"nb","nb","nb",
      "nb","nb","nb","nb","nb",
		"nb","nb");
for($j=0; $j<count($ii) ; $j++)	//i Anzahl Raume
	{
		$Parameter = ("ID_T_Soll_ohnePV");
		if($jj[$j]!= "nb")
			{
				$ii[$j][1] = array ( $Parameter      => $jj[$j]);
			}
	}
/////////////////////////////////
// WHK   $ii[$j][2]
$jj = array ();
$jj = array ( "nb","nb","nb","nb",44444,"nb",
      "nb","nb","nb","nb","nb",
      "nb","nb","nb","nb","nb",
		"nb","nb");
for($j=0; $j<count($ii) ; $j++)	//i Anzahl Raume
	{
		$Parameter = ("ID_T_Soll_mitPV");
		if($jj[$j]!= "nb")
			{
				$ii[$j][2] = array ( $Parameter      => $jj[$j]);
			}
	}
/////////////////////////////////
/////////////////////////////////
/////////////////////////////////
		
		
/////////////////////////////////
// echo
for($j=0; $j<count($ii) ; $j++)	//i Anzahl Raume
	{
		echo $ii[$j]["Raum"];
		if (isset($ii[$j][0]))
			{
				echo "  ID_T_Ist:", $ii[$j][0]["ID_T_Ist"], "  ";
			}
		if (isset($ii[$j][1]))
			{
				echo "  ID_T_Soll_ohnePV:", $ii[$j][1]["ID_T_Soll_ohnePV"], "  ";
			}
		if (isset($ii[$j][2]))
			{
				echo "  ID_T_Soll_mitPV:", $ii[$j][2]["ID_T_Soll_mitPV"], "  ";
				echo "  ID2:", $ii[$j][2]["ID_T_Soll_mitPV"], "  ";
			}
		echo "
";
	}

//SetValue($ID_array,$ii);

//$parStr = $ComPort.",".$RegVar.",".$AbfrageID.",".$VariablenDivID;
///SetValueString($parStrID, $parStr);
?>


Hatten wir das nicht erst ?

PHP: serialize - Manual
PHP: unserialize - Manual

oder, was ich lieber nutze :slight_smile:

PHP: json_encode - Manual
PHP: json_decode - Manual

Den erhaltenen String in eine IPS-Variable schreiben und freuen :smiley:
Michael

oh super, danke

werde ich mir morgen mal anschauen…

das decodieren war erfolgreich, jedoch schaffe ich das encodieren nicht…
kannst du mir auf die Sprünge helfen?


<?
$ID_array = 44171 /*[Ger�te\Sonstiges\array_Raum]*/;

$ii = array ();
$jj = array ();

$jj = array ( "nb","nb","nb","nb","nb","nb",
      "nb","nb","nb","nb","nb",
      "nb","nb","nb","nb","nb",
		"nb","nb");

/////////////////////////////////
$Parameter = ("Raum");
$ii [0] = array (  $Parameter        => "FlurOG");
$ii [1] = array (  $Parameter        => "Ankleide");
$ii [2] = array (  $Parameter        => "BadOG");
$ii [3] = array (  $Parameter        => "KindOst");
$ii [4] = array (  $Parameter        => "KindWest");
$ii [5] = array (  $Parameter        => "Eltern");

$ii [6] = array (  $Parameter        => "Eingang");
$ii [7] = array (  $Parameter        => "Speis");
$ii [8] = array (  $Parameter        => "Wohnen");
$ii [9] = array (  $Parameter        => "Arbeit");
$ii [10] = array (  $Parameter        => "BadEG");

$ii [11] = array (  $Parameter        => "FlugKG");
$ii [12] = array (  $Parameter        => "Keller");
$ii [13] = array (  $Parameter        => "Waschen");
$ii [14] = array (  $Parameter        => "Hobby");
$ii [15] = array (  $Parameter        => "Technik");

$ii [16] = array (  $Parameter        => "Aussen");
$ii [17] = array (  $Parameter        => "Garage");


/////////////////////////////////
// T_Ist $ii[$j][0]
$jj = array ();
$jj = array ( "nb","nb",12345 /*[Objekt #12345 existiert nicht]*/,"nb","nb","nb",
      "nb","nb","nb","nb","nb",
      "nb","nb",23456 /*[Objekt #23456 existiert nicht]*/,"nb","nb",
		"nb","nb");
for($j=0; $j<count($ii) ; $j++)	//i Anzahl Raume
	{
		$Parameter = ("ID_T_Ist");
		if($jj[$j]!= "nb")
			{
				$ii[$j][0] = array ( $Parameter      => $jj[$j]);
			}
	}
/////////////////////////////////
// FBH   $ii[$j][1]
$jj = array ();
$jj = array ( "nb","nb","nb","nb","nb","nb",
      "nb",36598 /*[Objekt #36598 existiert nicht]*/,"nb","nb","nb",
      "nb","nb","nb","nb","nb",
		"nb","nb");
for($j=0; $j<count($ii) ; $j++)	//i Anzahl Raume
	{
		$Parameter = ("ID_T_Soll_ohnePV");
		if($jj[$j]!= "nb")
			{
				$ii[$j][1] = array ( $Parameter      => $jj[$j]);
			}
	}
/////////////////////////////////
// WHK   $ii[$j][2]
$jj = array ();
$jj = array ( "nb","nb","nb","nb",44444 /*[Objekt #44444 existiert nicht]*/,"nb",
      "nb","nb","nb","nb","nb",
      "nb","nb","nb","nb","nb",
		"nb","nb");
for($j=0; $j<count($ii) ; $j++)	//i Anzahl Raume
	{
		$Parameter = ("ID_T_Soll_mitPV");
		if($jj[$j]!= "nb")
			{
				$ii[$j][2] = array ( $Parameter      => $jj[$j]);
			}
	}
/////////////////////////////////
/////////////////////////////////
/////////////////////////////////
		
		
/////////////////////////////////
// echo
for($j=0; $j<count($ii) ; $j++)	//i Anzahl Raume
	{
		echo $ii[$j]["Raum"];
		if (isset($ii[$j][0]))
			{
				echo "  ID_T_Ist:", $ii[$j][0]["ID_T_Ist"], "  ";
			}
		if (isset($ii[$j][1]))
			{
				echo "  ID_T_Soll_ohnePV:", $ii[$j][1]["ID_T_Soll_ohnePV"], "  ";
			}
		if (isset($ii[$j][2]))
			{
				echo "  ID_T_Soll_mitPV:", $ii[$j][2]["ID_T_Soll_mitPV"], "  ";
				echo "  ID2:", $ii[$j][2]["ID_T_Soll_mitPV"], "  ";
			}
		echo "
";
	}

SetValue($ID_array,json_encode($ii));
echo json_encode($ii);
//json_decode($json)
//mixed json_decode ( string $json [, bool $assoc = false [, int $depth = 512 [, int $options = 0 ]]] )
//$parStr = $ComPort.",".$RegVar.",".$AbfrageID.",".$VariablenDivID;
///SetValueString($parStrID, $parStr);
?>



[{"Raum":"FlurOG"},{"Raum":"Ankleide"},{"Raum":"BadOG","0":{"ID_T_Ist":12345}},{"Raum":"KindOst"},{"Raum":"KindWest","2":{"ID_T_Soll_mitPV":44444}},{"Raum":"Eltern"},{"Raum":"Eingang"},{"Raum":"Speis","1":{"ID_T_Soll_ohnePV":36598}},{"Raum":"Wohnen"},{"Raum":"Arbeit"},{"Raum":"BadEG"},{"Raum":"FlugKG"},{"Raum":"Keller"},{"Raum":"Waschen","0":{"ID_T_Ist":23456}},{"Raum":"Hobby"},{"Raum":"Technik"},{"Raum":"Aussen"},{"Raum":"Garage"}]

decodieren

<?
$ID_array = 44171 /*[Geräte\Sonstiges\array_Raum]*/;


$json = GetValue($ID_array);
//echo json_encode($ii);
$ii = ( json_decode($json));
//mixed json_decode ( string $json [, bool $assoc = false [, int $depth = 512 [, int $options = 0 ]]] )
//$parStr = $ComPort.",".$RegVar.",".$AbfrageID.",".$VariablenDivID;
///SetValueString($parStrID, $parStr);

/////////////////////////////////
// echo
echo $ii[0]["Raum"];
?>



Fatal error:  Cannot use object of type stdClass as array in [Geräte\Sonstiges\Array_Raum_V01b] on line 14

Du hast da beim decode einen Parameter über sehen.
Schau mal in das Manual:
assoc
When TRUE, returned objects will be converted into associative arrays.
Michael

Dank dir Michael,
das „true“ hat gefehlt.
Jetzt funktionierts wie gewollt. :cool:


<?
$ID_array = 44171 /*[Geräte\Sonstiges\array_Raum]*/;
$json = GetValue($ID_array);
$ii = ( json_decode($json,true));
?>