如何从3D数组内部数据列表PHP
下面是数组的代码。我试图在内部数组中列出字段类别,当字段为空时,写“无类别”。我就是做不到。我一直在尝试使用两个foreach嵌套将列表保存到一个新数组中,但我不能完全正确地完成它如何从3D数组内部数据列表PHP,php,arrays,Php,Arrays,下面是数组的代码。我试图在内部数组中列出字段类别,当字段为空时,写“无类别”。我就是做不到。我一直在尝试使用两个foreach嵌套将列表保存到一个新数组中,但我不能完全正确地完成它 Array( 'type' => 'success', 'value => array ( 0 => array ( 'id' => 1, 'joke' => 'Chuck Norris uses ribbed condoms
Array(
'type' => 'success',
'value => array (
0 => array (
'id' => 1,
'joke' => 'Chuck Norris uses ribbed condoms inside out, so he gets the pleasure.',
'categories' => array());
1 => array (
'id' => 2,
'joke' => 'MacGyver can build an airplane out of gum and paper clips. Chuck Norris can kill him and take it.',
'categories' => array(
[0] => nerdy
));
2 => array (
'id' => 3,
'joke' => 'MacGyver can build an airplane out of gum and paper clips. Chuck Norris can kill him and take it.',
'categories' => array(
[0] => explicit
));
);
)
//这就是我在没有运气的情况下尝试的
$output = array();
foreach($response as $row){
foreach($row as $cat){
$output[] = $cat['categories'];
}
}
谢谢 首先,数组中有错误的语法,分号代替逗号,方括号中的数组键。下面是正确的语法
$response = Array(
'type' => 'success',
'value' => array (
0 => array ('id' => 1, 'joke' => 'Chuck Norris uses ribbed condoms inside out, so he gets the pleasure.', 'categories' => array()),
1 => array ('id' => 2, 'joke' => 'MacGyver can build an airplane out of gum and paper clips. Chuck Norris can kill him and take it.',
'categories' => array(0 => 'nerdy')),
2 => array (
'id' => 3,
'joke' => 'MacGyver can build an airplane out of gum and paper clips. Chuck Norris can kill him and take it.',
'categories' => array(
0 => 'explicit'))));
现在是输出。您想要一个输出,其中“无类别”字段将是字符串,对吗?因此,结果数组的print\r
如下所示
Array
(
[0] => without category
[1] => Array
(
[0] => nerdy
)
[2] => Array
(
[0] => explicit
)
)
如果是,以下是展开阵列的方式
foreach($response as $row) {
if (is_array($row)) {
foreach($row as $cat) {
if (empty($cat['categories'])) {
$output[] = 'without category';
} else {
$output[] = $cat['categories'];
}
}
}
}
首先,数组中有错误的语法,分号代替逗号,方括号中的数组键。下面是正确的语法
$response = Array(
'type' => 'success',
'value' => array (
0 => array ('id' => 1, 'joke' => 'Chuck Norris uses ribbed condoms inside out, so he gets the pleasure.', 'categories' => array()),
1 => array ('id' => 2, 'joke' => 'MacGyver can build an airplane out of gum and paper clips. Chuck Norris can kill him and take it.',
'categories' => array(0 => 'nerdy')),
2 => array (
'id' => 3,
'joke' => 'MacGyver can build an airplane out of gum and paper clips. Chuck Norris can kill him and take it.',
'categories' => array(
0 => 'explicit'))));
现在是输出。您想要一个输出,其中“无类别”字段将是字符串,对吗?因此,结果数组的print\r
如下所示
Array
(
[0] => without category
[1] => Array
(
[0] => nerdy
)
[2] => Array
(
[0] => explicit
)
)
如果是,以下是展开阵列的方式
foreach($response as $row) {
if (is_array($row)) {
foreach($row as $cat) {
if (empty($cat['categories'])) {
$output[] = 'without category';
} else {
$output[] = $cat['categories'];
}
}
}
}
如果它是空的,你想做什么?在
$output
中添加“无类别”或回显此文本?是的,但我只想添加“无类别”和所有其他类别一次…如果其为空,您想做什么?在$output
中添加“无类别”或回显此文本?是的,但我只想添加“无类别”和所有其他类别一次…我明白了,它与我要查找的内容类似,但并不精确。我正在寻找每个类别的列表,不重复它们,并且在相同的数组级别中,这样我就可以使用它们作为菜单。我可能没有解释我自己的权利,但感谢很多!我明白了,它和我要找的东西很相似,但并不精确。我正在寻找每个类别的列表,不重复它们,并且在相同的数组级别中,这样我就可以使用它们作为菜单。我可能没有解释我自己的权利,但感谢很多!