PHP获取具有特定键值的对象

时间:2017-03-29 10:25:10

标签: php

我试图只使用namespace =" global"来获取这些metafields对象,如何在PHP中执行此操作?

Connection.prototype.FillCourseWithSubmenuContent = function(course){
    var self = this; //This class
    var submenuItems = [];
    return new BPromise(function(resolve, reject){
      return Api.GetCourseSubmenuUrl(ApiConnection.authToken).then(function(response){
        return request.get({url: self.url + response.url + course.id, followRedirect: false, jar: cookiejar}, function(err,httpResponse,body){
          if(err){
            reject(err);
          }
          var cheerio = require('cheerio');
          var dashboardhtml = cheerio.load(body, {
                  normalizeWhitespace: true,
                  decodeEntities: true
              }
          );
          //Find all the links on the page
          dashboardhtml('a').each(function(i, elem) {
              console.log("Object:");
              console.log({"text":dashboardhtml(elem).text(), "url":dashboardhtml(elem).attr('href')});
              submenuItems.push({"text":dashboardhtml(elem).text().trim(), "url":dashboardhtml(elem).attr('href')});
          });
          resolve();
        });
      }).then(function(){
        console.log(submenuItems);
        return Api.FilterSubmenuContentList(ApiConnection.authToken, submenuItems);
      });
    }).catch(function(error){
      return reject(error);
    });
};

1 个答案:

答案 0 :(得分:4)

$response = json_decode($response, true);

$filtered['metafields'] = array_filter($response['metafields'], function ($item) {
   return $item['namespace'] === 'global'; 
});

var_dump($filtered);

array(1) {
  ["metafields"]=>
  array(2) {
    [0]=>
    array(2) {
      ["id"]=>
      int(30007223558)
      ["namespace"]=>
      string(6) "global"
    }
    [2]=>
    array(2) {
      ["id"]=>
      int(154644565)
      ["namespace"]=>
      string(6) "global"
    }
  }
}