K
K
K. A.2018-03-04 02:49:42
PHP
K. A., 2018-03-04 02:49:42

Is it possible to build an array by conditions?

I'm compiling an XML file using FluidXml and I ran into this problem:
depending on the conditions, I need to output several additional nodes / parameters in the body of the XML, but I can't figure out how to do it.
Now I have this code:

$query = AnpProperties::where('published', '>', '0')->get();
    
        $xml = new FluidXml(null, ['encoding'   => 'UTF-8' ]);

        $xml->addChild('MassUploadRequest', true, ['xmlns' => 'http://assis.ru/ws/api', 'timestamp' => time()]);

        foreach($query as $object){
            $object_xml = $xml->addChild([
                'object' => [
                    '@externalId' => $object->id,
                    '@publish'  => 'true',
                    'request' => [
                        '@xmlns:xsi' => 'http://www.w3.org/2001/XMLSchema-instance',
                        'common' => [
                            '@name' => $object->title,
                            '@description' => $object->description,
                            'address' => [
                                'coordinates' => [
                                    '@lat' => $object->latitude,
                                    '@lon' => $object->longitude
                                ]
                            ]
                        ]
                    ],
                ]
            ]);
        }

But if, conditionally, $object->id = 777, then I need to add a few more attributes to the "common" node, i.e. the common code should look something like this in this case:
...
                        'common' => [
                            '@name' => $object->title,
                            '@description' => $object->description,
//вот сюда вставить
                            '@param_1' => 'value_1',
                            '@param_2' => 'value_2',
//а дальше оставить как есть
                            'address' => [
                                'coordinates' => [
                                    '@lat' => $object->latitude,
                                    '@lon' => $object->longitude
                                ]
                            ]
                        ]
...

Because Since the construction is in a loop, I would like to somehow use the condition directly when describing the array. I tried using the "?:" operator - without success:
...
                        'common' => [
                            '@name' => $object->title,
                            '@description' => $object->description,
                            $object->id == 777 ? [
                                '@param_1' => 'value_1',
                                '@param_2' => 'value_2',
                            ] : null, 
                            'address' => [
                                'coordinates' => [
                                    '@lat' => $object->latitude,
                                    '@lon' => $object->longitude
                                ]
                            ]
                        ]
...

Maybe there is a working option? Or is it basically impossible? Thank you.

Answer the question

In order to leave comments, you need to log in

2 answer(s)
S
Stalker_RED, 2018-03-04
@Stalker_RED

right at the description of the array.
Nope.
But it's possible like this:
if ($object->id = 777) {
  $foo['common']['@param_1'] = 'value_1',
  $foo['common']['@param_2'] = 'value_2',
}

A
Andrey, 2018-03-04
@VladimirAndreev

$data = Array_merge($data, ($id== 100500 ? $['a'=>'b'][]))
But I wouldn't recommend doing that

Didn't find what you were looking for?

Ask your question

Ask a Question

731 491 924 answers to any question