开发者

Problem with Building Arrays in Different Coding Styles

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_开发者_如何学Goarray() 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?


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']);
}


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']);


$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) {


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.

0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜