skip to Main Content

I’m trying to create an array inside an array by using a foreach loop, I have a sample out come and the problem is I can not produce the exact out come as the sample – here’s my code:

$json = '{"id":"118","item_key":"m6jgm","name":"Bright Madire","ip":"192.168.92.81","meta":{"2b9ta":"Bright","qy6sa":"Madire","k9ngs":"Bright Madire","y69z6":"[email protected]","58nj0":"0774222497","q78bq":"IS","culq4":"Software Engineer","tqe5r":"IT and Electronics","procured_items":{"form":"11","i116":{"procured_item_name":"Macbook Pro","procured_item_quantity":"1","gmy78":"","wki9u":"","procured_item_description":"- 500GB SDDrn- 16GB Unifiedrn- M2 Chip Processorrn- 15" Screen","arpya":""},"i117":{"procured_item_name":"Lenobo ThinkPad","procured_item_quantity":"2","gmy78":"","wki9u":"","procured_item_description":"- 500GB SDDrn- 16GB Unifiedrn- Core i7 Processorrn- 15" Screen","arpya":""}},"procured_item_name":["Macbook Pro","Lenobo ThinkPad"],"procured_item_quantity":["1","2"],"gmy78":["",""],"wki9u":["",""],"procured_item_description":["- 500GB SDDrn- 16GB Unifiedrn- M2 Chip Processorrn- 15" Screen","- 500GB SDDrn- 16GB Unifiedrn- Core i7 Processorrn- 15" Screen"],"arpya":["",""],"zzrfl":"","1g8ya":"","6cm1d":"RFQ","qcl2b":"No","xx48b":"2023-07-27","zsii":"16:40","request_status":"Send To Suppliers","qaolg":"","sromp":"","department_approval":"Approve","tvghy":"","finance_approval":"Approve","w2iyy":"","2iyii":"","2iyii-value":"8","fsu1s":"","nh3tu":"3"},"form_id":"10","post_id":"0","user_id":"8","parent_item_id":"0","is_draft":"0","updated_by":"10","created_at":"2023-07-27 14:24:42","updated_at":"2023-07-27 14:37:10"}';

$entryArray = json_decode($json, TRUE);
    $procuredItems = $entryArray['meta']['procured_items'];

    $first_field = 320;
    $second_field = 323;
    $third_field = 322;

    $ids = "0";
    $rows = "";
    $i = 1;
    $bigArray = array();
    
        $values[ 'value' ] = array(
          'form' => '10',
          'row_ids' => array( '0, 1' ),
        );
        
        
foreach($procuredItems as $key=>$value) {
  if ($key != "form"){
    
    // defining the dynamic values
    $title = $value['procured_item_name'];
    $quantity = $value['procured_item_quantity'];
    $desc = $value['procured_item_description'];

    // inserting the dynamic arrays into the main array
    $values[ 'value' ][] = array(
    $i-1 => array( 0 => '', $first_field => $value['procured_item_name'], $second_field => $value['procured_item_quantity'], $third_field => $value['procured_item_description'] ),
    );
    
    $i += 1;
  }
}

//this print my trial code output
var_dump( $values[ 'value' ] );
echo '</br>';
echo '</br>';
echo '</br>';

// Sample array structure
     $values[ 'value' ] = array(
       'form' => '10',
       'row_ids' => array( $ids ),
          
        0 => array( 0 => '', $first_field => 'Macbook Pro', $second_field => '1', $third_field => '- 500GB SDD - 16GB Unified - M2 Chip Processor - 15" Screen' ),
        1 => array( 0 => '', $first_field => 'Lenobo ThinkPad', $second_field => '2', $third_field => '- 500GB SDD - 16GB Unified - Core i7 Processor - 15" Screen' ),
     );

// this print supposed sample output
var_dump( $values[ 'value' ] );

The correct printed array should be like this, the expected output below:

array(4) { ["form"]=> string(2) "10" ["row_ids"]=> array(1) { [0]=> string(3) "0,1" } [0]=> array(4) { [0]=> string(0) "" [320]=> string(11) "Macbook Pro" [323]=> string(1) "1" [322]=> string(59) "- 500GB SDD – 16GB Unified – M2 Chip Processor – 15" Screen" } [1]=> array(4) { [0]=> string(0) "" [320]=> string(15) "Lenobo ThinkPad" [323]=> string(1) "2" [322]=> string(59) "- 500GB SDD – 16GB Unified – Core i7 Processor – 15" Screen" } }

Unfortunalely my array is different because the moment I am trying to add those dynamic data, it adds another layer of an array. this is my output:

array(4) { ["form"]=> string(2) "10" ["row_ids"]=> array(1) { [0]=> string(4) "0, 1" } [0]=> array(1) { [0]=> array(4) { [0]=> string(0) "" [320]=> string(11) "Macbook Pro" [323]=> string(1) "1" [322]=> string(62) "- 500GB SDD – 16GB Unified – M2 Chip Processor – 15" Screen" } } [1]=> array(1) { [1]=> array(4) { [0]=> string(0) "" [320]=> string(15) "Lenobo ThinkPad" [323]=> string(1) "2" [322]=> string(62) "- 500GB SDD – 16GB Unified – Core i7 Processor – 15" Screen" } } }

I need help on producing the expected array.

2

Answers


  1. Revise the following line

        // inserting the dynamic arrays into the main array
        $values[ 'value' ][] = array(
        $i-1 => array( 0 => '', $first_field => $value['procured_item_name'], $second_field => $value['procured_item_quantity'], $third_field => $value['procured_item_description'] ),
        );
    

    to my version,

        // inserting the dynamic arrays into the main array
        $values[ 'value' ][$i-1] = array( 0 => '', $first_field => $value['procured_item_name'], $second_field => $value['procured_item_quantity'], $third_field => $value['procured_item_description'] );
    

    Second, please formatting expected output, also using print_r() with <pre> more easier to read.

        print("<pre>");
        print_r($values);
    
    Login or Signup to reply.
  2. I think there is too much complexity in your coding attempt.

    If I am correctly understanding that form comes from $entryArray['meta']['procured_items']['form'], row_ids is a delimited string populated from the number of procured items, and you are pushing subsets of data into the result array, then the following should suffice.

    Code: (Demo)

    $result = [
        'form' => $entryArray['meta']['procured_items']['form'],
        'row_ids' => implode(', ', range(0, count($entryArray['meta']['procured_items']) - 2)),
    ];
    foreach ($entryArray['meta']['procured_items'] as $k => $procuredItem) {
        if ($k === 'form') {
            continue;
        }
        $result[] = [
            0 => '',
            320 => $procuredItem['procured_item_name'],
            323 => $procuredItem['procured_item_quantity'],
            322 => $procuredItem['procured_item_description'],
        ];
    }
    var_export($result);
    

    Of course, you can use variables $first_field, $second_field, and third_field if there is some benefit in doing so.

    Login or Signup to reply.
Please signup or login to give your own answer.
Back To Top
Search