Ofelia Muradova
Ofelia Muradova

Reputation: 157

Filling a multidimensional array 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);

 ?>

Upvotes: 1

Views: 98

Answers (4)

user7399674
user7399674

Reputation:

Copy paste this 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);
    }
    return $arrayFinal; // I added this line
}
function IsPrime($n) {
    for ($x = 2; $x < $n; $x++) {
        if ($n % $x == 0) {
            return 0;
        }
    }
    return 1;
}
check($arr);
print_r($arrayFinal);
?>

Your everything is fine. You just need to return what have you manipulated.

Upvotes: 1

lazyCoder
lazyCoder

Reputation: 2561

hey @Shiroi Okami you have a scope problem because in php you can not use global variable or an array inside the function or vice versa in your case you declared $arrayFinal out side the function and $arrayFinal inside the function which are treated as different array if you want to use $arrayFinal which is inside the function so just declare it with global inside the function like below:

global $arrayFinal;

global allow us to use the variable or an array to outside of the function

Try below code it will resolve your problem

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

function check($a){
$arr1 = 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);
    }
        global $arrayFinal;
        $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);

 ?>

Upvotes: 1

BizzyBob
BizzyBob

Reputation: 14740

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;
}

Upvotes: 1

jens
jens

Reputation: 111

Your function check($a) has no "return";

Upvotes: 1

Related Questions