简体   繁体   English

如何通过多维数组中的特定键引用/拉取/排序

[英]How to Reference/pull/sort by a specific key in a multidimensional array

I am writing a page that pulls images and image data out of a multidimensional array.我正在编写一个从多维数组中提取图像和图像数据的页面。 I need to be able to click a button that calls a function to sort out the images by tags(IE tag_GlassDoor & tag_GlassWall) - basically to show only images that do or do not have that particular element (in this case im using 0 and 1 for yes and no), such as a glass door.我需要能够单击一个调用 function 的按钮来按标签(IE tag_GlassDoor 和 tag_GlassWall)对图像进行分类 - 基本上只显示具有或不具有该特定元素的图像(在这种情况下我使用 0 和 1是和否),例如玻璃门。 I can currently make that array display the data, but I cant figure out how to sort the data by one of the array keys, or even really the syntax to pull a single value out at will.我目前可以使该数组显示数据,但我无法弄清楚如何通过数组键之一对数据进行排序,甚至无法弄清楚随意拉出单个值的语法。

   $arrImages[] = 
    [
        'img_sm'=>'image1.jpg', 
        'tag_GlassDoor'=>0,
        'tag_GlassWall'=>1,
    ];
   $arrImages[] = 
    [
        'img_sm'=>'image2.jpg', 
        'tag_GlassDoor'=>1,
        'tag_GlassWall'=>1,
    ];

Faltering is the answer, it can be used to filter one dimensional Arrays and multidimensional array.步履蹒跚是答案,它可以用来过滤一维 Arrays 和多维数组。 the general implementation would be something like this:一般的实现是这样的:

$arr = array(
    array(
        'image' => "data",
        'hasObject' => 1
    ),
    array(
        'image' => "data",
        'hasObject' => 0 
    ),
);
$finteredArray = array_filter($arr, function ($r) {
   return (bool) $r['hasObject'];
});

print_r($finteredArray);
// it outputs:
// Array ( [0] => Array ( [image] => data [hasObject] => 1 ) )

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM