简体   繁体   English

从多维数组中提取值

[英]pull value from multidimensional array

i have 2 arrays as below我有 2 个 arrays 如下

data1[]=array ( 0 => 0, 1 => array ( 0 => 4, 1 => 8, ), 2 => 0, 3 => array ( 0 => 2, 1 => 6, 2 => 10, ), 4 => array ( 0 => 1, 1 => 5, ), 5 => array ( 0 => 3, 1 => 7, 2 => 11, ), )

data2[]=array ( 0 => 0, 1 => array ( 0 => 1, 1 => 5, ), 2 => array ( 0 => 4, 1 => 8, ), 3 => 0,  )

i am trying to compare each element of array one with each of array 2 and ahow it on screen我正在尝试将数组 1 的每个元素与数组 2 中的每个元素进行比较,以及它在屏幕上的表现

my code is我的代码是

for($i=0;$i<9;$i++){

   if( is_array($data1[$i]) == "1")
   { $sourcecount=count($data1[$i],1); }

   else{$sourcecount=1; }

       //echo $sourcecount."<br>";
     for($j=0;$j < $sourcecount;$j++){

      if( is_array($data2[$j]) == "1")
       { $endcount=count($data2[$j],1); }

       else
       {$endcount=1; }
       //echo $endcount."<br>";

         for($k=0;$k<9;$k++){
             for($m=0;$m<$endcount;$m++){
                if( is_array($data1[$i]) == "1")
                { $source=$data1[$i][$j]; }

                else
                {$source=$data1[$j]; }


                if( is_array($data2[$k]) == "1")
                { $end=$data2[$k][$m]; }

                else
                {$end=$data2[$m]; }


                echo $i.$j." ".$k.$m." - ". $source." ".$end."<br>";

             }
         }
     }
 }

but i now getting the output as但我现在得到 output 作为

00 00 - 0 0 
00 10 - 0 1 
00 20 - 0 4 
00 30 - 0 0

its missing the它缺少

00 11 - 0 5 00 21 - 0 8

and i get error我得到错误

Array to string conversion数组到字符串的转换

on

echo $i.$j." ".$k.$m." - ".回声 $i.$j." ".$k.$m." - ". $source." ".$end." $source." ".$end."
"; ";

at times有时

i am unable to figure out y its coming so...as the values are not getting printed properly or i get the error我无法弄清楚它的到来,所以......因为值没有正确打印或者我收到错误

Here's what you could do.这是你可以做的。 Use a recursive function .使用递归 function I created an alternative of your function where you can have infinite depth.我创建了您的 function 的替代方案,您可以在其中拥有无限深度。 Even if $a[3] is an array and $b[3] is an int, it will validate the int with each value of $a[3] array.即使$a[3]是一个数组并且$b[3]是一个 int,它也会使用$a[3]数组的每个值来验证 int。 If you wish to disable that, you could only change the OR to an AND where you check if $a_data and $b_data are array.如果您想禁用它,您只能将 OR 更改为 AND,您可以在其中检查$a_data$b_data是否为数组。

Here I created another function array_depth to check how deep is the array.这里我创建了另一个 function array_depth来检查数组的深度。 I then pad, by adding 0, on the result at the end of compare_rec .然后我通过在compare_rec末尾的结果上添加 0 来填充。

<?php

$a = array (
    0 => 0, 
    1 => array(
        0 => 4, 
        1 => 8,
    ), 
    2 => 0, 
    3 => array(
        0 => 2, 
        1 => 6, 
        2 => 10, 
    ), 
    4 => array(
        0 => 1, 
        1 => 5,
    ), 
    5 => array(
        0 => 3, 
        1 => 7, 
        2 => 11, 
    ), 
);

$b = array(
    0 => 0, 
    1 => array(
        0 => 1, 
        1 => 5,
    ), 
    2 => array(
        0 => 4, 
        1 => 8, 
    ), 
    3 => 0,
);

$max_depth = max(array_depth($a), array_depth($b));
echo compare_rec($a, $b, $max_depth);

function array_depth(array $array) 
{
    $max_depth = 1;

    foreach ($array as $value) 
    {
        if (is_array($value)) 
        {
            $depth = array_depth($value) + 1;

            if ($depth > $max_depth) 
            {
                $max_depth = $depth;
            }
        }
    }

    return $max_depth;
}

function compare_rec($a, $b, $max_depth, string $resp = "")
{
    $result = "";
    $longest_array = max(is_array($a) ? count($a) : 1, is_array($b) ? count($b) : 1);

    for($i = 0; $i < $longest_array; $i++)
    {
        $a_data = (is_array($a) ? (isset($a[$i]) ? $a[$i] : 0) : $a);
        $b_data = (is_array($b) ? (isset($b[$i]) ? $b[$i] : 0) : $b);

        // If either of $a_data or $b_data are array, rec the function
        if (is_array($a_data) || is_array($b_data))
        {
            $result .= compare_rec($a_data, $b_data, $max_depth, $resp . $i);
        }
        else if (is_numeric($a_data) && is_numeric($b_data))
        {
            $result .= "00 " . str_pad($resp . $i, $max_depth, "0") . " - " . $a_data . " " . $b_data . "\n";
        }
    }

    return $result;
}

If there's something else, just ask.如果还有什么,就问吧。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM