2

I have coding about 3 dimensional array. I need a function to automatically check where the empty slot is, and then insert the empty array ($rhw[104][1][2]) values Class C.

The coding structure is,

$rhw[101][1][2] = "Class A";
$rhw[102][1][2] = "Class B";
$rhw[103][1][2] = "";

And i just can make like the coding below,

if (empty($rhw[103][1][2])) {
    echo "TRUE";
} else {
    echo "FALSE";
}

But there is already declared like --- if (empty($rhw[103][1][2])) --- I dont know how to automatically check where the empty slot is (which is $rhw[103][1][2]).

Such as,

if (empty($rhw[][][])) {
    insert "Class C";
} else {
    echo "The slot has been fulfilled";
}

But it can not be proceed.

Thank you, guys! :)

2 Answers2

0

Taken from in_array() and multidimensional array

in_array() does not work on multidimensional arrays. You could write a recursive function to do that for you:

function in_array_r($needle, $haystack, $strict = false) {
    foreach ($haystack as $item) {
        if (($strict ? $item === $needle : $item == $needle) || (is_array($item) && in_array_r($needle, $item, $strict))) {
            return true;
        }
    }

    return false;
}

Usage:

$b = array(array("Mac", "NT"), array("Irix", "Linux"));
echo in_array_r("Irix", $b) ? 'found' : 'not found';

For check a particular position, you can use a more simple solution:

if(isset($rhw[103]) && isset($rhw[103][1]) && isset($rhw[103][1][2]))
{
    echo "TRUE";
} 
else 
{
    echo "FALSE";
}

Or use a function for check isset for each multidimensional position.

function check_multidimensional($data, $a, $b, $c)
{
    return isset($data[a]) && isset($data[$a][$b]) && isset($data[$a][$b][$c]);
}

You can even make a more generic function for N dimensions.

Community
  • 1
  • 1
shakaran
  • 10,612
  • 2
  • 29
  • 46
  • what does 'in_array() does not work on multidimensional arrays' means? – Grace Martina Mar 19 '13 at 02:47
  • I mean, why it doesnt work? Could i make the function to automatically check which the empty slot is? – Grace Martina Mar 19 '13 at 02:49
  • Yup, i see. I didn't get it clearly but i will re-read it for many times. Thank you. And if i had check the empty slot, how could the values fill in the empty 3 dimensional array? – Grace Martina Mar 19 '13 at 02:54
  • @Grace Martina in_array() only works on one dimensional arrays because it doesn't check for/recurse into additional dimensions. So, you have to write your own. – Patashu Mar 19 '13 at 03:02
  • @GraceMartina that's probably another SO question, but you can use array_fill() function http://php.net/manual/es/function.array-fill.php – shakaran Mar 19 '13 at 03:03
  • array_fill() function -- okay i will check it out.. thank you for your supports.. – Grace Martina Mar 19 '13 at 03:06
  • @GraceMartina a upvote in my answer would be appreciated (only if it is suitable for you). I feed my dog with karma points :) – shakaran Mar 19 '13 at 03:29
  • @GraceMartina It's the arrow showed in each answer (left side) that allow you show that a particular answer is useful. That gives reputation points to the person who answers you. – shakaran Mar 19 '13 at 22:58
0

Coba ini deh udah di edit. penasaran :p

$rwh = array(
   101 => array( 1 => array(1 => 'Value key 1', 2 => 'Class A')),
   102 => array( 1 => array(1 => 'Value key 1', 2 => 'Class B')),
   103 => array( 1 => array(1 => 'Value key 1', 2 => ''))
);

echo 'PERTAMA : '.print_r($rwh);

function emptyArray($array = array() , $newval = '')
{
    $key_val = array();

    if(is_array($array) && !empty($array))
    {
        foreach($array as $key => $value)
        {
            $key_val[$key] = emptyArray($value, $newval);
        }
    }
    else if(empty($array))
        return $newval;
    else
        return $array;

    return $key_val;
}

$hasil = emptyArray($rwh, 'Class C');
echo "AKHIR : ".  print_r($hasil);