0

I have a problem listed below:

I handle 3 arrays from client side. They always have random length, but by their length they are equal to each other. Then I use $summary_array to gather all data and iterate through.

Example:

$country = (1,2,3);
$city = (4,5,6);
$sightseen = (7,8,9);

$summary_array = ($country, $city, $sightseen);

OR

$country = (1,2,3,4);
$city = (1,3,4,5);
$sightseen = (5,2,9,4);

$summary_array = ($country, $city, $sightseen);

And now I need to iterate through these arrays:

foreach($summary_array as $value) {
    //...
}

And I need to get in the output:

1 : 1 2 3
2 : 4 5 6
3 : 7 8 9

OR

1 : 1 2 3 4
2 : 1 3 4 5
3 : 5 2 9 4

How I can do that?

1
  • Sorry, I was wrong!!!! I receive my values from client side like this: $country = (1, 2, 3); $city = (4, 5, 6); $sightseen = (7, 8, 9); So I need to obtain values which contain one value from each other: 0: 1 4 7 1: 2 5 8 2: 3 6 9 Commented May 11, 2015 at 8:37

1 Answer 1

3

Besides that you missed the keyword arrayin every array declaration, you can simply implode() each array in every iteration like this:

foreach($summary_array as $k => $value) {
    echo ($k+1) . " : " . implode(" ", $value) . "<br>";
}

output:

1 : 1 2 3  //1 : 1 2 3 4
2 : 4 5 6  //2 : 1 3 4 5
3 : 7 8 9  //3 : 5 2 9 4

EDIT:

To rotate your array, just simply use this before your foreach loop:

$summary_array = call_user_func_array("array_map", array(NULL, $country, $city, $sightseen));
Sign up to request clarification or add additional context in comments.

4 Comments

I'm sorry, but there is one more question)) How I can implement a SQL query from data I got? for example: foreach( $option as $k => $value) { INSERT INTO table name (country_id, city_id, sightseen_id) VALUES ($country, $city, $sightseen); Exatly I mean 3 query from arrays: INSERT INTO table name (country_id, city_id, sightseen_id) VALUES (1, 4, 7); INSERT INTO table name (country_id, city_id, sightseen_id) VALUES (2, 5, 8); INSERT INTO table name (country_id, city_id, sightseen_id) VALUES (3, 6, 9); ???
@user2642396 Just implode the value in the string like this: echo $sql = "INSERT INTO table name (country_id, city_id, sightseen_id) VALUES (" . implode(", ", $value) . ")"; (<- Use this in the foreach loop)
Sorry))) One more))) And what if I need to perform a select? SELECT * FROM tablename WHERE country_id=$A AND city_id=$B AND sightseen_id=$C; ???
@user2642396 I think this goes slowly a bit too broad, the comment section is not there for answering questions. Please try at least a few times and if you have a new problem you can ask a new question.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.