Welcome to OStack Knowledge Sharing Community for programmer and developer-Open, Learning and Share
Welcome To Ask or Share your Answers For Others

Categories

0 votes
315 views
in Technique[技术] by (71.8m points)

php - how to map result value to its key value in laravel?

I have 2 arrays first is data and 2nd is result array

i am trying to map result array with data array so i used combine() method

but problem is that if i have same result then it map 1 item and skips remaining consider

first array($customersidurl)

[
    19 => null,
    20 => null,
    21 => null,
    24 => "31.4346084,74.2793016,12",
    25 => null,
    26 => "31.58834,74.37375"
]

second result array($shortest)

[
    0 => 8532.8587780495,
    1 => 8532.8587780495,
    2 => 8532.8587780495,
    3 => 18.831126689097,
    4 => 8532.8587780495,
    5 => 0.85071506409078
]

and my output is

[
    "" => 8532.8587780495,
    31.4346084,74.2793016,12 => 18.831126689097,
    31.58834,74.37375 => 0.85071506409078,
]

it skipped 3 results. I don't want that skipping to happen. The code I used is

$customersidurl = Customer::whereIn('created_by', $adminot)
    ->get()
    ->pluck('location_url', 'id');

foreach ($customersidurl as $short) {
    $shortest[] = $this->getsortedDistance($cords , $cords1 ,$short);
}

$combined = $customersidurl->combine($shortest);

can someone help me any other which can map each of elements with corresponding?

required output is

[
    "" => 8532.8587780495,
    "" => 8532.8587780495,
    "" => 8532.8587780495,
    31.4346084,74.2793016,12 => 18.831126689097,
    "" => 8532.8587780495,
    31.58834,74.37375 => 0.85071506409078
]

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome To Ask or Share your Answers For Others

1 Answer

0 votes
by (71.8m points)

try this as you are passing ids

$customersidurl = Customer::whereIn('created_by', $adminot)->get()->pluck('location_url', 'id');
      
        foreach ($customersidurl as $short){
         $shortest[] = $this->getsortedDistance($cords , $cords1 ,$short);
        }
        $customersid = Customer::whereIn('created_by', $adminot)->pluck('id')->toArray();
        for($i = 0; $i < sizeof($customersid); $i++){
            $maping[$customersid[$i]] =  $shortest[$i];
        }

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome to OStack Knowledge Sharing Community for programmer and developer-Open, Learning and Share
Click Here to Ask a Question

...