简体   繁体   English

从数组PHP中提取[cat_ID]

[英]Extract [cat_ID] from array PHP

I have the following array (just a part of the complete array) and I want to extract the value of [cat_ID]. 我有以下数组(只是完整数组的一部分),我想提取[cat_ID]的值。

Array ([1] => stdClass Object ( [term_id] => 21 [name] => z_aflyst [slug] => z_aflyst
[term_group] => 0 [term_taxonomy_id] => 23 [taxonomy] => category 
[description] => [parent] => 0 [count] => 1 [object_id] => 7 [cat_ID] => 21 
[cat_name] => z_aflyst ))

So I need to extract the 21 in this case. 所以我需要在这种情况下提取21。 However, I only want to extract the cat_ID if the cat_name equals z_aflyst . 不过,我只想提取cat_ID如果cat_name等于z_aflyst

Give that you have an array of objects: 给你一个对象数组:

Array (
[1] => stdClass Object 
( 
[term_id] => 21 
[name] => z_aflyst 
[slug] => z_aflyst
[term_group] => 0 
[term_taxonomy_id] => 23 
[taxonomy] => category 
[description] => 
[parent] => 0 
[count] => 1 
[object_id] => 7 
[cat_ID] => 21 
[cat_name] => z_aflyst )
)

foreach ($items as $item)
{
    if($item->cat_name == 'z_aflyst')
    {
        //do stuff
    }
}

Your array is in this example a little more than a simple array, is a standard object . 您的数组在此示例中只是一个简单的数组,是一个标准对象 And you can take advantage of this. 你可以利用这一点。 You can the properties of a standard object by this formula: 您可以通过以下公式获得标准对象的属性:

$object->property;

in your case the object is 在你的情况下,对象是

$object = $array[1];

or by this formula as an array 或者通过这个公式作为数组

$array[key];

in your case to get the value of cat_ID: 在你的情况下,获取cat_ID的值:

$object->cat_ID;

So your code will be something like: 所以你的代码将是这样的:

if ($object->cat_name == 'z_aflyst') {
     // do stuff with your cat_ID value
     echo $object->cat_ID;
}
// will echo 21 only of the cat_name has value 'z_aflyst'

You have an array of standard objects , which properties can be accessed using the arrow (object) operator -> . 您有一组标准对象 ,可以使用箭头(对象)运算符->访问这些属性。

You can use array_filter() to filter out unwanted elements of the array: 您可以使用array_filter()过滤掉数组中不需要的元素:

// Assuming your array is called $items
$filtered = array_filter($items, function($v) {
    return $v->cat_name === 'z_aflyst';
});

After that, you can "flatten" the array using array_map() so that it only contains the cat_ID if you wish: 之后,您可以使用array_map() “展平”数组,以便它只包含cat_ID(如果您愿意):

$filtered = array_map(function($v) {
    return $v->cat_ID;
}, $filtered);

This will leave you with a one-dimension, indexed array of cat_IDs. 这将为您提供一个一维的索引数组cat_IDs。

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

相关问题 获取stdclass对象数组cat_id值? - get stdclass object array cat_id value? cat_id 和 term_id 有什么区别? - What is the difference between cat_id and term_id? PHP 重写页面 url 时如何删除或重定向 category.php?cat_id=2 urls 到重写的 urls? - PHP When rewrite pages urls how to remove or redirect category.php?cat_id=2 urls to rewrited urls? term_id,term_taxonomy_id和cat_ID有什么区别? - What's the difference between term_id, term_taxonomy_id and cat_ID? 选择所有项目cat_id = x或parent_id = x(父ID不同的表) - Select all items Where cat_id= x or parent_id = x (Parent id different table) SQLSTATE [23000]:违反完整性约束:1048列“ cat_id”不能为空 - SQLSTATE[23000]: Integrity constraint violation: 1048 Column 'cat_id' cannot be null Codeigniter 3博客应用程序:每删除一个类别,就将该类别中所有帖子的cat_id设置为1 - Codeigniter 3 blogging application: whenever a category is deleted, set cat_id of all posts in that category to 1 数组中的类别层次结构(目录ID =>父ID) - Category hierarchy from array(cat id => parent id) 如何从PHP中的按钮数组中提取特定的ID号? - How to extract specific id number from a array of buttons in php? PHP从字符串中提取ID - PHP extract ID from a string
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM