0

I have 2 arrays: Mapped Data:

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
        )
}

Now I have another array which is my CSV Data:

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
        )
)

Now I want to take CSV Arrays values if CSV arrays key is matching with Mapped Data Value.

I created logic and try array_key_exists but failed, I try this code of my logic but what it does it is inserting false entries into dbs

If matched I want an array final like

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
     )
)

Here is my logic,

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();
                                    }   
                                }
                            }
                        }

                    }
                }

Where my $json_decode is my CSV Data array.

Can anyone help me out?

1 Answer 1

1

Full simplified script for your task:

// 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);

Demo here.

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

1 Comment

Perfect! can you explain me a bit how this works?

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.