Empty array of values

Im trying to check an array structure to make sure it is in the correct format before processing. Ive had a google and cannot find a function, so throught Id try and do it myself.

So my plan is to remove all the values from the array, then using array_diff compare it with the correct structure, if anything is returned then its in the wrong structure, if empty then its correct and Ill process it.

I dont want to use in_array to check if the keys are in as there might be more.

Soo I started with the following:-
<?php
class checkArrayStructure {
    function blankArray($tmpArr) {
        $arr = $tmpArr;
        foreach ($arr as &$key=>$val) {
            if (is_array($val)) {
                $val = $this->blankArray($val);
            } else {
                $val = null;
            }
        }
        return $arr;
    }
    function check($testArray,$correctFormat) {
        $tmpTest = $this->blankArray($testArray);
        $tmpCorrect = $this->blankArray($correctFormat);
    }
}

$tt = new checkArrayStructure();

print_r($tt->blankArray(array("username"=>"tcross","password"=>"myPassword")));
?>

Open in new window


But then relaised you cant pass array items by reference in PHP, so Im a bit stuck now how I can remove all values from an array to leave only the keys and then compare.

As you can see the class isnt much at the moment, but will be extended on going forward with other functions to do this, its only a skeleton to test.

Thank you
tonelm54Asked:
Who is Participating?

[Product update] Infrastructure Analysis Tool is now available with Business Accounts.Learn More

x
I wear a lot of hats...

"The solutions and answers provided on Experts Exchange have been extremely helpful to me over the last few years. I wear a lot of hats - Developer, Database Administrator, Help Desk, etc., so I know a lot of things but not a lot about one thing. Experts Exchange gives me answers from people who do know a lot about one thing, in a easy to use platform." -Todd S.

Julian HansenCommented:
you cant pass array items by reference in PHP
Yes you can.
foreach ($arr as &$key=>&$val) {
  ...
  $val = null; // Changes the value in the source array
}

Open in new window

$val is now a reference to the value.
0
Julian HansenCommented:
Having said that I am not sure what you are wanting to do though. I don't follow the logic of your blank array.

If you want to flatten your array you can do it like this

<?php

$a = array(1,2,array(3,4, array(5,6,7), 8), 9);
$it = new RecursiveIteratorIterator(new RecursiveArrayIterator($a));
$n = array();
foreach($it as $v) {
  $n[] = $v;
}

echo "<pre>" . print_r($n, true) . "</pre>";

Open in new window

Result
Array
(
    [0] => 1
    [1] => 2
    [2] => 3
    [3] => 4
    [4] => 5
    [5] => 6
    [6] => 7
    [7] => 8
    [8] => 9
)

Open in new window

If you want to flatten on keys then
<?php
$a = array('field1' => 'somevalue1','field2' => 2,'field3-array' => array('sub-field1' => 3,'sub-field2' => 4, 'sub-field3-array' => array('sub-sub-field1' => 5,'sub-sub-field2' => 6,'sub-sub-field3' => 7), 'sub-field4' => 8), 'field4' => 9);
$it = new RecursiveIteratorIterator(new RecursiveArrayIterator($a));
$n = array();
foreach($it as $k => $v) {
  $n[] = $k;
}

echo "<pre>" . print_r($n, true) . "</pre>";

Open in new window

Result
Array
(
    [0] => field1
    [1] => field2
    [2] => sub-field1
    [3] => sub-field2
    [4] => sub-sub-field1
    [5] => sub-sub-field2
    [6] => sub-sub-field3
    [7] => sub-field4
    [8] => field4
)

Open in new window

0

Experts Exchange Solution brought to you by

Your issues matter to us.

Facing a tech roadblock? Get the help and guidance you need from experienced professionals who care. Ask your question anytime, anywhere, with no hassle.

Start your 7-day free trial
It's more than this solution.Get answers and train to solve all your tech problems - anytime, anywhere.Try it for free Edge Out The Competitionfor your dream job with proven skills and certifications.Get started today Stand Outas the employee with proven skills.Start learning today for free Move Your Career Forwardwith certification training in the latest technologies.Start your trial today
PHP

From novice to tech pro — start learning today.