Как получить значение 2-го массива совпадающего ключа первого массива - PullRequest
0 голосов
/ 27 октября 2019

У меня есть 2 массива: сопоставленные данные:

Array
(
    [0] => Array
        (
            [id] => 
        )

    [1] => Array
        (
            [country_code] => 
        )

    [2] => Array
        (
            [user_id] => 
        )

    [3] => Array
        (
            [category_id] => 
        )

    [4] => Array
        (
            [post_type_id] => 
        )

    [5] => Array
        (
            [make] => make
        )

    [6] => Array
        (
            [description] => description
        )
}

Теперь у меня есть другой массив, который является моими данными CSV:

Array
(
    [0] => Array
        (
            [status] => used
            [type] => Trailer
            [category] => Storage
            [location] => Calgary AB
            [make] => Strick
            [model] => Storage Van
            [year] => 1997
            [last_update] => 8/30/2019
            [unit_number] => VE420900U
            [vin] => 1S11E8283VE420900
            [price] => 2900
            [currency] => C
            [body_style] => Storage
            [colour] => Grey
            [suspension] => Spring Ride
            [suspension_type] => 
            [axle_config] => Single
            [trailer_length] => 28
            [width] => 0
            [height] => 0
            [description] => Storage,  suspension, Single axle, Steel rims, Wood floor, Roll up rear door rear door, Length: 28ft
            [main_photo] => https://www.maximinc.com/images/trailer/1997-strick-storage-van-40192.jpg
            [thumbnail] => https://www.maximinc.com/images/trailer/t-1997-strick-storage-van-40192.jpg
            [main_photo_date] => 10/18/2017 21:57
            [url] => View Online
        )

    [1] => Array
        (
            [status] => used
            [type] => Trailer
            [category] => Storage
            [location] => Calgary AB
            [make] => Roussy
            [model] => Storage Van
            [year] => 1987
            [last_update] => 8/30/2019
            [unit_number] => H1004175U
            [vin] => 2R183M3C1H1004175
            [price] => 4900
            [currency] => C
            [body_style] => Storage
            [colour] => White
            [suspension] => Spring Ride
            [suspension_type] => 
            [axle_config] => Tandem
            [trailer_length] => 48
            [width] => 102
            [height] => 0
            [description] => Storage,  suspension, Tandem axle, Steel rims, Wood floor, Swing rear door, Width: 102in, Length: 48ft
            [main_photo] => https://www.maximinc.com/images/trailer/1987-roussy-storage-van-40238.jpg
            [thumbnail] => https://www.maximinc.com/images/trailer/t-1987-roussy-storage-van-40238.jpg
            [main_photo_date] => 10/18/2017 22:07
            [url] => View Online
        )
)

Теперь я хочу взять значения массивов CSV, если CSVключ массива совпадает с Mapped Data Value.

Я создал логику и попробовал array_key_exists, но не смог, я пробую этот код моей логики, но для этого он вставляет ложные записи в dbs

Если совпадаетМне нужен окончательный массив типа

Array
( 
  [0]=> Array
     (
       [description] =>Storage,  suspension, Single axle, Steel rims, Wood floor, Roll up rear door rear door, Length: 28ft
       [make] => Strick
     )
  [1]=> Array
     (
       [description] =>Storage,  suspension, Tandem axle, Steel rims, Wood floor, Swing rear door, Width: 102in, Length: 48ft
       [make] => Roussy
     )
)

Вот моя логика,

foreach ($json_decode as $key => $value) 
            {
                /*if(!empty($value))
                {
                    $jsonvalue = (array)$value;
                    foreach ($jsonvalue as $jkey=> $jvalue) 
                    {
                        foreach ($mapped_data as $mkey => $mvalue) 
                        {
                            $mvalue = (array)$mvalue;
                            foreach ($mvalue as $mappedkey=> $mappedvalue) 
                            {
                                if($jkey == $mappedvalue)
                                {
                                    if(!empty($jvalue))
                                    {
                                        // echo "Matched one====".'<br>';
                                        // print_r($jkey).'<br>';   
                                        // print_r($mappedvalue).'<br>';    
                                        // print_r($jvalue).'<br>';

                                        $post->$mappedvalue = $jvalue;
                                        $post->title = 'test';
                                        $post->category_id = '0';
                                        $post->city_id = '0';
                                        //$post->save();
                                    }   
                                }
                            }
                        }

                    }
                }

Где мой $json_decode - это мой массив данных CSV.

Может кто-нибудь мне помочьиз

1 Ответ

1 голос
/ 27 октября 2019

Полный упрощенный скрипт для вашей задачи:

// an example of mapped array
$mapped = [
    [
        'key1' => false,
    ],
    [
        'key2' => 'value',
    ],
];

// now you need to flat your array so as to use advantages of flat arrays
// `array_filter` will also remove keys with empty values.
$flatMapped = array_filter(array_merge(...$mapped));

// here you see that you have a flat array, and not array of arrays
print_r($flatMapped);

// sample csv data
$csvData = [
    [
        'key1' => 42,
        'key2' => 142,
        'key3' => 242,
    ],
    [
        'key1' => 342,
        'key2' => 442,
        'key3' => 542,
    ],
];

// here I modify $csvData array in place, but you 
// can create new array and put values there
foreach ($csvData as &$item) {
    // `array_intersect_key` returns keys with values from 
    // first array which also exist in second array
    $item = array_intersect_key($item, $flatMapped);
}
print_r($csvData);

Демо здесь .

...