0

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.

Aman Rawat
  • 2,625
  • 1
  • 25
  • 40
arek
  • 1
  • 6
  • Does this http://stackoverflow.com/questions/25726512/how-to-insert-dynamic-multidimensional-array-in-database-with-mysqli help you? – SuperDJ Jan 24 '17 at 10:05
  • Yes, although my initial problem is not how I will insert the columns into the database but how I'll get the specific values that I want from the array. Such as the "100,101" and "value4, value7" that I'm writing above. – arek Jan 24 '17 at 10:11

2 Answers2

0
<?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
}

?>
Ashu
  • 1,320
  • 2
  • 10
  • 24
  • This works fine! But my problem is that ii displays value4 value7 in different rows and not value4, value7 – arek Jan 24 '17 at 10:53
  • push these values in array & then implode it... then it will give u value4, value7 – Ashu Jan 24 '17 at 10:55
  • Perfect! And with a counter and an if inside the second foreach I did it! Thank you very much – arek Jan 24 '17 at 22:04
0

I think this what you need exactly . loop through the array and use implode() end() like this .

    <?php



        foreach($your_array as $key=>$val)
        {
           $ids ='';
           $new_cat_label='';

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


           foreach($val['category_labels'] as $val1)
           {
                if(!empty($new_cat_label))
                {

                    $new_cat_label.= " ,".end($val1);

                }
                else
                {

                   $new_cat_label.= end($val1);
                }

           }


          echo $ids."  <br>";
          echo $new_cat_label."  <br>";

           //here insert query 

          // insert into table_name (col1,col2)values($ids,$new_cat_label);



        }

    ?>
JYoThI
  • 11,977
  • 1
  • 11
  • 26