0

How can I call specific array when the array keys are numbers. My array looks like this:

my arrays is equal to $output:

Array
(
    [7] = Array
        (
            [name] = Grill Specifications
            [attributes] = Array
                (
                    [46] = Array
                        (
                            [name] = Fuel Type
                            [values] = Array
                                (
                                    [0] = Charcoal,Natural Gas,Combo
                                    [1] = Charcoal,Propane,Combo
                                    [2] = Charcoal,Propane,Natural Gas,Combo
                                    [3] = Natural Gas
                                    [4] = Natural Gas,Propane
                                    [5] = Propane
                                    [6] = Propane,Natural Gas
                                )

                        )

and so on... I have alot of them...

What I need to do is take all the keys of the [values] and explode the values of those keys where there is a comma. Then make them all unique. I do not know how to call those arrays without having to keep typing the numbers like this: $output[7][attributes][46][values]

Any help would be appreciated.

JCBiggar
  • 2,477
  • 3
  • 20
  • 33

3 Answers3

0
function extractUniques(array $array) {
    $values = [];
    $callback = function(array $array) use (&$callback, &$values) {
        foreach ($array AS $key => $value) {
            $isArray = is_array($value);
            if (strcasecmp($key, 'value') === 0 && $isArray) {
                foreach ($value AS $item) {
                    if (is_scalar($item)) {
                        $values = array_merge($values, explode(',', (string) $item));
                    }
                }
            } else if ($isArray) {
                $callback($value);
            }
        }
    };
    $callback($array);
    $trimmed = array_map(function($value) {
        return trim($value);
    }, $values);
    return array_unique($trimmed);
}

This function goes recursively through the array and searches keys equal to "value", if array is found they are being iterated and every scalar element is exploded using comma as a delimiter. Finally all strings are trimmed and unique list of elements is returned.

Usage:

$uniques = extractUniques($output);
var_dump($uniques);

Also, you probably are going to need a rather new version of PHP. 5.3 or 5.4 should work fine. If it is not working you can start with replacing $values = []; with $values = array(); and replacing $callback with actual function so you won't be using closures.

Mikk
  • 2,209
  • 3
  • 32
  • 44
0

If you have a specific array deep in a tree like that, and you just want to read the values, simply assign the sub-array to a variable.

$values = $output[7]['attributes'][46]['values'];

(Note that any of those keys can be variables. For example, if $id === 7 and $key === 'attributes', you can say $values = $output[$id][$key][46]['values']; to do the same thing.)

Now you can just look at $values instead of spelunking in a tree every time, because each is effectively a copy of the other.

assert('$values[0] === $output[7]["attributes"][46]["values"][0]');

If you need to be able to change the array, you can use a reference instead...

$values =& $output[7]['attributes'][46]['values'];
#        ^
#        +-- note the ampersand

That makes $values an alias for $output[7]['attributes'][46]['values'], so any change you make to one will also appear in the other.

(Note, though, references tend to cause magical-looking behavior -- and in a computer program, magic is often a bad thing. Don't go using references willy-nilly just because you can; they should only be used where they're clearer than the alternative.)

As for the problem at hand, once you have $values, it'd be simpler to join and then explode.

$joined = implode(',', $values);
$unique = array_unique(explode(',', $joined));
cHao
  • 84,970
  • 20
  • 145
  • 172
0
$values = array();

for ($i = 0; $i < count($output[7]['attributes'][46]['values']); $i++) {
    $exploded = explode(',', $output[7]['attributes'][46]['values'][$i]);

    for ($j = 0; $j < count($exploded); $j++) {
        array_push($values, $exploded[$j]);
    }
}

echo array_unique($values);

That's probably the most straightforward way to get this done and you can add extra comparisons and validation based on what you find in your data.

gfish3000
  • 1,557
  • 1
  • 11
  • 22
  • Arrays don't have a `.length` in PHP. Use `count(your array)` instead. Or even better, `foreach ((your array) as $i => $str)`. – cHao Apr 12 '14 at 16:45
  • Opps, got my wires crossed between PHP and JS there for a sec. Good catch @cHao, thanks! – gfish3000 Apr 12 '14 at 17:00