多维数组,比较值 [英] multidimensional array, comparing the values

查看:69
本文介绍了多维数组,比较值的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我使用此命令将其放入树枝模板


  {{dump(elements) }} 


数组。还请记住,所有值都动态添加到模板中,这意味着我需要使用我不能使用的模板,例如:elements [ master]。



我必须查看第一个数组的值在第二个数组中的位置。 (例如:where_network在第二个数组中)并获取1,2,3,4,5的值?



请记住,我什至会影响此API响应。



我正在使用Symfony 2.8。



我想获取元素的值 1 和元素 1 [2]等。我需要它。如果元素 1 ['our_network'] 1 == 1则表示一个行为,如果它等于元素 1 ['our_netwrok'] 1 == 0,然后执行其他操作。



如果您有任何建议或建议,请告诉我。

解决方案

我是通过PHP完成的,不确定是否要通过Twig ...



使用array_key_exists来查看第一个数组中的元素是否存在于第二个数组中。和两个循环。一个foreach检查第一个数组中的每个元素,一个for循环以打印出元素值。



这是我的代码:

  $ elements = array(
array( master, Home, our_network, doesnotexist),
array( master => array( master,0,0,0,0,0),
Home => array( Home,0,0,0,0,0),
our_network => array( our_network,0,0,0,0,0))
);
echo< pre> \n;
print_r($ elements);
echo< / pre> \n;

echo< pre> \n;
foreach($ elements [0]作为$ section)
{
echo $ section\n;
if(array_key_exists($ section,$ elements [1]))
{
echo EXISTS!\n;
for($ i = 1; $ i< = 5; $ i ++)
{
echo $ i:。 $ elements [1] [$ section] [$ i]。 \n;
}
}
else
{
echo不存在!\n;
}
}
echo< / pre> \n;

一堆pre只是用来格式化测试Apache上的输出。它会为您提供以下输出:

 数组

[0] =>数组

[0] =>主
[1] =>家用
[2] => our_network
[3] => dosnotexist


[1] =>数组

[master] =>数组

[0] =>主
[1] => 0
[2] => 0
[3] => 0
[4] => 0
[5] = > 0


[Home] =>数组

[0] => Home
[1] => 0
[2] => 0
[3] => 0
[4] => 0
[5] => 0


[our_network] =>数组

[0] => our_network
[1] => 0
[2] => 0
[3] => 0
[4] => 0
[5] => 0






存在!
1:0
2:0
3:0
4:0
5:0
家庭
存在!
1:0
2:0
3:0
4:0
5:0
our_network
存在!
1:0
2:0
3:0
4:0
5:0
didnotexist
不存在!

如果您只需要访问一个区域的值,请执行以下操作:

  echo只是一个部分:。 $ elements [1] ['Home'] [3]。  \n; 


I got this inside my twig template using this command

{{ dump(elements) }}

So this response is made from two arrays from which second array is not complete because it is too large. I am going to call them first [0] and second 1 array. Also please remember that all of the values are coming dynamically into the template, which means that I need to use I can't use it like for example: elements["master"].

I have to see where values of the first array are in second array. (for example: where our_network is in second array) and to get the values for 1,2,3,4,5 ??

Please, remember that I could affect even this API response.

I am using Symfony 2.8.

I want to get the values of elements1 and elements1[2] etc.. but only where I need it. And I need it if elements1['our_network']1 == 1 then one behavoior, and if it is equal to elements1['our_netwrok']1 == 0 then other behavior.

If you have any suggestions or recommendations, let me know.

解决方案

I did it via PHP, not sure if you wanted if via Twig...

Use array_key_exists to see if the elements from first array exist in second array. And two loops. A foreach to check each element in the first array, a for loop to print out the elements values.

Here is my code:

$elements = array(
                array("master","Home","our_network","doesnotexist"),
                array("master"      => array("master",     0,0,0,0,0),
                      "Home"        => array("Home",       0,0,0,0,0),
                      "our_network" => array("our_network",0,0,0,0,0))
                );
echo "<pre>\n";
print_r($elements);
echo "</pre>\n";

echo "<pre>\n";
foreach ($elements[0] as $section)
{
    echo "$section\n";
    if (array_key_exists($section,$elements[1]))
    {
        echo "    EXISTS!\n";
        for ($i = 1; $i <= 5; $i++)
        {
            echo "        $i: " . $elements[1][$section][$i] . "\n";
        }
    }
    else
    {
        echo "    DOES NOT EXIST!\n";
    }
}
echo "</pre>\n";

The bunch of pre are just to format the output on my test Apache. It gives you this output:

Array
(
    [0] => Array
        (
            [0] => master
            [1] => Home
            [2] => our_network
            [3] => doesnotexist
        )

    [1] => Array
        (
            [master] => Array
                (
                    [0] => master
                    [1] => 0
                    [2] => 0
                    [3] => 0
                    [4] => 0
                    [5] => 0
                )

            [Home] => Array
                (
                    [0] => Home
                    [1] => 0
                    [2] => 0
                    [3] => 0
                    [4] => 0
                    [5] => 0
                )

            [our_network] => Array
                (
                    [0] => our_network
                    [1] => 0
                    [2] => 0
                    [3] => 0
                    [4] => 0
                    [5] => 0
                )

        )

)
master
    EXISTS!
        1: 0
        2: 0
        3: 0
        4: 0
        5: 0
Home
    EXISTS!
        1: 0
        2: 0
        3: 0
        4: 0
        5: 0
our_network
    EXISTS!
        1: 0
        2: 0
        3: 0
        4: 0
        5: 0
doesnotexist
    DOES NOT EXIST!

If you need to access the value for just one section, do this:

echo "just one section: " . $elements[1]['Home'][3] . "\n";

这篇关于多维数组,比较值的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆