Home > Back-end >  Short array and update key as of value[0] and remove unused data
Short array and update key as of value[0] and remove unused data

Time:01-23

Here is my array so far i am exploding the [category_name] using this php function

$query = $this->db->get('payment');

        $result = $query->result();

        // echo '<pre>';print_r($result);

        foreach ($result as $key => $category) {

            $category_sale_data = explode(',', $category->category_name);

            foreach ($category_sale_data as $key => $value) {

                $category->sale_data[] = explode('*', $value);

            }
            
            $newresults[]=$category;
        }
        
        $result=$newresults;

        echo '<pre>';print_r($result);
        die();

Here is output:

Array
(
    [0] => stdClass Object
        (
            [category_name] => 32459*1500*lab*1,32460*400*lab*1,32461*600*lab*1
            [sale_data] => Array
                (
                    [0] => Array
                        (
                            [0] => 32459
                            [1] => 1500
                            [2] => lab
                            [3] => 1
                        )

                    [1] => Array
                        (
                            [0] => 32460
                            [1] => 400
                            [2] => lab
                            [3] => 1
                        )

                    [2] => Array
                        (
                            [0] => 32461
                            [1] => 600
                            [2] => lab
                            [3] => 1
                        )

                )

        )

)

i want output like this

Array
(
    [0] => stdClass Object
        (
            [category_name] => 32459*1500*lab*1,32460*400*lab*1,32461*600*lab*1
            [sale_data] => Array
                (
                    [32459] => Array
                        (
                            
                            [0] => 1500
                        )

                    [32460] => Array
                        (
                            [0] =>400
                        )

                    [32461] => Array
                        (
                            [0] => 600
                        )

                )

        )

)

i need to update records to short array to make it useful for me later, i have tried my best please update me asap I am newbie and learning about php arrays that's why looking an expert to help me solve this problem and help me to grow my skills, Bundle of thanks in advance

CodePudding user response:

You need to extract the key and the value before to add data in sale_data :

foreach ($category_sale_data as $key => $value) {
    [$sale_key, $sale_value] = explode('*', $value);
    $category->sale_data[$sale_key][] = $sale_value;
}

Full example - (livedemo)

// Sample data
$category = (object) ['category_name' => '32459*1500*lab*1,32460*400*lab*1,32461*600*lab*1'];
// process
$category_sale_data = explode(',', $category->category_name);
foreach ($category_sale_data as $key => $value) {
    [$sale_key, $sale_value] = explode('*', $value);
    $category->sale_data[$sale_key][] = $sale_value;
}
// display
print_r($category);

Output :

stdClass Object
(
    [category_name] => 32459*1500*lab*1,32460*400*lab*1,32461*600*lab*1
    [sale_data] => Array
        (
            [32459] => Array
                (
                    [0] => 1500
                )
            [32460] => Array
                (
                    [0] => 400
                )
            [32461] => Array
                (
                    [0] => 600
                )
        )
)
  •  Tags:  
  • Related