普通网友 2019-04-29 18:22
浏览 56
已采纳

如何在复杂的数组中使用cakephp集合函数,它只适用于简单的数组

I transformed an XML to JSON, this is the link: https://api.myjson.com/bins/us5f4

I used a lot of CakePHP 3.30 and below Collection's functions. What I need to do is to only extract some data, for example the tag @INDICATOR and @TIME_PERIOD.

I already tried using nest, filter, append, map in collections and none of them work for some reason.

This is my controller:

<?php
namespace App\Controller;

use App\Controller\AppController;
use Cake\Collection\Collection;
use Cake\ORM\TableRegistry;
use Cake\Utility\Xml;

class BalancesFiscalesController extends AppController
{public function balancesDatos2(){
        $xmlArray = Xml::toArray(Xml::build('webroot/e-ggds/BalanceFiscalGC.xml'));


                    $collection = new Collection($xmlArray);
                    $collection->nest('@INDICATOR', '[]')->toList(); //I tried with the parent "Series" still same problem. It returns me the whole JSON instead of what I need. I also tried filter, map, append, no luck.

        //Gobierno Central
        $response = [
            'gob_central' =>[
                    'titulos' =>[
                        $collection
                    ]
                    ]
            ];


           $this->set([
            'data' => $response,
            '_serialize' => ['data']
              ]);

    }

}

  • 写回答

1条回答 默认 最新

  • duanbu4962 2019-04-30 21:30
    关注

    To extract a field from a collection, you'd use the extract function. Assuming that it's all under the [data][gob_central][titulos][0][StructureSpecificData][message:DataSet][Series] key, it might look like this:

    $series = $xmlArray['data']['gob_central']['titulos'][0]['StructureSpecificData']['message:DataSet']['Series'];
    $data = collection($series)->extract('@INDICATOR')->toArray();
    

    If there might be multiple items in the titulos key, then maybe more like this:

    $titulos = $xmlArray['data']['gob_central']['titulos'];
    $data = collection($titulos)->extract('StructureSpecificData.message:DataSet.Series.{*}.@INDICATOR')->toArray();
    
    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论

报告相同问题?

悬赏问题

  • ¥15 如何让企业微信机器人实现消息汇总整合
  • ¥50 关于#ui#的问题:做yolov8的ui界面出现的问题
  • ¥15 如何用Python爬取各高校教师公开的教育和工作经历
  • ¥15 TLE9879QXA40 电机驱动
  • ¥20 对于工程问题的非线性数学模型进行线性化
  • ¥15 Mirare PLUS 进行密钥认证?(详解)
  • ¥15 物体双站RCS和其组成阵列后的双站RCS关系验证
  • ¥20 想用ollama做一个自己的AI数据库
  • ¥15 关于qualoth编辑及缝合服装领子的问题解决方案探寻
  • ¥15 请问怎么才能复现这样的图呀