phparraysassociative

Use two columns from a 2d array to form an associative array


I have an array like this :

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

But I want an associative array, to access it like this : $_REQUEST['Nom'] and have in return 'Cheval' etc...

The first array is actually an object, created with jQuery and here is his construction :

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;
        }
});

After this, I do some parsing in 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 is the same array as arrayInfo

Thanks in advance for your help


Solution

  • You need to modify your array like this:

    $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
    

    For your modified question, answer will be a little changed ofcourse:

        $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
    

    I hope it helps