将PHP数组作为对象存储在API中

时间:2019-04-11 07:46:21

标签: php arrays api

我正在使用PHP Laravel构建自定义项目。我已经从表单捕获了一些输入,并希望将它们存储在API中。基本上,API接受对象数组。我正在尝试存储我的输入,但是它不会保存所有输入,而只会保存第一个输入。

API的要求

[
    {
        "dependant_name": "Dependant 1 ",
        "relationship": "Spouse",
        "date_of_birth": "1999-02-26"    
    },
      {
        "dependant_name": "Dependant 2",
        "relationship": "Child",
        "date_of_birth": "2008-11-21"    
    },
    {
        "dependant_name": "Dependant 3",
        "relationship": "Child",
        "date_of_birth": "2015-10-17"    
    }
]

从表单收集后的输入

$fn = [
  "fName1" => "Martoo",
  "fName2" => "dsjksdksd",
  "sName1" => "Njogu",
  "sName2" => "jkdsjsdk",
  "dob" => "07-04-2001",
  "dob1" => "06-04-2001",
  "relation1" => "Wife",
  "relation2" => "Son"
];

我的代码以检查密钥是否存在

 //If fName exist create only one array collection to suit API
    if(array_key_exists("fName1" , $fn)){ 
        $a=[
            'dependant_name' => $fn['fName1'] . " " . $fn['sName1'],
            'relationship' => $fn['relation1'],
            'date_of_birth' => $fn['dob']
        ];
    }

    //If fName and fName2 exists create 2 array collections to suit API
    if(array_key_exists("fName1" , $fn) && array_key_exists("fName2" , $fn)){ 
        $a=[
            'dependant_name' => $fn['fName1'] . " " . $fn['sName1'],
            'relationship' => $fn['relation1'],
            'date_of_birth' => $fn['dob'],

            'dependant_name' => $fn['fName2'] . " " . $fn['sName2'],
            'relationship' => $fn['relation2'],
            'date_of_birth' => $fn['dob1']
        ];
    }

   dd($a);

在dd($ a)之后,我只得到第一个数组集合

2 个答案:

答案 0 :(得分:1)

您需要按以下方式更改$a的结构

if (array_key_exists("fName1", $fn)) {
    $a[] = [ // you were replacing $a every time here
        'dependant_name' => $fn['fName1'] . " " . $fn['sName1'],
        'relationship'   => $fn['relation1'],
        'date_of_birth'  => $fn['dob'],
    ];
}

//If fName and fName2 exists create 2 array collections to suit API
if (array_key_exists("fName1", $fn) && array_key_exists("fName2", $fn)) {
    $a[] = [ // you were replacing $a every time here
        'dependant_name' => $fn['fName1'] . " " . $fn['sName1'],
        'relationship'   => $fn['relation1'],
        'date_of_birth'  => $fn['dob'],

        'dependant_name' => $fn['fName2'] . " " . $fn['sName2'],
        'relationship'   => $fn['relation2'],
        'date_of_birth'  => $fn['dob1'],
    ];
}

dd($a);

答案 1 :(得分:0)

您需要使用数组存储输出。您可以使用以下代码。

$fn = [
  "fName1" => "Martoo",
  "fName2" => "dsjksdksd",
  "sName1" => "Njogu",
  "sName2" => "jkdsjsdk",
  "dob" => "07-04-2001",
  "dob1" => "06-04-2001",
  "relation1" => "Wife",
  "relation2" => "Son"
];
$a = array();
//If fName exist create only one array collection to suit API
if (array_key_exists("fName1", $fn)) {
  $a[] = array(
    'dependant_name' => $fn['fName1'] . " " . $fn['sName1'],
    'relationship' => $fn['relation1'],
    'date_of_birth' => $fn['dob']
  );
}

//If fName and fName2 exists create 2 array collections to suit API
if (array_key_exists("fName1", $fn) && array_key_exists("fName2", $fn)) {
  $a[] = array(
    'dependant_name' => $fn['fName1'] . " " . $fn['sName1'],
    'relationship' => $fn['relation1'],
    'date_of_birth' => $fn['dob']
  );

  $a[] = array('dependant_name' => $fn['fName2'] . " " . $fn['sName2'],
    'relationship' => $fn['relation2'],
    'date_of_birth' => $fn['dob1']
  );
}
echo "<pre>";
print_r($a);