2013-04-21 40 views
0

基本上按照标题。下面的代码很好地工作,但似乎是一个相当漫长的过程来做什么似乎是一个相当简单的任务。有没有一种更有效的方法来根据已经存在于数组中的内容来插入新的数组变量

我会看看foreach是如何工作的,但我想知道是否真正正确地解决了这个问题。

编辑:例如,我看到人们使用“{}”来连接而不是“。”。说它速度更快,但我一直无法让他们在这段代码中工作。

<?php 
// Create Array 
    $foobar = array(
     "foo" => "test1", 
     "bar" => "hello", 
     "far" => "this", 
     "boo" => "is", 
    // "foo_result" => "", // Uncomment to check that function does nothing where foo_result already available 
    "foo_link" => "http://1.media.collegehumor.cvcdn.com/82/16/162e153d618d49869783ccd475005fd5.jpg", 
    "for" => "cool" 
); 

function _insertarray($foobar) { 
// provide arrays to test against 
    $foo1 = array("test1","test2"); 
    $foo2 = array("test3","test4"); 
    $foo3 = array("test5","test6"); 
    $foo4 = array("test7","test8"); 

// End function where "foo_result" is already set 
    if (isset($foobar['foo_result'])) { 
     // Nothing to do 
    } 
    else { 

    // add the variable to the foo_result index based on values returned 
     if ((count(array_intersect($foo1, $foobar))) ? true : false) { 
      $foobar['foo_result'] = "<iframe src='" . $foobar["foo_link"] . "'>"; 
     } 
     else if ((count(array_intersect($foo2, $foobar))) ? true : false) { 
      $foobar['foo_result'] = "<a href='" . $foobar["foo_link"] . "'>"; 
     } 
     else if ((count(array_intersect($foo3, $foobar))) ? true : false) { 
      $foobar['foo_result'] = "<img src='" . $foobar["foo_link"] . "'>"; 
     } 
     else if ((count(array_intersect($foo4, $foobar))) ? true : false) { 
      $foobar['foo_result'] = "<iframe src='" . $foobar["foo_link"] . "'>"; 
     } 
     else { 
      // Nothing to do here 
     } 
    } 
    return $foobar;  
} 
    print_r(_insertarray($foobar)); 
?> 

在此先感谢

+0

只是为了澄清,你的意思是'CPU时间短'或'更短/更好的代码'有效' – Patashu 2013-04-21 04:12:24

+2

为什么你写'if(blah?true:false)'而不是'if(blah )'? – Barmar 2013-04-21 04:16:23

+0

@Patashu我的意思是减少CPU时间,并且如果它导致代码更短,那么它会带来奖励。 – user1937392 2013-04-21 04:20:43

回答

3

我不认为这将会更快CPU的时间,但它通过消除所有重复简化了代码。

$test_array = array(
    array('check' => array("test1", "test2"), 
     'format' => '<iframe src="%s">'), 
    array('check' => array("test3", "test4"), 
     'format' => '<a href="%s">'), 
    array('check' => array("test5", "test6"), 
     'format' => '<img src="%s">'), 
    array('check' => array("test7", "test8"), 
     'format' => '<iframe src="%s">')); 

foreach ($test_array as &$el) { 
    if (array_intersect($foobar, $el['check'])) { 
    $foobar['foo_result'] = sprintf($el['format'], $foobar["foo_link"]); 
    break; 
} 

array_intersect可能是瓶颈,但前提是你的数组非常大。如果是这样的话,我建议你编写一个函数arrays_have_common_element,它可以更有效地执行它(你可以将其中一个数组转换为关联数组的键(函数array_flip()将为你执行此操作),然后遍历另一个数组直到它找到一个匹配)。

+0

哦哇[sprintf](http://php.net/manual/en/function.sprintf.php)看起来很棒。去阅读它。谢谢! – user1937392 2013-04-21 04:36:28

+0

我会看看array_flip(),看看我如何去。我不确定如何结束问题,但感谢您提供快速响应和编辑建议的进一步改进。 – user1937392 2013-04-21 04:45:19

+0

问题只有在不合适,重复等情况下才会关闭。否则,您只会接受最合适的答案。 – Barmar 2013-04-21 04:51:02

相关问题