user3004208
user3004208

Reputation: 559

PHP multidimensional array being nested twice

I am using this function to get data from a database.

public function getModelSpecs($modelCode) {
    $modelSpecs = db::fetch_array("SELECT * FROM `product_specs` WHERE model='{$modelCode}'");

    return $modelSpecs;
}

And i am accessing by using a for loop based on the count of a previous item.

for ($i = 0; $i < count($model); $i++) {
    $modelSpecs[] = $product->getModelSpecs($model[$i]['code']);
    $count++;
}
echo "<pre>".print_r($modelSpecs, true)."</pre>";

This all works but why is it nesting my array like so, as in its two levels deep.

Array
(
    [0] => Array
        (
            [0] => Array
                (
                    [id] => 183
                    [model] => ZS125-48A
                    [engine_brand] => 158FMI-B
                )

        )

    [1] => Array
        (
            [0] => Array
                (
                    [id] => 172
                    [model] => STR125YB
                    [engine_brand] => K154FMI
                )

        )

Is that just the structure of it as i am already fetching array in the function or can i not make it so that it is only down one level like so:

Array
(
    [0] => Array
        (
                [id] => 183
                [model] => ZS125-48A
                [engine_brand] => 158FMI-B
        )

    [1] => Array
        (
                [id] => 172
                [model] => STR125YB
                [engine_brand] => K154FMI
        )

Thanks

Upvotes: 1

Views: 112

Answers (3)

Lenny
Lenny

Reputation: 5939

fetch_array returns an array of entries.

so it's like

  • [0] = entry 1
  • [1] = entry 2
  • ...etc

So if you only want the first entry you need to do a reset() on it to get the first item. Here is the updated code that should work...

for ($i = 0; $i < count($model); $i++) {
    $modelSpecs[] = reset($product->getModelSpecs($model[$i]['code']));
    $count++;
}
echo "<pre>".print_r($modelSpecs, true)."</pre>";

This will cause an error if that function for some reason doesn't return an array (if the query finds nothing so you might want to do this instead.

for ($i = 0; $i < count($model); $i++) {
    $myModelSpec=$product->getModelSpecs($model[$i]['code']);
    //goes to the next item if this last item isn't an array.
    if(!is_array($myModelSpec)) continue;
    $modelSpecs[] = reset($myModelSpec);
    $count++;
}
echo "<pre>".print_r($modelSpecs, true)."</pre>";

Upvotes: -1

Drumbeg
Drumbeg

Reputation: 1944

I think the line

$modelSpecs[] = $product->getModelSpecs($model[$i]['code']);

should just be

$modelSpecs = $product->getModelSpecs($model[$i]['code']);

Otherwise you are assigning your result set to an element of the $modelSpecs array, whereas I think you are trying to assign $modelSpecs to the whole of the result set.

Upvotes: 6

user3004208
user3004208

Reputation: 559

Just to update everyone i have fixed the issue by using fetch_row so it fetches a new row each time then stores it into the array.

public function getModelSpecs($modelCode) {
    $modelSpecs = db::fetch_row("SELECT * FROM `product_specs` WHERE model='{$modelCode}'");

    return $modelSpecs;
}

Upvotes: 0

Related Questions