3

So I have a foreach loop which loops though an array of items. The checkIfSubCategorie() returns an array with new items.

I want to place all these items in the $subcategorien array. This works good. The problem is that those items don't go through the initial foreach loop. Is this possible and if yes, how?

foreach($subcategorien as $subcat) {

    //make array with all subcategorien
    $newarray = self::checkIfSubCategorie($subcat);

    if(is_array($newarray)) {
        foreach($newarray as $a) {

            // add item to subcategorien
            array_push($subcategorien, $a);
        }
    }
}

This is the $subcategorien array before the initial foreach loop:

array(2) {
  [0]=> string(3) "701"
  [1]=> string(3) "702"
}

var_dump of $newarray:

array(1) {
  [0]=> string(1) "8"
}
bool(false)

This is the $subcategorien array after the foreach loop:

array(3) {
  [0]=> string(3) "701"
  [1]=> string(3) "702"
  [2]=> string(1) "8"
}

The result should be:

array(3) {
  [0]=> string(3) "701"
  [1]=> string(3) "702"
  [2]=> string(1) "8"
  [3]=> string(1) "9"
}
11
  • can you var_dump the value of $newarray in the first loop, before line4? Commented Dec 16, 2015 at 9:34
  • Hi, Can you please show your array's output Commented Dec 16, 2015 at 9:35
  • So, just to be sure I understand, you want the new values added to $subcategorien to be lopped in the first foreach? Commented Dec 16, 2015 at 9:35
  • @Epodax yes that's what I want Commented Dec 16, 2015 at 9:36
  • @rahul added the output Commented Dec 16, 2015 at 9:37

3 Answers 3

1

What you are looking for is called recursive function. Something like:

function getCategoryChildren($categoryParentId, $categories = array()) {
    // TODO: query to get all the categories by their parents id
    while ($row = $stmt->fetch_assoc()) {
         $categories[] = $row['category_id'];
         $categories = getCategoryChildren($row['category_id'], $categories);
    }
    return $categories;
}

$allCategories = getCategoryChildren(0);
Sign up to request clarification or add additional context in comments.

Comments

0

Try this it similar to your code:

 $result_array = array();
 foreach($subcategorien as $subcat){
    //make array with all subcategorien
    $newarray = checkIfSubCategorie($subcat);
    $result_array[] = $subcat;
    if(is_array($newarray)){
        foreach($newarray as $a){
         $result_array[] = $a;
        }
    }
 }

Comments

0

From PHP Manuals array_push() will raise a warning if the first argument is not an array. This differs from the $var[] behavior where a new array is created.

If you use array_push() to add one element to the array it's better to use $array[] = because in that way there is no overhead of calling a function.

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.