数组到关联数组

时间:2017-01-11 11:24:21

标签: php arrays associative

我有一个这样的数组:

array(3) {
  [0]=>
  array(2) {
    [0]=>
    string(6) "action"
    [1]=>
    string(5) "depot"
  }
  [1]=>
  array(2) {
    [0]=>
    string(9) "Demandeur"
    [1]=>
    string(11) "Particulier"
  }
  [2]=>
  array(2) {
    [0]=>
    string(3) "Nom"
    [1]=>
    string(6) "Cheval"
  }

但是我想要一个关联数组,像这样访问它:$ _REQUEST ['Nom']并且返回'Cheval'等......

第一个数组实际上是一个用jQuery创建的对象,这是他的构造:

jQuery('input:submit').click(function() {
    var itemMetaArray = {};
    jQuery('.frm_pro_form :input:not(:hidden, :submit)').each(function() {
        if(jQuery('#field_demande').val() != undefined){
            itemMetaArray['action'] = jQuery('#field_demande').val();
        } 
        else itemMetaArray['action'] = jQuery('#field_demande2').val();
        var label = jQuery(this).closest('.frm_form_field').find('.frm_primary_label').text().trim();
        if(jQuery(this).attr('name').indexOf("file") == -1 ) {
            itemMetaArray[label] = jQuery(this).val();
        }else{
              var fileName = jQuery(this).attr('name');
              fileName = fileName.substring(0,fileName.length-2);
              itemMetaArray[label] = fileName;
        }
});

在此之后,我在PHP中进行一些解析:

$dataInfo = explode(",",$_POST['dataInfo']);
for ($i = 0; $i < count($dataInfo); ++$i){
    $tmpWithoutBrackets[$i] = trim($dataInfo[$i],"{..}");
    $tmpWithoutColon[$i] = explode(":",$tmpWithoutBrackets[$i]);
    for($j = 0; $j < (count($tmpWithoutColon) + 1); ++$j){
        $arrayFinal[$i][$j] = trim($tmpWithoutColon[$i][$j],"\"");
    }
    $arrayFinal[$i] = array_diff($arrayFinal[$i],array(""));
}

$ arrayFinal与arrayInfo

是相同的数组

提前感谢您的帮助

1 个答案:

答案 0 :(得分:2)

您需要像这样修改数组:

$test = array('arrayInfo' => array(array('Nom','Cheval'),array('Prenom','Nathan'),array('Adresse postale','4 impasse Molinas')),'arrayFile' => array(array ('application/pdf','3a514fdbd3ca2af35bf8e5b9c3c80d17','JVBERi0xLjUNCiW1tbW1DQoxIDAgb2JqDQo8PC9UeXBlL0NhdGFsb2cvUGFnZXMgMiAwIFIvTGFuZyhmci1GUikgL1N0cnVjdFRyZWVSb290IDI2IDAgUi9NYXJrSW5mbzw8L01hcmtlZCB0cnVlPj4vTWV0')));

$result = array();
foreach($test as $k=>$t){
    $count = 0;
    $_key = '';
    $_value = '';
    foreach($t as $f){
        foreach($f as $final){
            $count++;
            if($count%2!==0){
                $_key = $final;
            }
            else{
                $_value = $final;
            }
            if($_value!==''){
                $result[$k][$_key] = $_value;
                $_value = '';
            }
        }
    }
}

echo $result['arrayInfo']['Nom'];//Cheval

对于你修改过的问题,答案会有所改变:

    $test = array(array('Nom','Cheval'),array('Prenom','Nathan'),array('Adresse postale','4 impasse Molinas'));

$result = array();
foreach($test as $k=>$t){
    $count = 0;
    $_key = '';
    $_value = '';
    foreach($t as $final){
        $count++;
        if($count%2!==0){
            $_key = $final;
        }
        else{
            $_value = $final;
        }
        if($_value!==''){
            $result[$_key] = $_value;
            $_value = '';
        }
    }
}
var_dump($result);
echo $result['Nom'];//Cheval

我希望它有所帮助