0

I am trying to format an array in php . It does not return in expected json format . Here is details.

$categories = category::where('cat_flag','Y')->pluck('cat_name as category_name')->toArray();
$items = item::leftjoin('categories','items.cat_id' ,'=', 'categories.id')
                      ->where('item_flag','Y')
                      ->get([
                            'item_name',
                            'items.cat_id as category_id',
                            'cat_name as category_name'
                        ])->toArray();

        $formatedArray = [];
        foreach ($categories as $category) {
            foreach ($items as $item) {
                if ($item['category_name'] == $category) {
                    $formatedArray['cat_name'] = $category;
                    $formatedArray['datas'][] = $item;
                }
            }
        }

my expected output is like bellow. but there is logical error in array format part . I am not getting properly how to format that. Thanks in advance

       [
         {
           "cat_name" : "food",
           "datas" : [
                {
                  "item_name": "item2",
                  "category_id": "1"
                },
                {
                  "item_name": "item4",
                  "category_id": "1"
                }
             ]
           },
           {
           "cat_name" : "drinks",
           "datas" : [
                {
                  "item_name": "coca cola",
                  "category_id": "4"
                }
             ]
           }
        ]

But my output showing like . All items are in same category

  {
"cat_name": "cat4",
"datas": [
    {
        "item_name": "item2",
        "category_id": "1"
    },
    {
        "item_name": "item22",
        "category_id": "2"
    }
 ]
}
3
  • what is the formatting error? Commented Jun 27, 2020 at 5:34
  • no error showing. I can't set "cat_name" : "food" this object . It just set once . But i want to mentioned format . Thanks for reply Commented Jun 27, 2020 at 5:40
  • in if you should $category->category_name not just $category maybe that is posint Commented Jun 27, 2020 at 6:25

2 Answers 2

1

I think the following code is the problem: In each iteration, $formattedArray contents are being replaced with new values!

$formatedArray = [];
        foreach ($categories as $category) {
            foreach ($items as $item) {
                if ($item['category_name'] == $category) {
                    $formatedArray['cat_name'] = $category;
                    $formatedArray['datas'][] = $item;
                }
            }
        }

Replace the above with this:

$formatedArray = $thisArray = [];
        foreach ($categories as $category) {
            foreach ($items as $item) {
                if ($item['category_name'] == $category) {
                    $thisArray['cat_name'] = $category;
                    $thisArray['datas'][] = $item;
                    $formatedArray[] = $thisArray;
                    $thisArray = [];
                }
            }
        }
Sign up to request clarification or add additional context in comments.

4 Comments

Thanks for reply . But It separates same category into multiple. I need same Items under same category
{ "cat_name": "cat2", "datas": [ { "item_name": "item2", "category_id": "1" } ] }, { "cat_name": "cat2", "datas": [ { "item_name": "item22", "category_id": "1" } ] },
But I need like bellow comment
{ "cat_name": "cat2", "datas": [ { "item_name": "item2", "category_id": "1" }, { "item_name": "item22", "category_id": "1" } ] }
1

Hey dude try this you are comparing string to object

foreach ($categories as $category) {
        foreach ($items as $item) {
            if ($item['category_name'] == $category) {
                if(!empty($formatedArray)){
                  foreach(formatedArray as $array){
                     if(!empty($array) && $array['cat_name']  == $category){
                          array_push($formatedArray['datas'], $item)
                     }else{
                          $newArray = [];
                          $newArray['cat_name'] = $category;
                          $newArray['datas'][] = $item;
                          array_push($formatedArray, $newArray);
                     }
                  }
                }else{
                  $formatedArray['cat_name'] = $category;
                  $formatedArray['datas'][] = $item
                }
            }
        }
    }

14 Comments

Thanks for reply. It was array that's why I converted into array like $category['category_name'] . But it showing Illegal string offset 'category_name'
@Mithun updated look at this did you mean like that
Thanks for giving me time. But again Illegal string offset 'category_name'
can you show me few example from database for both table
$Categories = array:2 [ 0 => "cat2" 1 => "cat4" ]
|

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.