Home > OS >  php array returning same values in loop
php array returning same values in loop

Time:01-25

I am trying to loop through each key but i am facing a problem of same value repeating inside for each loop

Here is example of my current code and result (click here)

here is my code so far

<?php
$data2 = array(
        'category_name' => '33287*100*prescription*1,32457*1250*lab*1'
    );
    $result = array('0' => (object)$data2);

    foreach ($result as $key => $category) {
        $category_name = explode(',', $category->category_name);
    }

    $newresults=[];
    foreach ($category_name as $key) {
        $category->category_name = $key;
        $newresults[]=$category;
    }

    $result=$newresults;

    $newresults=[];

        $category->items_count = 0;
        
        foreach ($result as $key => $value) {

            list($sale_key, $sale_value) = explode('*', $value->category_name);
            // $category->items_count  = count($sale_value);
            $newresults[]=$category;
        }

    $result=$newresults;

   

i am expect the result should be

Array
(
    [0] => stdClass Object
        (
            [category_name] => 33287*100*prescription*1
            [items_count] => 0
        )

    [1] => stdClass Object
        (
            [category_name] => 32457*1250*lab*1
            [items_count] => 0
        )

)

CodePudding user response:

The bug is that you're relying only on the last version of $category after you've finished looping it earlier - you'd have to be using it within the loop where it's assigned, in order to get each value in turn, or you could use $value from your last foreach loop.

But as a general observation, this code has way too many loops etc. just for processing one array in the way you've requested. Here's a much simpler version:

$category_name = '33287*100*prescription*1,32457*1250*lab*1';
$category_name_arr = explode(',', $category_name);

print_r($category_name_arr);

$newresults=[];

foreach ($category_name_arr as $cat) {
    $newresults[] = (object) array("category_name" => $cat, "items_count" => 0);
}

print_r($newresults);

Demo: http://sandbox.onlinephpfunctions.com/code/065a32b40f67e00aa85f0f5b58ecacd510f2f38a


If you still need to be able to support multiple lines of input, you can do it like this, by just merging the exploded arrays before you process them:

$data = array(
    '33287*100*prescription*1,32457*1250*lab*1',
    '33222*900*prescription*3,22233*1200*lab*2',
    );
$category_name_arr = [];

foreach ($data as $category_name)
{
  $category_name_arr = array_merge($category_name_arr, explode(',', $category_name));
}

print_r($category_name_arr);

$newresults=[];

foreach ($category_name_arr as $cat) {
    $newresults[] = (object) array("category_name" => $cat, "items_count" => 0);
}

print_r($newresults);

CodePudding user response:

adding $category = new stdClass();

foreach ($category_name as $key) {
            

$category = new stdClass();

            $category->category_name = $key;
            $newresults[]=$category;
        }
  •  Tags:  
  • Related