Take the tour ×
Stack Overflow is a question and answer site for professional and enthusiast programmers. It's 100% free, no registration required.

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

share|improve this question
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. –  Drumbeg Dec 11 at 8:56
 
check by printing $modelSpecs –  vishal shah Dec 11 at 8:57
add comment

3 Answers

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.

share|improve this answer
add comment

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;
}
share|improve this answer
 
You answered your own question. A good example of 'Rubber ducking'. ;) en.wikipedia.org/wiki/Rubber_duck_debugging –  Drumbeg Dec 11 at 9:09
 
You also removed the [], which I think may have been your problem. –  Drumbeg Dec 11 at 9:19
add comment

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>";
share|improve this answer
add comment

Your Answer

 
discard

By posting your answer, you agree to the privacy policy and terms of service.

Not the answer you're looking for? Browse other questions tagged or ask your own question.