Kasia13130
Kasia13130

Reputation: 29

Create an array for each line after explode ()

I am trying to do an array for each row after doing explode on it. The problem is that it overwrites me with the last value in the column I retrieved from the database instead of adding items to the array relative to the rows, or adds items to the array after one string even if the items were on separate rows in the database. How should I modify the code so that elements from a given line are added to the array and then elements from the next line are added to the next table, etc.? Thank you very much for all your help

        public static function findLilanteCategoryFromEmpik()
{
    $empik_lilante_categories = MysqlProvider::getEmpikCategory();
    $arr_lilante_category_from_empik = [];

    foreach($empik_lilante_categories as $categories)
    {
        $arrLilante = [];
        //$lilante_category_from_empik[] = $categories['lilante_category'];
        $lilanteCategories = $categories['lilante_category'];
        // echo "</br>";
        // print_r($lilanteCategories);
        // echo "</br>";

        $arrLilanteCategory = explode("|", $lilanteCategories);
        // echo "<br/>";
        // print_r($arrLilanteCategory);
        // echo "<br/>";
        array_push($arrLilante, $lilanteCategories);

        // echo "<br/>";
        // print_r($arrLilante);
        // echo "<br/>";
        //$arr_lilante_category_from_empik[] = $arrLilante;

    }
    array_push($arr_lilante_category_from_empik, $arrLilante);
    echo "<br/>";
    print_r($arr_lilante_category_from_empik);
    echo "<br/>";

    return $arr_lilante_category_from_empik;
}

Upvotes: 1

Views: 56

Answers (1)

Gerard de Visser
Gerard de Visser

Reputation: 8050

Try this cleaned up and adjusted code:

<?php
public static function findLilanteCategoryFromEmpik()
{
    $empik_lilante_categories = MysqlProvider::getEmpikCategory();
    $arr_lilante_category_from_empik = [];

    foreach($empik_lilante_categories as $categories)
    {
        $arrLilante = [];
        $lilanteCategories = $categories['lilante_category'];

        $arrLilanteCategory = explode("|", $lilanteCategories);
        array_push($arrLilante, $lilanteCategories);
        array_push($arr_lilante_category_from_empik, $arrLilante);
    }
    echo "<br/>";
    print_r($arr_lilante_category_from_empik);
    echo "<br/>";

    return $arr_lilante_category_from_empik;
}

Upvotes: 1

Related Questions