In a while loop I have:
在一个while循环中,我有:
$row = mysql_fetch_array($result)
Held under $row
are two arrays which when shown using print_r
display the following:
在$ row下保持的是两个数组,当使用print_r显示时,显示如下:
Array
(
[4] => Post Content
[value] => Post Content
)
Array
(
[4] => Post Title
[value] => Post Title
)
How can I choose "Post Content" and "Post Title" from the array without it being repeated in the while loop?
如何从数组中选择“Post Content”和“Post Title”而不在while循环中重复?
The original version of this question confused the duplication with the problem. The issue is how to extract the second array [value]
when they are both held under $row
.
这个问题的原始版本混淆了问题的重复。问题是如何在$ row下保存第二个数组[value]。
4 个解决方案
#1
You can also make sure the values won't be insterted twice using mysql_fetch_assoc() or mysql_fetch_row().
Thus:
您还可以使用mysql_fetch_assoc()或mysql_fetch_row()确保值不会被插入两次。从而:
$row = mysql_fetch_assoc($result);
Array
(
['value'] => Post Content
)
Array
(
['value'] => Post Title
)
#2
You want to make sure an array only has unique values? If that doesn't work with the arrays in an array, it's classic de-duping with a store of what you've already seen.
您想确保数组只有唯一值吗?如果这不适用于数组中的数组,那么它就是经典的重复数据删除,并且存储了您已经看过的内容。
$dedupedValues = array()
foreach ($row as $items) {
if (! is_set($dedupedValues[$items['value']])) {
$dedupedValues[$items['value']] = true; // we have this value
echo "echo the title/value....";
}
}
Another alternative is to improve the SQL that gets the data to begin with to avoid duplicates in the first place (see: MySQL 'DISTINCT')
另一种方法是改进首先获取数据的SQL,以避免重复(参见:MySQL'DISTINCT')
#3
Pim Jager showed much better way of doing this, but if you insist on using mysql_fetch_array():
Pim Jager显示了更好的方法,但是如果你坚持使用mysql_fetch_array():
$i = 0;
$size = count($row);
if ($size > 0) {
while ($i < floor($size / 2)) {
..... = $row[$i];
$i += 1;
}
}
Edit:
After reading again, I'm not sure if I quite understand the question.
再看完之后,我不确定我是否完全明白这个问题。
If you have nested arrays for example:
如果您有嵌套数组,例如:
$row = array('data1' => array('orange', 'banana', 'apple'),
'data2' => array('carrot', 'collard', 'pea'));
And you want to access each array then:
然后你想访问每个数组:
$data1 = $row['data1'];
// = array('orange', 'banana', 'apple')
$data2 = $row['data2'];
// = array('carrot', 'collard', 'pea')
// OR
$orange = $row['data1'][0];
$banana = $row['data1'][1];
$apple = $row['data1'][2];
$carrot = $row['data2'][0];
// ... so on..
Taking that in account the loop looks like this:
考虑到这一点,循环看起来像这样:
$i = 0;
$size = count($row);
if ($size > 0) {
while ($i < floor($size / 2)) {
$something = $row['data1'][$i]; //Or $row[0][$i]; using your example
$something = $row['data2'][$i]; //Or $row[1][$i]; using your example
$i += 1;
}
}
But I really suggest using mysql_fetch_assoc() and foreach loop, like in Pims Jagers example
但我真的建议使用mysql_fetch_assoc()和foreach循环,就像在Pims Jagers示例中一样
#4
Final solution was found by slightly restructuring the original query as per Topbit's suggestion and the use of two while loops.
根据Topbit的建议和使用两个while循环,通过略微重构原始查询找到最终解决方案。
while($row_title = mysql_fetch_array($result_title)){
$row_body = mysql_fetch_array($result_body);
// code
}
Thank you to all those who suggested solutions.
感谢所有建议解决方案的人。
#1
You can also make sure the values won't be insterted twice using mysql_fetch_assoc() or mysql_fetch_row().
Thus:
您还可以使用mysql_fetch_assoc()或mysql_fetch_row()确保值不会被插入两次。从而:
$row = mysql_fetch_assoc($result);
Array
(
['value'] => Post Content
)
Array
(
['value'] => Post Title
)
#2
You want to make sure an array only has unique values? If that doesn't work with the arrays in an array, it's classic de-duping with a store of what you've already seen.
您想确保数组只有唯一值吗?如果这不适用于数组中的数组,那么它就是经典的重复数据删除,并且存储了您已经看过的内容。
$dedupedValues = array()
foreach ($row as $items) {
if (! is_set($dedupedValues[$items['value']])) {
$dedupedValues[$items['value']] = true; // we have this value
echo "echo the title/value....";
}
}
Another alternative is to improve the SQL that gets the data to begin with to avoid duplicates in the first place (see: MySQL 'DISTINCT')
另一种方法是改进首先获取数据的SQL,以避免重复(参见:MySQL'DISTINCT')
#3
Pim Jager showed much better way of doing this, but if you insist on using mysql_fetch_array():
Pim Jager显示了更好的方法,但是如果你坚持使用mysql_fetch_array():
$i = 0;
$size = count($row);
if ($size > 0) {
while ($i < floor($size / 2)) {
..... = $row[$i];
$i += 1;
}
}
Edit:
After reading again, I'm not sure if I quite understand the question.
再看完之后,我不确定我是否完全明白这个问题。
If you have nested arrays for example:
如果您有嵌套数组,例如:
$row = array('data1' => array('orange', 'banana', 'apple'),
'data2' => array('carrot', 'collard', 'pea'));
And you want to access each array then:
然后你想访问每个数组:
$data1 = $row['data1'];
// = array('orange', 'banana', 'apple')
$data2 = $row['data2'];
// = array('carrot', 'collard', 'pea')
// OR
$orange = $row['data1'][0];
$banana = $row['data1'][1];
$apple = $row['data1'][2];
$carrot = $row['data2'][0];
// ... so on..
Taking that in account the loop looks like this:
考虑到这一点,循环看起来像这样:
$i = 0;
$size = count($row);
if ($size > 0) {
while ($i < floor($size / 2)) {
$something = $row['data1'][$i]; //Or $row[0][$i]; using your example
$something = $row['data2'][$i]; //Or $row[1][$i]; using your example
$i += 1;
}
}
But I really suggest using mysql_fetch_assoc() and foreach loop, like in Pims Jagers example
但我真的建议使用mysql_fetch_assoc()和foreach循环,就像在Pims Jagers示例中一样
#4
Final solution was found by slightly restructuring the original query as per Topbit's suggestion and the use of two while loops.
根据Topbit的建议和使用两个while循环,通过略微重构原始查询找到最终解决方案。
while($row_title = mysql_fetch_array($result_title)){
$row_body = mysql_fetch_array($result_body);
// code
}
Thank you to all those who suggested solutions.
感谢所有建议解决方案的人。