douqian2524 2016-06-02 15:04
浏览 22

当1的键与2中的值匹配时合并2个数组

I need help figuring out what I am doing wrong and/or if there is a better way to do it, my question is two part. The first part is I am using Joomla 3.5.1 and in the template I have two fieldsets, one that allows the user to enter links to their social media accounts and the second I am attempting to make a sort if you will using a set of drop downs. This may or may not be the best so I am open to options here.

Now for the second part. I am able to bring in the both field sets into separate arrays, for simplicity array 1 and array 2. Array 1 has the links and array 2 has the order. So my question is this, how would be the best way to loop through and match everything up while removing the empties in array 1 and "none" in array 2.

One thought I did have is should array 2 not be multidimensional and let the key be the "soXYZ", but then how would the best way to match it up knowing that the lengths are different and the order as well.

Array 2 where (-1) are "None" in the drop downs

Array
(
[0] => soPhone
[1] => soContact
[2] => soFacebook
[3] => soMap
[4] => -1
[5] => -1
[6] => -1
[7] => -1
[8] => -1
[9] => -1
[10] => -1
[11] => -1
[12] => -1
[13] => -1
[14] => -1
)

Array 1

Array
(
[0] => Array
    (
        [0] => soPhone
        [1] => 555.867.5309
    )

[1] => Array
    (
        [0] => soContact
        [1] => Contact
    )

[2] => Array
    (
        [0] => soMap
        [1] => Map
    )

[3] => Array
    (
        [0] => soFacebook
        [1] => Facebook
    )

[4] => Array
    (
        [0] => soTwitter
        [1] => Twitter
    )

[5] => Array
    (
        [0] => soGoogle
        [1] => Google Plus
    )

[6] => Array
    (
        [0] => soLinkedIn
        [1] => Linked In
    )

[7] => Array
    (
        [0] => soPinterest
        [1] => Pinerest
    )

[8] => Array
    (
        [0] => soYouTube
        [1] => YouTube
    )

[9] => Array
    (
        [0] => soVimeo
        [1] => Vimeo
    )

[10] => Array
    (
        [0] => soYelp
        [1] => Yelp
    )

[11] => Array
    (
        [0] => soInstagram
        [1] => Instagram
    )

[12] => Array
    (
        [0] => soTripAdvisor
        [1] => Trip Advisor
    )

[13] => Array
    (
        [0] => soHouzz
        [1] => Houzz
    )

[14] => Array
    (
        [0] => soAngiesList
        [1] => Angies List
    )

)

Array Result

Array
(
[0] => Array
    (
        [0] => soPhone
        [1] => 555.867.5309
    )

[1] => Array
    (
        [0] => soContact
        [1] => Contact
    )

[2] => Array
    (
        [0] => soFacebook
        [1] => Facebook
    )

[3] => Array
    (
        [0] => soMap
        [1] => Map
    )
)

Any guidance is appreciated.

  • 写回答

1条回答 默认 最新

  • dotdx80642 2016-06-03 20:28
    关注

    After the power of posting I was able to eventually get my desired result, which as follows:

    I set up three arrays arrLinks (originally Array 1) , arrLinksOrder (originally Array 2) and arrLinksCombined (Array Result), then populated arrLinks with the user entered text links on the template, arrLinksOrder pulls from the drop downs.

    My original plan was to have arrLinks be multidimensional but in the end I just set the key's for each in my code.

    I then used this stackoverflow link to help combine the arrLinksOrder and arrLinks. Once everything is combined I was able to build the html and really clean up the file to something easier to manage.

    Code below:

    // Populate the contents of the text-box into an array
        $arrLinks = array( 
            "soFacebook"=> $tbFacebook,
            "soTwitter"=> $tbTwitter,
            "soGoogle"=> $tbGoogle,
            "soLinkedIn"=> $tbLinkedIn,
            "soPinterest"=> $tbPinterest,
            "soYouTube"=> $tbYouTube,
            "soInstagram"=> $tbInstagram,
        );
    
    // Populate an array (arrLinksOrder) with the drop down selections
        for ($i=1; $i < ($linksCount+1); $i++) { 
            $varName = 'listSocialOrder'.$i;
            $arrLinksOrder[$i] = $$varName;  
        }
    
    // Combine arrLinksOrder and arrLinks, if the arrLinksOrder exists in arrLinks
        foreach($arrLinksOrder as $key) {
            if(array_key_exists($key, $arrLinks)) {
                $arrLinksCombined[$key] = $arrLinks[$key];
            }
        }
    

    If anyone has any improvements please feel free to share, this is what worked for me at the time.

    评论

报告相同问题?

悬赏问题

  • ¥15 如何在scanpy上做差异基因和通路富集?
  • ¥20 关于#硬件工程#的问题,请各位专家解答!
  • ¥15 关于#matlab#的问题:期望的系统闭环传递函数为G(s)=wn^2/s^2+2¢wn+wn^2阻尼系数¢=0.707,使系统具有较小的超调量
  • ¥15 FLUENT如何实现在堆积颗粒的上表面加载高斯热源
  • ¥30 截图中的mathematics程序转换成matlab
  • ¥15 动力学代码报错,维度不匹配
  • ¥15 Power query添加列问题
  • ¥50 Kubernetes&Fission&Eleasticsearch
  • ¥15 報錯:Person is not mapped,如何解決?
  • ¥15 c++头文件不能识别CDialog