0

I am using Laravel .if i dd() $arrBody I get following result dd($arrBody);

    array:7[▼
            "first_name" => "john"
            "last_name" => "doe"
            "email_address" => "[email protected]",
            "age"=> 25,
            "date"=>"2021-07-9",
            "country"=>"USA",
            "code"=>"3045"
]

Now I want to get email,firstname and last name from $arryBody and assigned them to email,first_name and last_name key. But rest of keys like age, country,state and date i want them to go in custom_fields array but its like a hardcoded here. since I am displaying age, date,country one by one. My array may have more key/values so I want to make custom_fields array dynamic. I want field_name inside inside custom_fields to have a same name that comes after email,first_name and last_name of $arrBody array instead of manually writing field_name and want to assign that keys value to "value"

$data = [
        "subscribers"=>[
            [
                "email"=> $arrBody['email'],
                "first_name"=> $arrBody['first_name'],
                "last_name"=> $arrBody['last_name'],
                "subscribed"=> true,
                
                "custom_fields"=>[
                    [ 
                        "field_name"=> "age",
                        "value_type"=> "text",
                        "value"=> array_key_exists('age',$arrBody) ? $arrBody['age']:''
                    ],
                    [ 
                        "field_name"=> "country",
                        "value_type"=> "text",
                        "value"=> array_key_exists('country',$arrBody) ? $arrBody['country']:''
                    ],
                    [ 
                        "field_name"=> "date",
                        "value_type"=> "text",
                        "value"=> array_key_exists('date',$arrBody) ? $arrBody['date']:''
                    ],
                    //so on...
                ]
            ]
        ]
    ];

In Laravel I can use

$main = ['email', 'first_name', 'last_name'];

$subscriber = Arr::only($arrBody, $main);

$custom = Arr::exclude($arrBody, $main);

Now I want this $custom array inside "custom_fields"=>[] dynamically until the length of $custom array instead of checking if it has age, country etc or not. something like this if possible

custom_fields" => [
                        [
                            "field_name" => array_keys($custom)
                            "value_type" => "text",
                            "value" => array_values($custom)
                        ],
            //go until the end of $custom array
];

1 Answer 1

1
$data = [
    "subscribers"=>[
        [
            "email"=> $arrBody['email'],
            "first_name"=> $arrBody['first_name'],
            "last_name"=> $arrBody['last_name'],
            "subscribed"=> true,
            "custom_fields"=> collect($custom)->map(function($value, $key) {
                return [ 
                    'field_name' => $key,
                    'value_type' => gettype($value),
                    'value' => $value,
                ]; 
            }),
        ]
    ]
];

add ->toArray() after if you want it back to an array over a collection,

Also gettype wont work unless you actually use those types, not a number in a string format.

Sign up to request clarification or add additional context in comments.

2 Comments

thanks, do I need to do ->toArray(), I want array of data inside custom_fields=>[]
Give it a go and find out.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.