Answer the question
In order to leave comments, you need to log in
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
]
]
]
],
]
]);
}
...
'common' => [
'@name' => $object->title,
'@description' => $object->description,
//вот сюда вставить
'@param_1' => 'value_1',
'@param_2' => 'value_2',
//а дальше оставить как есть
'address' => [
'coordinates' => [
'@lat' => $object->latitude,
'@lon' => $object->longitude
]
]
]
...
...
'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
]
]
]
...
Answer the question
In order to leave comments, you need to log in
right at the description of the array.Nope.
if ($object->id = 777) {
$foo['common']['@param_1'] = 'value_1',
$foo['common']['@param_2'] = 'value_2',
}
Didn't find what you were looking for?
Ask your questionAsk a Question
731 491 924 answers to any question