Mukund Mukund - 4 months ago 14
PHP Question

how to replace or eliminate @xsi::nill => true in php multi dimensional arrays

I have a php array like this

print_r(myarray) is

Array ( [0] => Array ( [@xsi:nil] => true ) [1] => Array ( [@xsi:nil] => true ) [2] => Array ( [@xsi:nil] => true ) [3] => 'some value' [4] => Array ( [@xsi:nil] => true ))


I need to eliminate the values
Array ( [@xsi:nil] => true )
or just to replace them with say
"nill"
. I tried a lot, this being a nested array i couldnt get the key for the values
[@xsi:nil] => true


How to check in php for the indexes which hold the value
Array ( [@xsi:nil] => true )
? and replace them with say 'nill'?

trial one :

$key1 = array_search(array('@xsi:nil'=>'true'), array_column($arrays, 'NOTE')); //to catch the indexes.

function searchMyCoolArray($arrays, $key, $search) {
$count = 0;

foreach($arrays as $object) {
if(is_object($object)) {
$ob1 = $object;
$object = get_object_vars($object);

$key1 = array_search(40489, array_column($arrays, 'uid'));
}

if(array_key_exists($key, $object) && $object[$key] == $search)
{
// print_r($first_names_note[$key]);
// echo "ffgfg ".$ob1[0]." rtrtrt";
// var_dump($object);
// print_r($arrays[$key]);
// echo $object;
// print_r($object);
// print_r($first_names_note)."<br>";
$count++;

//echo "sddsdsdsd";

}
}

return $count;
}

echo searchMyCoolArray($first_names_note, '@xsi:nil', 'true');

here i got the count correct, but it was not i need, I tried to get the indexs in the function itself, but failed


Please help, i googled alot pleeeeeeeeeeeeeeeeez

Answer

You may try to use array_walk to traverse the array and then unset all elements with the key @xsi:nil like this:

    <?php

        $arr   = array(
            array("@xsi:nil" => true),
            array("@xsi:nil" => true),
            array("@xsi:nil" => true),
            array("some_value" =>4),
            array("@xsi:nil" => true),
        );

        array_walk($arr, function(&$data){
            if(is_array($data) && array_key_exists("@xsi:nil", $data)){
                unset($data["@xsi:nil"]);
                $data[] = "nil";
            }
        });

        var_dump($arr);

        // IF YOU WANT TO REMOVE ALL EMPTY ARRAYS COMPLETELY, JUST DO THIS:
        $arr   = array_filter($arr);

        var_dump($arr);

        // GET THE LENGTH OF THE FILTERED ARRAY.
        $count = count($arr);  
        echo $count;   //<== PRODUCES 5

        // THE 1ST VAR_DUMP() PRODUCES:         
        array (size=5)
          0 => 
            array (size=1)
              0 => string 'nil' (length=3)
          1 => 
            array (size=1)
              0 => string 'nil' (length=3)
          2 => 
            array (size=1)
              0 => string 'nil' (length=3)
          3 => 
            array (size=1)
              'some_value' => int 4
          4 => 
            array (size=1)
              0 => string 'nil' (length=3)

        // THE 2ND VAR_DUMP() PRODUCES:
        array (size=5)
          0 => 
            array (size=1)
              0 => string 'nil' (length=3)
          1 => 
            array (size=1)
              0 => string 'nil' (length=3)
          2 => 
            array (size=1)
              0 => string 'nil' (length=3)
          3 => 
            array (size=1)
              'some_value' => int 4
          4 => 
            array (size=1)
              0 => string 'nil' (length=3)

Test it out HERE.

Cheers & Good Luck...

Comments