以不同编码风格构建数组的问题

发布于 2024-11-17 09:44:14 字数 2835 浏览 2 评论 0原文

我在构建数组时遇到一个奇怪的问题。我从一个如下所示的数组开始:

Array (
       [0] => Array (
                      [tag_id] => 19
                      [tag_translation_id] => 12
                      [fk_language_id] => 1
                      [fk_tag_id] => 19
                      [tag_name] => test
                    )
       [1] => Array (
                      [tag_id] => 20
                      [tag_translation_id] => 14
                      [fk_language_id] => 1
                      [fk_tag_id] => 20
                      [tag_name] => testa
                    )
       [2] => Array (
                      [tag_id] => 20
                      [tag_translation_id] => 15
                      [fk_language_id] => 3
                      [fk_tag_id] => 20
                      [tag_name] => fdfda
                    )
       )

我想要做的是将具有相同 tag_id 的每个结果合并到一个数组中。这是有效的:

    $tags = array();
    foreach($results->as_array() as $key=>$result)
    {
        if(!in_array($result['tag_id'], $tags))
        {
            $tags[$result['tag_id']] = array();
        }   
    }

    foreach($results->as_array() as $result)
    {
        array_push($tags[$result['tag_id']], array($result['fk_language_id'] , $result['tag_name']));   
    }

这是预期的结果:

Array ( 
  [19] => Array ( 
       [0] => Array ( 
               [0] => 1 
               [1] => test 
           ) 
       ) 
  [20] => Array ( 
       [0] => Array ( 
               [0] => 1 
               [1] => testa 
           ) 
       [1] => Array (  
               [0] => 3 
               [1] => fdfda 
           ) 
       ) 
) 

但是,我这里有两个循环,我知道这并不理想。为什么这些不起作用?

    $tags = array();
    foreach($results->as_array() as $key=>$result)
    {
        $tags[$result['tag_id']] .= array($result['fk_language_id'] , $result['tag_name']);
    }

在这个示例中,我得到两个空数组...

Array ( [19] => Array [20] => ArrayArray ) 

甚至...

    $tags = array();
    foreach($results->as_array() as $key=>$result)
    {
        if(!in_array($result['tag_id'], $tags))
        {
            $tags[$result['tag_id']] = array();
        }
        array_push($tags[$result['tag_id']], array($result['fk_language_id'] , $result['tag_name']));
    }

由于某种原因,它会用第二个数组的第二个值覆盖第二个数组的第一个值。

Array ( 
  [19] => Array ( 
       [0] => Array ( 
               [0] => 1 
               [1] => test 
           ) 
       ) 
  [20] => Array ( 
      [0] => Array ( 
               [0] => 3 
               [1] => fdfda 
           ) 
       ) 
 ) 

在后两个例子中我做错了什么?

I'm having a strange problem while building arrays. I start off with an array that looks like this:

Array (
       [0] => Array (
                      [tag_id] => 19
                      [tag_translation_id] => 12
                      [fk_language_id] => 1
                      [fk_tag_id] => 19
                      [tag_name] => test
                    )
       [1] => Array (
                      [tag_id] => 20
                      [tag_translation_id] => 14
                      [fk_language_id] => 1
                      [fk_tag_id] => 20
                      [tag_name] => testa
                    )
       [2] => Array (
                      [tag_id] => 20
                      [tag_translation_id] => 15
                      [fk_language_id] => 3
                      [fk_tag_id] => 20
                      [tag_name] => fdfda
                    )
       )

What I want to do is merge each result with the same tag_id into a single array. This works:

    $tags = array();
    foreach($results->as_array() as $key=>$result)
    {
        if(!in_array($result['tag_id'], $tags))
        {
            $tags[$result['tag_id']] = array();
        }   
    }

    foreach($results->as_array() as $result)
    {
        array_push($tags[$result['tag_id']], array($result['fk_language_id'] , $result['tag_name']));   
    }

Here is the intended result:

Array ( 
  [19] => Array ( 
       [0] => Array ( 
               [0] => 1 
               [1] => test 
           ) 
       ) 
  [20] => Array ( 
       [0] => Array ( 
               [0] => 1 
               [1] => testa 
           ) 
       [1] => Array (  
               [0] => 3 
               [1] => fdfda 
           ) 
       ) 
) 

However, I've got two loops here, and I know this isn't ideal. Why do THESE not work??

    $tags = array();
    foreach($results->as_array() as $key=>$result)
    {
        $tags[$result['tag_id']] .= array($result['fk_language_id'] , $result['tag_name']);
    }

With this example I get two empty arrays...

Array ( [19] => Array [20] => ArrayArray ) 

Or even...

    $tags = array();
    foreach($results->as_array() as $key=>$result)
    {
        if(!in_array($result['tag_id'], $tags))
        {
            $tags[$result['tag_id']] = array();
        }
        array_push($tags[$result['tag_id']], array($result['fk_language_id'] , $result['tag_name']));
    }

Which for some reason overwrites the first value of the second array with the second value of the second array.

Array ( 
  [19] => Array ( 
       [0] => Array ( 
               [0] => 1 
               [1] => test 
           ) 
       ) 
  [20] => Array ( 
      [0] => Array ( 
               [0] => 3 
               [1] => fdfda 
           ) 
       ) 
 ) 

What am I doing wrong in the second 2 examples?

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

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

发布评论

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

评论(4

夜清冷一曲。 2024-11-24 09:44:15

要回答您的问题,您的第二种方法会失败,因为您使用了不正确的 .= 运算符。您的第三种方法失败,因为您的 !in_array 检查始终为 false (它检查 是否在数组中,而不是 key 已设置)并在每次迭代时覆盖数组。你只真正需要这个(正如其他人在伪代码中提到的):

$result = array();
foreach ($array as $values) {
    $result[$values['key']][] = array($values['foo'], $values['bar']);
}

To answer your question, your second method fails because you're using the incorrect .= operator. Your third method fails because your !in_array check is always false (it checks whether the value is in the array, not whether the key is set) and overwrites the array each iteration. You only really need this (as mentioned by others, in pseudo-code):

$result = array();
foreach ($array as $values) {
    $result[$values['key']][] = array($values['foo'], $values['bar']);
}
天暗了我发光 2024-11-24 09:44:15

.= 运算符是字符串连接。数组用+=合并。

如果我正确理解这个问题,代码应该是这样的:

$tags = array();
foreach ($results as $result)
    $tags[$result['tag_id']][] = array($result['fk_language_id'], $result['tag_name']);

The .= operator is string concatenation. Arrays are merged with +=.

If I understand the issue correctly, the code should go like this:

$tags = array();
foreach ($results as $result)
    $tags[$result['tag_id']][] = array($result['fk_language_id'], $result['tag_name']);
等待我真够勒 2024-11-24 09:44:15
$tags = array();
foreach($results->as_array() as $key=>$result)
{
    $tags[$result['tag_id']] .= array($result['fk_language_id'] , $result['tag_name']);
}

您不能使用 .=(点等于)运算符将值添加到数组中。

你为什么要做 $results->as_array() ???

简单地做:

foreach($results as $key=>$result) {
$tags = array();
foreach($results->as_array() as $key=>$result)
{
    $tags[$result['tag_id']] .= array($result['fk_language_id'] , $result['tag_name']);
}

you cannot add a value to an array with the .= (dot equal) operator.

why are you doing $results->as_array() ????

do simply:

foreach($results as $key=>$result) {
感性不性感 2024-11-24 09:44:15

不要使用 .= 尝试使用 []

$tags = array();
foreach ($results as $result)
{
    if(!isset($tags[$result['tag_id']]))
        $tags[$result['tag_id']] = array();
    $tags[$result['tag_id']][] = array($result['fk_language_id'], result['tag_name']);
}

.= 来连接字符串
+= 是连接一个数字
[] 是连接到一个数组

希望这有帮助吗?

编辑:注意到如果 tag_id 不存在于数组中,它“可能”会失败,因此可能值得首先检查并将其设置为数组以防万一。

Instead of using .= try using []

$tags = array();
foreach ($results as $result)
{
    if(!isset($tags[$result['tag_id']]))
        $tags[$result['tag_id']] = array();
    $tags[$result['tag_id']][] = array($result['fk_language_id'], result['tag_name']);
}

.= is to concatinate a string
+= is to concatinate a number
[] is to concatinate to an array

Hope this helps?

Edit: Noticed that it "might" fail if the tag_id doesn't already exist in the array, so it might be worth just checking first and setting it to an array just in case.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文