0

Let us say I have a set of data of the first name

first name : `{ "abc","bvd","jhhg", "jju","jju"}`
Last name : `{ "hhh","uuu","tre", "vvg","yyy"}`
Age : `{ "44","33","34", "34,"65"}`

I want the result to return as

{
"fname" : "abc",
"lname" : "hhh",
"age" : "44"
},
{
"fname" : "bvd",
"lname" : "uuu",
"age" : "33"
},
{
"fname" : "jhhg",
"lname" : "tre",
"age" : "34"
}

How to do it in php / laravel

I have achieved the result but the response is too slow

  $cart = array();

   for($i = 1;$i<=($qty);$i++)
   {

       $fnames = CreateFirstName::all()->random(1);

       $fname = ($fnames[0]->fname);

       $images = CreateImageName::all()->random(1);

       $image = ($images[0]->imagelocation);


       $lnames = CreateLastName::all()->random(1);
       $lname = ($lnames[0]->lname);

       $cart[] = [
           'id' => $images[0]->id,
           'fname' => mb_substr($fname, 0, 10),
           'lname' => mb_substr($lname, 0, 10),
           'age' => rand(18,43),
           'city' => $region_name,
           'image' => $image

       ];

   }


   $collection = collect($cart);

image

The response is coming more than 20000 ms on the server or local enviroment

3
  • 2
    "how to do it in php / laravel" Research on the internet, start coding, ask for help if you run into any errors. Stack Overflow is not a code writing service. We are always glad to help and support new coders but you need to help yourself first. You are expected to try to write the code yourself. Please read How to create a Minimal, Reproducible Example and How do I ask a good question?.
    – brombeer
    Jan 12, 2020 at 8:45
  • Also, please add a little more detail: should only the first three datasets be returned? Or is jju somehow not permitted to be included?
    – brombeer
    Jan 12, 2020 at 8:52
  • @kobid, do you need just 3 elements or all of them to returned in the way you have shared in expected output? Jan 12, 2020 at 8:59

2 Answers 2

1

As an alternative to the accepted answer you could also use array_map.

array_map will essentially take one or more arrays and iterate over them by passing the current iteration of each array to the callback function:

$people = array_map(function ($fname, $lname, $age) {
    return [
        'fname' => $fname,
        'lname' => $lname,
        'age'   => $age,
    ];
}, $firstNames, $lastNames, $ages);

You could then make it even shorter by using compact

$people = array_map(function ($fname, $lname, $age) {
    return compact('fname', 'lname', 'age');
}, $firstNames, $lastNames, $ages);
0
0

I assume the size of all the 3 arrays is same

$finalArray = array();
for($i=0;$i<count($firstArray);$i++){
    $finalArray[] = array("fname"=>$firstArray[$i], "lname"=>$lastArray[$i],"age"=>$ageArray[$i]);
} 

$finalArray has what you have.

2
  • That will not return OP's desired result though, but all elements from the arrays
    – brombeer
    Jan 12, 2020 at 8:54
  • The "I want the result to return as" part still only has three elements
    – brombeer
    Jan 12, 2020 at 8:57

Your Answer

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

Not the answer you're looking for? Browse other questions tagged or ask your own question.