Tutorial, multi-dimensional array processing

Source: Internet
Author: User
I have a multi-dimensional array with the following structure:

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"  }}

I want to add a flag element in the array with the same level as the status element. that is to say, as long as there is a status, the flag element matches it. may I ask, PHP has such a function, can this function be implemented?


Reply to discussion (solution)

Php cannot predict your special needs, so you must write

$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 cannot predict your special needs, so you must write

$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' => '',  ),)


I wrote a lot more code than you ,? Dead, close the post, and score...

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.