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.

2
  • Does this stackoverflow.com/questions/25726512/… help you? Commented Jan 24, 2017 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. Commented Jan 24, 2017 at 10:11

2 Answers 2

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
}

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

3 Comments

This works fine! But my problem is that ii displays value4 value7 in different rows and not value4, value7
push these values in array & then implode it... then it will give u value4, value7
Perfect! And with a counter and an if inside the second foreach I did it! Thank you very much
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);



        }

    ?>

Comments

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.