dimanche 27 octobre 2019

How to take value of 2nd array of matching key of first array

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?



via Chebli Mohamed

Aucun commentaire:

Enregistrer un commentaire