Monday, 27 August 2018

PHP: insert a multidimensional array into the mysql database, only specific values advertisements

I have a multidimensional array like this

Array =>
    [0] => Array
            (
                [address] => Zaa 6
                [category_ids] => Array
                    (
                        [0] => 100
                        [1] => 101
                    )

                [category_labels] => Array
                    (
                        [0] => Array
                            (
                                [0] => value1
                                [1] => value2
                                [2] => value3
                                [3] => value4
                         )

                        [1] => Array
                            (
                                [0] => value5
                                [1] => value6
                                [2] => value7
                            )

                    )

                [city] => gg
                [lat] => 37.964652
                [lng] => 23.708208
                [name] => New Place

            [1]=> Array the same as above

Every array is a record.

And I want to put all the elements in a mysql database. But in the column category_ids I want to insert "100, 101" and in category_labels I want to put only the last values of each array such as "value4, value7". How can I do this? I know that I can use end(), count() and implode() but I don't know how exactly.



<?php
$data=array(array(
                "address" => "Zaa",
                "category_ids" => array(100,101),

                "category_labels" => array(array("value1","value2","value3","value4"),array("value5","value6","value7")),
                "city" => "gg",
                "lat" => "37.964652",
                "lng" => "23.708208",
                "name" => "New Place"));

foreach($data as $row)
{
    echo $row['address']."<br>";

    $ids=implode(",",$row['category_ids']);

    echo $ids."<br>";

    $l_array=array();
    foreach($row["category_labels"] as $cat_label)
    {
       $count_l=count($cat_label);
        array_push($l_array,$cat_label[$count_l -1]);
    }
    echo implode(",",$l_array)."<br>";

    echo $row['city']."<br>";
    echo $row['lat']."<br>";
    echo $row['lng']."<br>";
    echo $row['name']."<br>";

    //write your query here by passing above parameters
}

?>

0 comments:

Post a Comment