开发者

PHP Getting unique values from multidimensional array inside a multidimensional array

开发者 https://www.devze.com 2023-04-06 19:46 出处:网络
I\'m trying to get values from a multidimensional array inside a multidimensional array. Here is the multidimensional array...

I'm trying to get values from a multidimensional array inside a multidimensional array. Here is the multidimensional array...

Array
(
    [0] => Array
        (
            [CalID] => 121111
            [Rink] => North
            [TimeChunks] => Array
                (
                    [int] => Array
                        (
                            [0] => 6
                            [1]开发者_Python百科 => 4
                            [2] => 3
                            [3] => 2
                            [4] => 1
                        )

                )

        )

    [1] => Array
        (
            [CalID] => 121111
            [Rink] => South
            [TimeChunks] => Array
                (
                    [int] => Array
                        (
                            [0] => 4
                            [1] => 2
                        )

                )


        )
)

I want to get only the valid time chunks from [TimeChunks][int] ie: 1,2,3,4,6,8 but I can't seem to drill down to the second multidimensional array. Here is what I've been trying with no dice:

    $tmp = array ();

foreach ($a as $row) 
    if (!in_array($row,$tmp)) array_push($tmp,$row);

print_r ($tmp);

Any suggestions?


$tmp = array();

foreach ($a AS $row) {
  if (isset($row['TimeChunks']['int']) && is_array($row['TimeChunks']['int'])) {
    $tmp = array_merge($tmp, $row['TimeChunks']['int']);
  }
}
$tmp = array_unique($tmp);

If the array keys aren't important to you, this should do the trick. It just combines all the values in the different [TimeChunks][int] and then removes the duplicates afterwards.


You're not looping through the array in the 'TimeChunks' key in your array at all, try this:

foreach ($a as $row) {
  foreach ($row['TimeChunks'] as $chunk) {
    if (!in_array($row,$chunk)) array_push($tmp,$$chunk);
  }
}


why not use foreach($a as $key=> $value)

then you can use your if statement on the keys and values if you want


Something like this?

$tmp = array();
foreach ($array as $section) { // Loop outer array
  if (isset($section['TimeChunks']['int'])) { // make sure ['TimeChunks']['int'] is set for this inner array
    foreach ($section['TimeChunks']['int'] as $chunk) { // Loop the time chunks
      $tmp[] = $chunk; // Push the chunks onto the $tmp array
    }
  }
}
$tmp = array_unique($tmp); // Remove duplicate values

print_r($tmp);

You have to loop through the outer array, get the contents of the ['TimeChunks']['int'] key and push that onto the $tmp array. You can do an in_array() check on each iteration, but it would be more efficient (less function calls) to add them all to the $tmp array and pass it through array_unique() afterwards.

0

精彩评论

暂无评论...
验证码 换一张
取 消