1

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

2
  • 1
    Your SQL fetch is returning a multidimensional array and then you are adding each element in the array to each element of a new array. Commented Dec 11, 2013 at 8:56
  • check by printing $modelSpecs Commented Dec 11, 2013 at 8:57

3 Answers 3

6

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.

Sign up to request clarification or add additional context in comments.

Comments

0

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;
}

2 Comments

You answered your own question. A good example of 'Rubber ducking'. ;) en.wikipedia.org/wiki/Rubber_duck_debugging
You also removed the [], which I think may have been your problem.
-1

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>";

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.