合并两个PHP标记

I want to convert two different PHP tags into a single PHP tag.This may sound a little weird but recently I found a question like this and answer with correct marked.

I can't find I'm currently lost address.

My question:

for example;

$gates = array('t1','t2','t3',t4','t5');

$openGates-> and $gates merge.

Result:

$openGates->t1; or t2,t3.

If I remember correctly, the answer to the problem I found before was;

$openGates->{$gates}; like this. I not sure

How can I do that?

It's not simple for newbie programmer...

At first:

$gates = array('t1','t2','t3','t4','t5');

It's Array

$openGates->

This is class instance. Btw. You can retrieve class instance var like $className->varName

You can't simple merge array and class instance. But you can create new class instance variables by loop.

foreach($gates as $gateKey=>$gateVal) {
    $openGates->$gatesVal = NULL;
}

But I think it's, in result should be like that:

 $gates = array('t1'=>'opened','t2'=>'closed','t3'=>'closed','t4'=>'opened','t5'=>'opened');
foreach($gates as $gateKey=>$gateVal) {
    $openGates->$gateKey = $gateVal;
}

echo $openGates->t1;

// or

foreach($gates as $key=>$val) {
    echo $openGates->$key.PHP_EOL;
}

Btw you can simple set $openGates->gates = $gates; and call it like echo $openGates->gates['t1'];

You seem to be confusing objects with arrays. Arrays simply contain data, unless you create them with keys also. Such as:

$gates = array('t1'=>true,'t2'=>false,'t3'=>"maybe",'t4'=>0,'t5'=>50);

Matthew Page is correct in that you should look up PHP OOP for a solution.

That being said, you can cast your array to an object providing it has both keys and values:

$gates = (object) array('t1'=>true,'t2'=>false,'t3'=>"maybe",'t4'=>0,'t5'=>50);

or

$openGates = (object) $gates;

This will allow you to access 'properties' of the object in the way you have demonstrated:

echo $openGates->t1;, for example. The -> operator only applies to objects, which are instances of classes, not arrays.

If you do cast your array to the type of object, be sure that you have both keys and values.