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?
 
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
 
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
Question has a verified solution.

Are you are experiencing a similar issue? Get a personalized answer when you ask a related question.

Have a better answer? Share it in a comment.

All Courses

From novice to tech pro — start learning today.