从 2d 数组中每个值的第一个成员创建 1d 数组 | PHP

发布于 2024-07-30 06:09:02 字数 123 浏览 7 评论 0原文

你怎么能这样做呢? 我在这里看到的代码不起作用

for($i=0;i<count($cond);$i++){
    $cond[$i] = $cond[$i][0];
}

How can you do this? My code seen here doesn't work

for($i=0;i<count($cond);$i++){
    $cond[$i] = $cond[$i][0];
}

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(5

濫情▎り 2024-08-06 06:09:02

它可以像这样简单:

$array = array_map('reset', $array);

It can be as simple as this:

$array = array_map('reset', $array);
旧时模样 2024-08-06 06:09:02
// Make sure you have your first array initialised here!
$array2 = array();
foreach ($array AS $item)
{
    $array2[] = $item[0];
}

假设您之后希望具有相同的变量名称,则可以将新数组重新分配回旧数组。

$array = $array2;
unset($array2); // Not needed, but helps with keeping memory down

另外,您也许可以根据数组中的内容执行类似的操作。

$array = array_merge(array_values($array));
// Make sure you have your first array initialised here!
$array2 = array();
foreach ($array AS $item)
{
    $array2[] = $item[0];
}

Assuming you want to have the same variable name afterwards, you can re-assign the new array back to the old one.

$array = $array2;
unset($array2); // Not needed, but helps with keeping memory down

Also, you might be able to, dependant on what is in the array, do something like.

$array = array_merge(array_values($array));
对你的占有欲 2024-08-06 06:09:02

如果源数组不是数字索引,则可能会出现问题。 试试这个:

$destinationArray = array();
for ($sourceArray as $key=>$value) {
    $destinationArray[] = $value[0]; //you may want to use a different index than '0'
}

There could be problems if the source array isn't numerically index. Try this instead:

$destinationArray = array();
for ($sourceArray as $key=>$value) {
    $destinationArray[] = $value[0]; //you may want to use a different index than '0'
}
虐人心 2024-08-06 06:09:02

如前所述,您的代码在各种情况下都无法正常工作。
尝试用这个值初始化你的数组:

$cond = array(5=>array('4','3'),9=>array('3','4'));

一个解决方案,对我来说更好可读的是以下代码:

//explain what to do to every single line of the 2d array
function reduceRowToFirstItem($x) { return $x[0]; }

// apply the trasnformation to the array
$a=array_map('reduceRowTofirstItem',$cond);

你可以阅读 数组映射参考以获得完整的解释。

您还可以选择使用 array_walk (它“就地”对数组进行操作)。 请注意,该函数不返回值,并且其参数是通过引用传递的。

function reduceToFirstItem(&$x) { $x=$x[0]; }
array_walk($cond, 'reduceToFirstItem');

As previously stated, your code will not work properly in various situation.
Try to initialize your array with this values:

$cond = array(5=>array('4','3'),9=>array('3','4'));

A solution, to me better readable also is the following code:

//explain what to do to every single line of the 2d array
function reduceRowToFirstItem($x) { return $x[0]; }

// apply the trasnformation to the array
$a=array_map('reduceRowTofirstItem',$cond);

You can read the reference for array map for a thorough explanation.

You can opt also for a slight variation using array_walk (it operate on the array "in place"). Note that the function doesn't return a value and that his parameter is passed by reference.

function reduceToFirstItem(&$x) { $x=$x[0]; }
array_walk($cond, 'reduceToFirstItem');
撩心不撩汉 2024-08-06 06:09:02

那应该有效。 为什么它不起作用? 你收到什么错误消息?
这是我要使用的代码:

$inArr;//This is the 2D array
$outArr = array();
for($i=0;$i<count($inArr);$i++){
        $outArr[$i] = $inArr[$i][0];
}

That should work. Why does it not work? what error message do you get?
This is the code I would use:

$inArr;//This is the 2D array
$outArr = array();
for($i=0;$i<count($inArr);$i++){
        $outArr[$i] = $inArr[$i][0];
}
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文