在数组内循环以添加动态数据,在 php 中

问题描述 投票:0回答:1

我正在尝试使用 foreach 循环在数组内创建一个数组,我有一个示例输出,问题是我无法生成与示例完全相同的输出 - 这是我的代码:

$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 SDD\r\n- 16GB Unified\r\n- M2 Chip Processor\r\n- 15\" Screen","arpya":""},"i117":{"procured_item_name":"Lenobo ThinkPad","procured_item_quantity":"2","gmy78":"","wki9u":"","procured_item_description":"- 500GB SDD\r\n- 16GB Unified\r\n- Core i7 Processor\r\n- 15\" Screen","arpya":""}},"procured_item_name":["Macbook Pro","Lenobo ThinkPad"],"procured_item_quantity":["1","2"],"gmy78":["",""],"wki9u":["",""],"procured_item_description":["- 500GB SDD\r\n- 16GB Unified\r\n- M2 Chip Processor\r\n- 15\" Screen","- 500GB SDD\r\n- 16GB Unified\r\n- Core i7 Processor\r\n- 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' ] );

正确的打印数组应该是这样的,预期输出如下:

数组(4) { ["form"]=> 字符串(2) "10" ["row_ids"]=> 数组(1) { [0]=> 字符串(3) "0,1" } [0] => 数组(4) { [0]=> 字符串(0) "" [320]=> 字符串(11) "Macbook Pro" [323]=> 字符串(1) "1" [322]=> 字符串( 59) "- 500GB SDD - 16GB 统一 - M2 芯片处理器 - 15" 屏幕" } [1]=> 数组(4) { [0]=> 字符串(0) "" [320]=> 字符串(15) " Lenobo ThinkPad" [323]=> string(1) "2" [322]=> string(59) "- 500GB SDD - 16GB Unified - Core i7 处理器 - 15" 屏幕" } }

不幸的是,我的数组是不同的,因为当我尝试添加这些动态数据时,它添加了数组的另一层。这是我的输出:

数组(4) { ["form"]=> 字符串(2) "10" ["row_ids"]=> 数组(1) { [0]=> 字符串(4) "0, 1" } [0] => 数组(1) { [0]=> 数组(4) { [0]=> 字符串(0) "" [320]=> 字符串(11) "Macbook Pro" [323]=> 字符串(1) "1" [322]=> 字符串(62) "- 500GB SDD - 16GB 统一 - M2 芯片处理器 - 15" 屏幕" } } [1]=> 数组(1) { [1]=> 数组(4) { [0]=> 字符串(0) "" [320]=> 字符串(15) "Lenobo ThinkPad" [323]=> 字符串(1) "2" [322]=> 字符串(62) "- 500GB SDD - 16GB 统一 - 酷睿 i7 处理器 - 15 英寸屏幕” } } }

我需要帮助来生成预期的数组。

php arrays foreach
1个回答
0
投票

修改以下行

    // 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'] ),
    );

到我的版本,

    // 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'] );

其次,请格式化预期输出,也使用

print_r()
<pre>
更容易阅读。

    print("<pre>");
    print_r($values);
© www.soinside.com 2019 - 2024. All rights reserved.