目錄
回复讨论(解决方案)
首頁 後端開發 php教程 求教,多维数组处理

求教,多维数组处理

Jun 23, 2016 pm 01:54 PM
多維 陣列

各位大神,我有一个多维数组,结构如下

array(7) {  ["hardware"]=>  array(1) {    ["status"]=>    string(2) "on"  }  ["software"]=>  array(2) {    ["systemchk"]=>    array(2) {      ["status"]=>      string(2) "on"      ["system"]=>      array(3) {        ["xp"]=>        array(3) {          ["status"]=>          string(2) "on"          ["sp"]=>          string(1) "1"          ["KB"]=>          string(0) ""        }        ["win7"]=>        array(3) {          ["status"]=>          string(2) "on"          ["sp"]=>          string(1) "1"          ["KB"]=>          string(0) ""        }        ["win8"]=>        array(3) {          ["status"]=>          string(2) "on"          ["sp"]=>          string(1) "1"          ["KB"]=>          string(0) ""        }      }    }    ["softwarechk"]=>    array(2) {      ["status"]=>      string(3) "off"      ["softfp"]=>      array(2) {        [0]=>        array(3) {          ["softfp_oid"]=>          string(18) "DEFAULT_SOFTFP_VRV"          ["list"]=>          string(1) "1"          ["group"]=>          string(0) ""        }        [1]=>        array(3) {          ["softfp_oid"]=>          string(18) "DEFAULT_SOFTFP_TMP"          ["list"]=>          string(1) "0"          ["group"]=>          string(0) ""        }      }    }  }  ["safechk"]=>  array(7) {    ["macchk"]=>    array(1) {      ["status"]=>      string(2) "on"    }    ["agentchk"]=>    array(1) {      ["status"]=>      string(2) "on"    }    ["termchk"]=>    array(1) {      ["status"]=>      string(2) "on"    }    ["arpchk"]=>    array(1) {      ["status"]=>      string(2) "on"    }    ["natchk"]=>    array(1) {      ["status"]=>      string(2) "on"    }    ["screenchk"]=>    array(3) {      ["status"]=>      string(2) "on"      ["screenpwd"]=>      string(1) "0"      ["screentime"]=>      string(1) "0"    }    ["oschk"]=>    array(1) {      ["status"]=>      string(2) "on"    }  }  ["illegalchk"]=>  array(4) {    ["status"]=>    string(2) "on"    ["pact"]=>    string(4) "HTTP"    ["ip"]=>    string(0) ""    ["port"]=>    string(0) ""  }  ["udiskchk"]=>  array(2) {    ["status"]=>    string(2) "on"    ["action"]=>    string(1) "0"  }  ["netcardchk"]=>  array(7) {    ["status"]=>    string(2) "on"    ["line"]=>    string(1) "1"    ["wireless"]=>    string(1) "1"    ["3g"]=>    string(1) "1"    ["vpn"]=>    string(1) "1"    ["other"]=>    string(1) "1"    ["total"]=>    string(1) "6"  }  ["userchk"]=>  array(1) {    ["status"]=>    string(2) "on"  }}
登入後複製

我想在数组里面增加一个元素flag与status同级,也就是说,只要是有status出现的,就有flag元素与之匹配,请问,PHP有这样的函数,实现这个功能么?


回复讨论(解决方案)

php 不可能预知你的特殊需求,所以必须你自己写

$a = array(  "hardware" => array(    "status" => "on",  ),  "software" => array(    "systemchk" => array(      "status" => "on",      "system" => array(        "xp" => array(          "status" => "on",          "sp" => "1",          "KB" => "",        ),        "win7" => array(          "status" => "on",          "sp" => "1",          "KB" => "",        ),        "win8" => array(          "status" => "on",          "sp" => "1",          "KB" => "",        ),      ),    ),    "softwarechk" => array(      "status" => "off",      "softfp" => array(        0 => array(          "softfp_oid" => "DEFAULT_SOFTFP_VRV",          "list" => "1",          "group" => "",        ),        1 => array(          "softfp_oid" => "DEFAULT_SOFTFP_TMP",          "list" => "0",          "group" => "",        ),      ),    ),  ),  "safechk" => array(    "macchk" => array(      "status" => "on",    ),    "agentchk" => array(      "status" => "on",    ),    "termchk" => array(      "status" => "on",    ),    "arpchk" => array(      "status" => "on",    ),    "natchk" => array(      "status" => "on",    ),    "screenchk" => array(      "status" => "on",      "screenpwd" => "0",      "screentime" => "0",    ),    "oschk" => array(      "status" => "on",    ),  ),  "illegalchk" => array(    "status" => "on",    "pact" => "HTTP",    "ip" => "",    "port" =>  "",  ),  "udiskchk" => array(    "status" => "on",    "action" => "0",  ),  "netcardchk" => array(    "status" => "on",    "line" => "1",    "wireless" => "1",    "3g" => "1",    "vpn" => "1",    "other" => "1",    "total" => "6",  ),  "userchk" => array(    "status" => "on",  ),);function addflag(&$ar) {  if(! is_array($ar)) return;$ar;  if(isset($ar['status'])) $ar['flag'] = '';  foreach($ar as &$v)  addflag($v);  return $ar;}var_export(addflag($a));
登入後複製
登入後複製
array (  'hardware' =>   array (    'status' => 'on',    'flag' => '',  ),  'software' =>   array (    'systemchk' =>     array (      'status' => 'on',      'system' =>       array (        'xp' =>         array (          'status' => 'on',          'sp' => '1',          'KB' => '',          'flag' => '',        ),        'win7' =>         array (          'status' => 'on',          'sp' => '1',          'KB' => '',          'flag' => '',        ),        'win8' =>         array (          'status' => 'on',          'sp' => '1',          'KB' => '',          'flag' => '',        ),      ),      'flag' => '',    ),    'softwarechk' =>     array (      'status' => 'off',      'softfp' =>       array (        0 =>         array (          'softfp_oid' => 'DEFAULT_SOFTFP_VRV',          'list' => '1',          'group' => '',        ),        1 =>         array (          'softfp_oid' => 'DEFAULT_SOFTFP_TMP',          'list' => '0',          'group' => '',        ),      ),      'flag' => '',    ),  ),  'safechk' =>   array (    'macchk' =>     array (      'status' => 'on',      'flag' => '',    ),    'agentchk' =>     array (      'status' => 'on',      'flag' => '',    ),    'termchk' =>     array (      'status' => 'on',      'flag' => '',    ),    'arpchk' =>     array (      'status' => 'on',      'flag' => '',    ),    'natchk' =>     array (      'status' => 'on',      'flag' => '',    ),    'screenchk' =>     array (      'status' => 'on',      'screenpwd' => '0',      'screentime' => '0',      'flag' => '',    ),    'oschk' =>     array (      'status' => 'on',      'flag' => '',    ),  ),  'illegalchk' =>   array (    'status' => 'on',    'pact' => 'HTTP',    'ip' => '',    'port' => '',    'flag' => '',  ),  'udiskchk' =>   array (    'status' => 'on',    'action' => '0',    'flag' => '',  ),  'netcardchk' =>   array (    'status' => 'on',    'line' => '1',    'wireless' => '1',    '3g' => '1',    'vpn' => '1',    'other' => '1',    'total' => '6',    'flag' => '',  ),  'userchk' =>   array (    'status' => 'on',    'flag' => '',  ),)
登入後複製
登入後複製

php 不可能预知你的特殊需求,所以必须你自己写

$a = array(  "hardware" => array(    "status" => "on",  ),  "software" => array(    "systemchk" => array(      "status" => "on",      "system" => array(        "xp" => array(          "status" => "on",          "sp" => "1",          "KB" => "",        ),        "win7" => array(          "status" => "on",          "sp" => "1",          "KB" => "",        ),        "win8" => array(          "status" => "on",          "sp" => "1",          "KB" => "",        ),      ),    ),    "softwarechk" => array(      "status" => "off",      "softfp" => array(        0 => array(          "softfp_oid" => "DEFAULT_SOFTFP_VRV",          "list" => "1",          "group" => "",        ),        1 => array(          "softfp_oid" => "DEFAULT_SOFTFP_TMP",          "list" => "0",          "group" => "",        ),      ),    ),  ),  "safechk" => array(    "macchk" => array(      "status" => "on",    ),    "agentchk" => array(      "status" => "on",    ),    "termchk" => array(      "status" => "on",    ),    "arpchk" => array(      "status" => "on",    ),    "natchk" => array(      "status" => "on",    ),    "screenchk" => array(      "status" => "on",      "screenpwd" => "0",      "screentime" => "0",    ),    "oschk" => array(      "status" => "on",    ),  ),  "illegalchk" => array(    "status" => "on",    "pact" => "HTTP",    "ip" => "",    "port" =>  "",  ),  "udiskchk" => array(    "status" => "on",    "action" => "0",  ),  "netcardchk" => array(    "status" => "on",    "line" => "1",    "wireless" => "1",    "3g" => "1",    "vpn" => "1",    "other" => "1",    "total" => "6",  ),  "userchk" => array(    "status" => "on",  ),);function addflag(&$ar) {  if(! is_array($ar)) return;$ar;  if(isset($ar['status'])) $ar['flag'] = '';  foreach($ar as &$v)  addflag($v);  return $ar;}var_export(addflag($a));
登入後複製
登入後複製
array (  'hardware' =>   array (    'status' => 'on',    'flag' => '',  ),  'software' =>   array (    'systemchk' =>     array (      'status' => 'on',      'system' =>       array (        'xp' =>         array (          'status' => 'on',          'sp' => '1',          'KB' => '',          'flag' => '',        ),        'win7' =>         array (          'status' => 'on',          'sp' => '1',          'KB' => '',          'flag' => '',        ),        'win8' =>         array (          'status' => 'on',          'sp' => '1',          'KB' => '',          'flag' => '',        ),      ),      'flag' => '',    ),    'softwarechk' =>     array (      'status' => 'off',      'softfp' =>       array (        0 =>         array (          'softfp_oid' => 'DEFAULT_SOFTFP_VRV',          'list' => '1',          'group' => '',        ),        1 =>         array (          'softfp_oid' => 'DEFAULT_SOFTFP_TMP',          'list' => '0',          'group' => '',        ),      ),      'flag' => '',    ),  ),  'safechk' =>   array (    'macchk' =>     array (      'status' => 'on',      'flag' => '',    ),    'agentchk' =>     array (      'status' => 'on',      'flag' => '',    ),    'termchk' =>     array (      'status' => 'on',      'flag' => '',    ),    'arpchk' =>     array (      'status' => 'on',      'flag' => '',    ),    'natchk' =>     array (      'status' => 'on',      'flag' => '',    ),    'screenchk' =>     array (      'status' => 'on',      'screenpwd' => '0',      'screentime' => '0',      'flag' => '',    ),    'oschk' =>     array (      'status' => 'on',      'flag' => '',    ),  ),  'illegalchk' =>   array (    'status' => 'on',    'pact' => 'HTTP',    'ip' => '',    'port' => '',    'flag' => '',  ),  'udiskchk' =>   array (    'status' => 'on',    'action' => '0',    'flag' => '',  ),  'netcardchk' =>   array (    'status' => 'on',    'line' => '1',    'wireless' => '1',    '3g' => '1',    'vpn' => '1',    'other' => '1',    'total' => '6',    'flag' => '',  ),  'userchk' =>   array (    'status' => 'on',    'flag' => '',  ),)
登入後複製
登入後複製


我写的代码,比你多了好多行,?死了,结贴,给分。。。
本網站聲明
本文內容由網友自願投稿,版權歸原作者所有。本站不承擔相應的法律責任。如發現涉嫌抄襲或侵權的內容,請聯絡admin@php.cn

熱AI工具

Undresser.AI Undress

Undresser.AI Undress

人工智慧驅動的應用程序,用於創建逼真的裸體照片

AI Clothes Remover

AI Clothes Remover

用於從照片中去除衣服的線上人工智慧工具。

Undress AI Tool

Undress AI Tool

免費脫衣圖片

Clothoff.io

Clothoff.io

AI脫衣器

AI Hentai Generator

AI Hentai Generator

免費產生 AI 無盡。

熱門文章

R.E.P.O.能量晶體解釋及其做什麼(黃色晶體)
2 週前 By 尊渡假赌尊渡假赌尊渡假赌
倉庫:如何復興隊友
4 週前 By 尊渡假赌尊渡假赌尊渡假赌
Hello Kitty Island冒險:如何獲得巨型種子
3 週前 By 尊渡假赌尊渡假赌尊渡假赌

熱工具

記事本++7.3.1

記事本++7.3.1

好用且免費的程式碼編輯器

SublimeText3漢化版

SublimeText3漢化版

中文版,非常好用

禪工作室 13.0.1

禪工作室 13.0.1

強大的PHP整合開發環境

Dreamweaver CS6

Dreamweaver CS6

視覺化網頁開發工具

SublimeText3 Mac版

SublimeText3 Mac版

神級程式碼編輯軟體(SublimeText3)

如何使用 foreach 迴圈移除 PHP 陣列中的重複元素? 如何使用 foreach 迴圈移除 PHP 陣列中的重複元素? Apr 27, 2024 am 11:33 AM

如何使用 foreach 迴圈移除 PHP 陣列中的重複元素?

PHP 陣列鍵值翻轉:不同方法的效能比較分析 PHP 陣列鍵值翻轉:不同方法的效能比較分析 May 03, 2024 pm 09:03 PM

PHP 陣列鍵值翻轉:不同方法的效能比較分析

PHP數組多維排序實戰:從簡單到複雜場景 PHP數組多維排序實戰:從簡單到複雜場景 Apr 29, 2024 pm 09:12 PM

PHP數組多維排序實戰:從簡單到複雜場景

PHP數組深度複製的藝術:使用不同方法完美複製 PHP數組深度複製的藝術:使用不同方法完美複製 May 01, 2024 pm 12:30 PM

PHP數組深度複製的藝術:使用不同方法完美複製

深度複製PHP數組的最佳實踐:探索高效的方法 深度複製PHP數組的最佳實踐:探索高效的方法 Apr 30, 2024 pm 03:42 PM

深度複製PHP數組的最佳實踐:探索高效的方法

PHP 數組分組函數在資料整理的應用 PHP 數組分組函數在資料整理的應用 May 04, 2024 pm 01:03 PM

PHP 數組分組函數在資料整理的應用

PHP 陣列分組函數在尋找重複元素中的作用 PHP 陣列分組函數在尋找重複元素中的作用 May 05, 2024 am 09:21 AM

PHP 陣列分組函數在尋找重複元素中的作用

PHP 數組合併去重演算法:平行的解決方案 PHP 數組合併去重演算法:平行的解決方案 Apr 18, 2024 pm 02:30 PM

PHP 數組合併去重演算法:平行的解決方案

See all articles