dsgffz2579 2017-01-16 20:55
浏览 77
已采纳

填充多维数组PHP

I need to fill a multidimensional array arrayFinal so that it looks like this:

$arrayFinal = array(
    array('Number' => 1, 'isEven' => false, 'isPrime' => true),
    array('Number' => 2, '' => , '' => ), ...and so on
);

Pretty simple, but the array isn't filling, it's empty somehow.

Here's the code:

<?php
$arr = array(1,2,3,4,5,6,7,8,9,10);
$arrayFinal = array();
$isEven = false;
$isPrime = false;

function check($a){

foreach($a as $el){

    if($el % 2 == 0){
        print($el.' is even ,');
        $isEven = true;
        print($isEven);
    }
    else{
        print($el." is odd ,");
        $isEven = false;
        print($isEven);
    }

    $temp = IsPrime($el);
    if ($temp==0){
        print(' not a Prime Number.<br />');
        $isPrime = true;
        print($isPrime); 
    }
    else{
        print(' a Prime Number.<br />');
        $isPrime = false;
        print($isPrime);
    }
        $arrayFinal[] = array('Number' => $el, 'isEven' => $isEven, 'isPrime' => $isPrime); 
    }
}


function IsPrime($n){
for($x=2; $x < $n; $x++){
    if($n%$x ==0){
        return 0;
    }
}
return 1;
}



check($arr);

print_r($arrayFinal);

 ?>
  • 写回答

4条回答 默认 最新

  • dou4121 2017-01-16 21:45
    关注

    Your $arrayFinal is actually getting populated. You can see this by adding your print_r($arrayFinal); statement as the last line within your function. (after the for loop).

    The problem is you have a variable scope issue here. You essentially have two different variables with the same name. Your initialization of $arrayFinal outside your function is actually a different variable from the one you are using within your function. (this is actually true for your other variables $isEven and $isPrime as well)

    You are updating the variable local to the function. But you're not doing anything with that value before the function ends.

    You could choose to return that value by adding the following as the last line in your function:

     return $arrayFinal;
    

    Then you could use it like this:

    $output = check($arr);
    print_r($output);
    

    Then you could remove the $arrayFinal = array(); from the top of your code.

    Complete code could look something like:

    $arr = array(1,2,3,4,5,6,7,8,9,10);
    
    $output = check($arr);
    print_r($output);
    
    
    function check($a){
        $isEven = false;
        $isPrime = false;
        $arrayFinal = Array();
    
        foreach($a as $el){
    
            if($el % 2 == 0){
                print($el.' is even ,');
                $isEven = true;
                print($isEven);
            }
            else{
                print($el." is odd ,");
                $isEven = false;
                print($isEven);
            }
    
            $temp = IsPrime($el);
            if ($temp==0){
                print(' not a Prime Number.<br />');
                $isPrime = true;
                print($isPrime); 
            }
            else{
                print(' a Prime Number.<br />');
                $isPrime = false;
                print($isPrime);
            }
    
            $arrayFinal[] = array('Number' => $el, 'isEven' => $isEven, 'isPrime' => $isPrime); 
        }
    
        return $arrayFinal;
    }
    
    function IsPrime($n){
        for($x=2; $x < $n; $x++){
            if($n%$x ==0){
                return 0;
            }
        }
        return 1;
    }
    
    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论
查看更多回答(3条)

报告相同问题?