2013-04-21 45 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

相關問題