JSON in Array umwandeln

Ich bin nicht sicher ob die Frage schon mal kam - gefunden habe ich nichts.

Ich will gerne ein JSON

[{"ProfileName":"~Battery","ProfileValue":true},{"ProfileName":"~Battery.Reversed","ProfileValue":false},{"ProfileName":"~Battery.100","ProfileValue":"25"}]"

In einen Array umwandeln … das bekomme ich schon mal hin :slight_smile: mit

$Profiles2MonitorArray = json_decode($Profiles2Monitor,true);

Da kommt dann auch ordentlich das hier raus

array(3) {
  [0]=>
  array(2) {
    ["ProfileName"]=>
    string(8) "~Battery"
    ["ProfileValue"]=>
    bool(true)
  }
  [1]=>
  array(2) {
    ["ProfileName"]=>
    string(17) "~Battery.Reversed"
    ["ProfileValue"]=>
    bool(false)
  }
  [2]=>
  array(2) {
    ["ProfileName"]=>
    string(12) "~Battery.100"
    ["ProfileValue"]=>
    string(2) "25"
  }
}

für mein Modul bräuchte ich aber

$Profiles = array("~Battery" => true, "~Battery.Reversed" => false, "~Battery.100" => 25);

Jetzt die Frage: Wie bekomme ich den unteren Zustand hin? Ich habe es mir str_replace probiert, aber so richtig bekomme ich den Overhead nicht raus. Gibt es hier einen eleganten weg?

Habe jetzt mal ein wildes str_replace probiert, aber schon beim JSON

$ToRemove = array('"ProfileName":',',"ProfileValue"');
$Profiles2Monitor = str_replace($ToRemove,"",$Profiles2Monitor);

da kommt jetzt das bei raus

string(69) "[{"~Battery":true},{"~Battery.Reversed":false},{"~Battery.100":"25"}]"
array(3) {
  [0]=>
  array(1) {
    ["~Battery"]=>
    bool(true)
  }
  [1]=>
  array(1) {
    ["~Battery.Reversed"]=>
    bool(false)
  }
  [2]=>
  array(1) {
    ["~Battery.100"]=>
    string(2) "25"
  }
}

Also besser, aber noch nicht so ganz wie ich es brauche.

Benutze Array Funktionen wie array_column oder schleifen wie foreach.
Das sind auch alles keine Fragen zum PHP SDK, oder wie man es benutzt.
Habe deine Themen entsprechend nach Scripts verschoben.
Da sind allgemeine PHP Frage besser platziert.
Michael

Mal ganz grob

$result_array = array();
foreach(json_decode($Profiles2Monitor,true) as $sub_array) {
  $result_array[$sub_array["ProfileName"]] = $sub_array["ProfileValue"];
}
1 „Gefällt mir“

Oder; ohne Schleife:

$Result = array_combine(
    array_column($Profiles2MonitorArray, 'ProfileName'), 
    array_column($Profiles2MonitorArray, 'ProfileValue')
    );
var_dump($Result);

PS: Das ist noch immer falsch, da sind ein paar doppelte Hochkomma am Ende hinter der letzten ] zu viel.

Warum schreibst du nicht direkt ein Array? Den String brauchst du doch nie direkt.
Einfach immer json_encode und json_decode benutzen.

        $Profiles2MonitorArray = [
            [
                'ProfileName'  => '~Battery',
                'ProfileValue' => true
            ],
            [
                'ProfileName'  => '~Battery.Reversed',
                'ProfileValue' => false
            ],
            [
                'ProfileName'  => '~Battery.100',
                'ProfileValue' => '25'
            ]
        ];
1 „Gefällt mir“

@sokkederheld danke dir - habe es eingebaut und ging auf anhieb!!!

@Nall-chan danke wie immer

Jetzt ich weiter an dem Modul feilen.