Сортировка массива


у меня есть этот массив


Array
(
    [0] => Array
        (
            [0] => Array
                (
                    [data] => 2017<br>Condenser<br>Buick Verano
                )

            [1] => Array
                (
                    [description] => COMPLETE COOLING (RAD/CON/FAN)
                )

            [2] => Array
                (
                    [part_grade] => A
                )

            [3] => Array
                (
                    [stock] => AA0818
                )

            [4] => Array
                (
                    [us_price] => $450
                )

            [5] => Array
                (
                    [dealer_info] => Auto Alex USA-CA(Sun-Valley)  Request_Quote 818-504-7600 Request_Insurance_Quote
) [6] => Array ( [dist_mile] => 2344 ) ) [1] => Array ( [0] => Array ( [data] => 2013<br>Condenser<br>Cadillac XTS ) [1] => Array ( [description] => WTH RAD AND FAN ASSM,8-12,000-44K ) [2] => Array ( [part_grade] => A ) [3] => Array ( [stock] => 217563 ) [4] => Array ( [us_price] => $400.01 ) [5] => Array ( [dealer_info] => Miami Parts USA-FL(Opa-Locka) Request_Quote 1-800-644-4788 Request_Insurance_Quote
) [6] => Array ( [dist_mile] => 9 ) )

но это не служит мне, и я хотел бы преобразовать его как следующее, Я уже пробовал, и я не получаю его, я хотел бы оставить его таким образом.


Array
(
    [0] => Array
        (

                    [data] => 2017<br>Condenser<br>Buick Verano,
                    [description] => COMPLETE COOLING (RAD/CON/FAN),
                    [part_grade] => A,
                    [stock] => AA0818,
                    [us_price] => $450,
                    [dealer_info] => Auto Alex USA-CA(Sun-Valley)  Request_Quote 818-504-7600 Request_Insurance_Quote
, [dist_mile] => 2344 ) [1] => Array ( [data] => 2013<br>Condenser<br>Cadillac XTS, [description] => WTH RAD AND FAN ASSM,8-12,000-44K, [part_grade] => A, [stock] => 217563, [us_price] => $400.01, [dealer_info] => Miami Parts USA-FL(Opa-Locka) Request_Quote 1-800-644-4788 Request_Insurance_Quote
, [dist_mile] => 9 )

Вот код функции, которая генерирует массив

public function getResults(Request $request)
{

    $params = array(
        "userDate" => $request->userDate, 
        "userModel" => $request->userModel,
        "userPart" => $request->userPart,
        "userLocation" => $request->userLocation,
        "userPreference" => $request->userPreference,
        "userZip" => $request->userZip,
        "userVIN" => "",
        "svZip" => "y",
        "userPage" => "1",
        "userInterchange" => $request->userInterchange,
        "userDate2" => "Ending Year",
        "userSearch" => "int",
        "Search Car Part Inventory.x" => "33",
        "Search Car Part Inventory.y" => "10",
    );

    // Send a GET request to: http://www.foo.com/bar
    $response = Curl::to('http://car-part.com/cgi-bin/search.cgi')
    ->withContentType('application/x-www-form-urlencoded')
    ->withData($params)
    ->post();

    if(stripos($response, "dummyVar")) {
        echo "contiene";
    } else {
        if (stripos($response, "dbPart") && stripos($response, "userInterchange=")) {
            $index = stripos($response, "<table width=\"100%\" border=\"1\" cellspacing=\"0\" cellpadding=\"4\">");
            $response = substr($response, $index);
            $index = stripos($response, "</table>");
            $response = substr($response, 0, $index + 8);
            $index = stripos($response, "</tr>");
            $response = substr($response, $index + 5);
            $index = strripos($response, "<tr>");
            $response = substr($response, 0, $index);
            $response = str_replace("<tr>", "", $response);
            $response = str_replace("<td>", "", $response);
            $response = str_replace("<td align=center>", "", $response);
            $response = str_replace("<td align=center nowrap>", "", $response);

            $response = explode("</tr>", $response);

            $temp = array();

            foreach ($response as $key => $value) {
                # code...
                $value = explode("</td>", $value);
                array_pop($value);
                array_push($temp, $value);

            }

           $response = array();

           for ($i=0; $i < count($temp); $i++) { 
                $arr = array();
                $labels = ['data', 'description', 'part_grade', 'stock', 'us_price', 'dealer_info', 'dist_mile'];
               # code...


                for ($j=0; $j < count($temp[$i]); $j++) { 
                    # code...

                    array_push($arr, [$labels[$j] => $temp[$i][$j]]);

                }
                array_push($response, $arr);

           }
           print_r($response);



        } else {
             return response()->json(['statusCode' => 404, 'statusText' => "Not Found", 'data' => null], 404);
        }
    }
}
 1
php
Author: Shaz, 2018-04-24

3 answers

Вы должны изменить формат вложенных петель следующим образом:

for ($i=0; $i < count($temp); $i++) { 
    $arr = array();
    $labels = ['data', 'description', 'part_grade', 'stock', 'us_price', 'dealer_info', 'dist_mile'];
           # code...


    for ($j=0; $j < count($temp[$i]); $j++) { 
                # code...

        $arr[$labels[$j] = $temp[$i][$j]]);

    }
    array_push($response, $arr);

}
 1
Author: alanfcm, 2018-04-24 20:47:03

Исправьте это таким образом


$response = array();
array_pop($temp);
for ($i = 0; $i < count($temp); $i++) {
    $arr = array(
        'data' => $temp[$i][0],
        'description' => $temp[$i][1],
        'part_grade' => $temp[$i][2],
        'stock' => $temp[$i][3],
        'us_price' => $temp[$i][4],
        'dealer_info' => $temp[$i][5],
        'dist_mile' => $temp[$i][6],
    );
    array_push($response, $arr);

}
 0
Author: Anthony Medina, 2018-04-24 20:57:50

Эта функция может вам пригодиться.

США array_map чтобы создать новый массив, не беспокоясь о том, какие значения имеют ключи.

/*Función que llamará array_map*/

function mapByIndex( $array ) {
    $arr=array();
    foreach ( $array as $v ){
        if ( is_array( $v ) ) {
            foreach ( $v as $key => $value ) {
                $arr[$key] = $value;
            }
        }
    }
    return $arr;
}

/*Enviamos el array original, para que lo ordene*/   
$arrNew = array_map( "mapByIndex", $arrOriginal );

Если мы сделаем тест:

print_r( $arrNew );

У нас будет следующий результат:

Array
(
    [0] => Array
        (
            [data] => 2017<br>Condenser<br>Buick Verano
            [description] => COMPLETE COOLING (RAD/CON/FAN)
            [part_grade] => A
            [stock] => AA0818
            [us_price] => $450
            [dealer_info] => Auto Alex USA-CA(Sun-Valley)  Request_Quote 818-504-7600 Request_Insurance_Quote
            [dist_mile] => 2344
        )

    [1] => Array
        (
            [data] => 2013<br>Condenser<br>Cadillac XTS
            [description] => WTH RAD AND FAN ASSM,8-12,000-44K
            [part_grade] => A
            [stock] => 217563
            [us_price] => $400.01
            [dealer_info] => Miami Parts USA-FL(Opa-Locka)  Request_Quote 1-800-644-4788 Request_Insurance_Quote
            [dist_mile] => 9
        )

)

Надеюсь, это полезно для вас.


Демо

СМ. ДЕМО В REXTESTER

 0
Author: A. Cedano, 2020-06-11 10:54:57