I have an array with this structure:
array(4) {
[0]=> array(x) {
[0]=> string(x)"London"
[1]=> string(xx) "John"}
[1]=> array(x) {
[0]=> string(x)"London"
[1]=> string(xx) "Charles"}
[2]=> array(x) {
[0]=> string(x)"Paris"
[1]=> string(xx) "Alan"}
[3]=> array(x) {
[0]=> string(x)"Paris"
[1]=> string(x) "Wayne"}
}
How can I display the array data in this order:
London
John | Charles
Paris
Alan | Wayne
This can be done with an unique multidimensional array? or should i have another array to select distinct cities?
I am talking about something like a loop with:
foreach ($cities as $city) {
echo '<div class="city">'.$city.'</div>';
for() {
echo '<div class="persons">'.$persons.'</div>';
}
}
thanks
Group your cities in a new array, then spit it out
$newCities = array();
foreach ($cities as $city) {
$newCities[$city[0]][] = $city[1];
}
foreach($newCities as $city => $persons) {
echo '<div class="city">' . $city . '</div>';
// use join to glue the pieces together
echo '<div class="persons">' . join(' | ', $persons) . '</div>';
}
It might be convenient to create a new associative array as an intermediate step. This method doesn't care if the cities are originally grouped or not.
$cities = array();
foreach ($original_array as $entry) {
$cities[$entry[0]][] = $entry[1];
}
The new array will contain city names as keys, and an array of people's names as corresponding values for each city. Like:
array(2) {
["London"]=> array(2) {
[0]=> "John"
[1]=> "Charles" }
["Paris"]=> array(2) {
[0]=> "Alan"
[1]=> "Wayne" }
}
So now you can do
foreach ($cities as $city_name => $people) {
echo '<div class="city">'.$city_name.'</div>';
foreach ($people as $person) {
echo '<div class="person">'.$person.'</div>';
}
}
I think it may help
foreach ($cities as $city) {
echo '<div class="city">'.$city.'</div>';
foreach ($cities as $city1) {
if ($city==$city1){
echo '<div class="persons">'.$persons.'</div>';
}
}
}
Since cities are always grouped as you specified, you only need one extra variable to do this properly. Assuming your input array is called $people
:
$last_city = '';
foreach ($people as $person) {
if ($person[0] != $last_city) {
if ($last_city != '') echo '</div>'; // Close opened persons block
$last_city = $person[0];
echo '<div class="city">' . $last_city . '</div>';
echo '<div class="persons">';
}
else echo ' | ';
echo $person[1];
}
echo '</div>'; // This assumes there's at least one record in $people
No extra associative array required. Note that this may be more semantic markup:
<div class="city">
<span>London</span>
John | Charles
</div>
In that case the above code would be:
$last_city = '';
foreach ($people as $person) {
if ($person[0] != $last_city) {
if ($last_city != '') echo '</div>'; // Close opened city block
$last_city = $person[0];
echo '<div class="city">';
echo '<span>' . $last_city . '</span>';
}
else echo ' | ';
echo $person[1];
}
echo '</div>'; // This assumes there's at least one record in $people
Try this
$array = array(
array('0' => array('0' => 'London', '1' => 'John')),
array('1' => array('0' => 'London', '1' => 'Charles')),
array('2' => array('0' => 'Paris', '1' => 'Alan')),
array('3' => array('0' => 'Paris', '1' => 'Wayne'))
);
$i = 0;
foreach ($array as $arr1) {
foreach ($arr1 as $arr2) {
$cities[$arr2[0]][$i] = array($arr2[0],$arr2[1]);
$i++;
}
}
foreach ($cities as $key => $city) {
echo '<h2 class="city">'.$key.'</h2>';
foreach($city as $persons) {
echo '<div class="persons">'.$persons[1].'</div>';
}
echo '<br/>';
}