1

I have a multidimensional array

output_querys =  array(2) { [0]=> array(5) { ["ID"]=> string(1) "2" ["Code"]=> string(6) "AWS001" ["BCode"]=> string(4) "B001" ["Des"]=> string(4) "loan" ["subCat"]=> string(3) "SWA" } [1]=> array(5) { ["ID"]=> string(1) "4" ["Code"]=> string(6) "AWS002" ["BCode"]=> string(4) "B002" ["Des"]=> string(3) "tax" ["subCat"]=> string(3) "PJA" } }

I want to get this to a variable and then save into dB.I have tried all the methods that has mentioned in related problems nothing worked. Errors I got 1.trying non-object 2.Illegal string offset

what I was trying to do is

foreach($output_querys as $output_query){
  $Pv_ID = $output_query->ID;
   $Pv_Code = $output_query->Code;
      $Pv_Description = $output_query->Des;
      
    
    $data_final = array(id => $Pv_ID, code => $Pv_Code, des => $Pv_Description);
                                                
      }
      db->insert(abc, $data_final);

what is the problem here??.even i tried with json encode and still shows the "Illegal string offset"

        $Pv_ID= json_decode($output_query['ID']);
                                       
        $Pv_Code = json_encode($output_query['Code'],true);
                                            
        $Pv_Description = json_encode($output_query['Des'],true);

6
  • 1
    Have you tried instead of using the -> (arrow), but to use $output_query['ID'] ? Commented Feb 7, 2022 at 11:48
  • 1
    when you access objects you use -> but when you want to access array do this $arr['key'] Commented Feb 7, 2022 at 11:50
  • @PeriklisKakarakidis I tried it but is shows illegel string off set to all Commented Feb 7, 2022 at 12:14
  • @RaoDYC it shows illegal stringoffset Commented Feb 7, 2022 at 12:15
  • @coder_000_ ok, well can you please provide the error in your question? Commented Feb 7, 2022 at 12:19

1 Answer 1

2

I tried your given array and code

    <?php
  $output_querys = array( 
    0 => array( 
            "ID" => "2", 
            "Code" => "AWS001", 
            "BCode" => "B001", 
            "Des" => "loan", 
            "subCat" => "SWA" 
    ),    
    1 => array( 
            "ID" => "4", 
            "Code" => "AWS002", 
            "BCode" => "B002", 
            "Des" => "tax", 
            "subCat" => "PJA" 
        )
);
  foreach($output_querys as $output){
    print_r($output['Code']); echo "\n";
    
  }
?>

It is working fine on my end. Here's the working example

I think you need to do this as well, if you want to insert 1 by 1 then move the insert in foreach loop

foreach($output_querys as $output_query){
  $Pv_ID = $output_query->ID;
   $Pv_Code = $output_query->Code;
      $Pv_Description = $output_query->Des;
      
    
    $data_final = array(id => $Pv_ID, code => $Pv_Code, des => $Pv_Description);
     db->insert(abc, $data_final);                                           
      }
      
Sign up to request clarification or add additional context in comments.

1 Comment

Thankyou.its working like this but when i get this to an array to save in dB Message: Illegal string offset 'id' warning is coming

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.