Break an multidimentional array into multiple columns - php

I have a multidimensional array that I want to break into 6 div columns, and I can't seem to do it so any help with this ?
Here's the array :
Array
(
[0] => Array
(
[id] => 17
[title] => White
[ref] => 24941
)
[1] => Array
(
[id] => 18
[title] => Blue
[ref] => 11395
)
[2] => Array
(
[id] => 19
[title] => Red
[ref] => 11394
)
.
.
.
and here's my foreach loop:
echo '<div class="row">';
echo '<div class="col-xs-2">';
$i = 1;
foreach ($colors as $key => $value) {
if ($i % 6 === 0) {
echo $value['title']. 'Ref: '. $value['ref']
}
echo '</div><div class="col-xs-2">';
$i++;
}
echo '</div>';
echo '</div>';
Much appreciated.

Though I don't get what you do with $all_colors = explode(",",$check_colors['value']);, this should help you out:
$item = reset($colors);
while ($item) {
echo "<div class='row'>";
for ($i = 0; $i < 6; $i++) {
if ($item)
echo "<div class='col-xs-2'>{$item['title']}Ref: {$item['ref']}</div>";
else
break;
$item = next($colors);
}
echo "</div>";
}

Related

How can I loop associative array respectively

I have following associative array:
Array
(
[0] => Array
(
[0] => 268
)
[1] => Array
(
[0] => 125
[1] => 258
[2] => 658
[3] => 128
[4] => 987
)
[2] => Array
(
[0] => 123
[1] => 258
)
[3] => Array
(
[0] => 168
)
)
I need the following result as string.
268
125258658128987
123258
168
What I have tried so far;
<?php
//consider my array is in $array variable
for ($i = 0; $i < count($array); $i++)
{
foreach ($array[$i] as $res)
{
echo $res . '<br/>';
}
}
?>
But unfortunately I am getting each numbers in a new line.
Any suggestion will be appreciated.
You have to echo the <br /> outside the foreach loop:
for ($i = 0; $i < count($array); $i++)
{
foreach ($array[$i] as $res) {
echo $res;
}
echo '<br />'; //<-- Put this outside the foreach loop
}
Or another option, you can use foreach and implode for a simpler approach
foreach ($array as $value)
{
echo implode('',$value);
echo '<br />';
}
Doc: implode()

How to display values inside multi-dimensional array in php

I would like to get the values of id and name inside this array.
Array
(
[data] => Array
(
[0] => Array
(
[id] => 238345159549706
[members] => Array
(
[data] => Array
(
[0] => Array
(
[id] => 100001130889528
[name] => Sy Cheeze
)
[1] => Array
(
[id] => 100002616426665
[name] => Chun Jenny
)
.......
I've tried using this foreach.
foreach ($acquaintances as $acquaintance)
{
foreach ($acquaintance as $acquaint)
{
$acqID = $acquaint['id'];
$acqName = $acquaint['name'];
echo $acqName;
}
}
but nothing will be displayed. What would I do with my code? Any idea and suggestions please. Thank you!
$array = array
(
array("bla",22,18),
array("blaa",15,13),
array("blaaa",5,2),
array("blaaaa",17,15)
);
for ($row = 0; $row < 4; $row++) {
echo "<p><b>Row number $row</b></p>";
echo "<ul>";
for ($col = 0; $col < 3; $col++) {
echo "<li>".$array[$row][$col]."</li>";
}
echo "</ul>";
}
You can also access the indices directly in your foreach loop. Like this:
foreach($acquaintances['data'] as $acquaintance) {
foreach($acquaintance['members']['data'] as $acquaint) {
$acqID = $acquaint['id'];
$acqName = $acquaint['name'];
echo $acqName . '<br/>';
}
}

PHP get value of multidimensional array and compair

I have this array $pages which spits out this data:
Array (
[Name] => Array (
[Subname] => Array (
[0] => 43.2057, -79.9632, 1, -70,-150
[1] => 140240757658.jpg
[2] => 5
[3] => 0
) )
[Name2] => Array (
[Subname2] => Array (
[0] => 43.1769, -79.4703, 5, -70,-150
[1] => 140267498933.png
[2] => 16
[3] => 0
) )
)
and I have this foreach setup:
foreach($pages as $row => $value) {
echo '<li>'.$row.'<ul>';
foreach($value as $x => $y) {
echo
'<li>
'.$x.'
</li></ul></li>';
}
}
What I am trying to do is if [3] in each of the Subname is equal to 0, then skip it from my foreach.
NOTE: Subname and Name are just examples, they will be different for each one.
This should work:
foreach ($pages as $page) {
foreach($page as $subname) {
if ($subname[3] != 0) {
/* Do whatever you want with the data of this subname */
}
}
}
Or this if you want to use the key names:
foreach ($pages as $pageKey => $page) {
foreach($page as $subnameKey => $subname) {
if ($subname[3] != 0) {
/* Do whatever you want with the data of this subname */
}
}
}
If you want to skip the sub-foreach then this is the solution
foreach($pages as $row => $value) {
if($value['Subname'][3] == 0)
continue;
echo '<li>' . $row . '<ul>';
foreach($value as $x => $y) {
echo '<li>' . $x . '</li></ul></li>';
}
}
Since this is an Associative Array you will not be able to to check
$pages['foo']['bar'][0] === 0
before the first foreach begins since we don't know the value of $pages['foo'] before hand. when we get to the check you have already output html at the point in the form of:
echo '<li>' . $row . '<ul>';

how can we define the index of array in foreach loop for adding/dividng two different array values

hi everyone i fetch the array by using foreach loop now how can i define the index of different elements in array for futher computation like i want the element1 of array 1 to be divided by element1 of array 2
code of for each loop
<?php
if (isset($_POST['submit'])) {
$data_t1 = $_POST['t1'];
foreach ($data_t1 as $key => $value) {
echo 'T1: ' . $value . '<br />';
echo 'T2: ' . $_POST['t2'][$key] . '<br />';
echo 'T3: ' . $_POST['a1'][$key] . '<br />';
echo 'Username: ' . $_POST['username'][$key] . '<br /><br />';
}
$data_t2 = $_POST['t2'];
$data_t3=$_POST['a1'];
$data_t4=$_POST['username'];
}
var_dump($data_t1);
var_dump($data_t2);
var_dump($data_t3);
var_dump($data_t4);
?>
now i want these array values to be used for further computation
echo "Measuring Efficiency ";
echo "<table border='1' align='center'>
<tr>
<th>Inputs</th>
<th>A</th>
<th>B</th>
<th>C</th>
</tr>";
echo "<tr>";
foreach($data_t4 as $t4)
{
echo $t4; this display the values of array 4
}
foreach($data_t1 as $t1)
{
echo $t1;
$r=round(($t1/$t4),2);
$fr=round(($t1/$t4),2);
$br=round(($t1/$t4),2);
echo "<td>" ."Room Size/Consumption". "</td>";
echo "<td>".$r. "</td>";
echo "<td>".$fr. "</td>";
echo "<td>".$br. "</td>";
echo "</tr>";
echo "<tr>";
}
?
how can i define the index of array for division of elements of two array
my result is
$data_t1 [12 12] $data_t4 [1.44 4.32] $data_t2 [2 4] $data_t3 [2 3]
i want to divide 12/1.44,2/1.44 2/1.44 n 12/4.32 4/4.32 3/4.32
how can i do ths
Is this what you mean?
$array_1 = array(10,30,50,70,90);
$array_2 = array(1,3,5,7,9);
foreach($array_1 as $i => $v){
$array_1[$i] = $array_1[$i]/$array_2[$i];
}
print_r($array_1);
Array
(
[0] => 10
[1] => 10
[2] => 10
[3] => 10
[4] => 10
)
Or do you want to do something similar with the keys, not the values like so.
$array_1 = array(
10=>234234,
30=>234,
50=>23443,
70=>234,
90=>234
);
$array_2 = array(
1=>32423,
3=>4232,
5=>2342,
7=>234,
9=>234
);
$array_a = array_keys($array_1);
$array_b = array_keys($array_2);
foreach($array_a as $i => $v){
$tmp[$i] = $array_a[$i]/$array_a[$i];
}
print_r($tmp);
Array
(
[0] => 1
[1] => 1
[2] => 1
[3] => 1
[4] => 1
)
OK, based on your edit I've made something new.
You said:
i want to divide
12/1.44
2/1.44
2/1.44
12/4.32
4/4.32
3/4.32
$data_t4 = array(1.44,4.32);
$data_t1 = array(12,12);
$data_t2 = array(2,4);
$data_t3 = array(2,3);
$nums = array(
't1'=>$data_t1,
't2'=>$data_t2,
't3'=>$data_t3,
);
$divs = $data_t4;
foreach($nums as $i => $v){
foreach($v as $ix => $vx){
foreach($divs as $dx => $dv){
$data[$i]['n:'.$vx]['d:'.$dv] = $vx/$dv;
}
}
}
print_r($data);
Array
(
[t1] => Array
(
[n:12] => Array
(
[d:1.44] => 8.3333333333333
[d:4.32] => 2.7777777777778
)
)
[t2] => Array
(
[n:2] => Array
(
[d:1.44] => 1.3888888888889
[d:4.32] => 0.46296296296296
)
[n:4] => Array
(
[d:1.44] => 2.7777777777778
[d:4.32] => 0.92592592592593
)
)
[t3] => Array
(
[n:2] => Array
(
[d:1.44] => 1.3888888888889
[d:4.32] => 0.46296296296296
)
[n:3] => Array
(
[d:1.44] => 2.0833333333333
[d:4.32] => 0.69444444444444
)
)
)

How to loop 3 dimension array using foreach PHP

Below is foreach and the 3 dimension arrays, no problem with the looping but i cannot sepcify which array to echo, they must me the whole arrays like echo $subvalue, any better solutions with looping 3 dimension array? i actually feel weird with this looping. Thanks in adv
foreach ($stories as $key => $story){
//echo "<br />";
foreach($story as $subkey => $subvalue){
echo $subvalue."<br />";
foreach($subvalue as $key => $subsubvalue){
echo $subsubvalue."<br />";
}
}
}
Array
(
[270] => Array
(
[uid] => 36
[user_email] => aaa#hotmail.com
[sid] => 270
[story_name] => Story C
[photo_url] => Array
(
[0] => story_photos/2012/0322/361332381418153311.jpg
[1] => story_photos/2012/0322/361332393792911587.jpg
)
[photo_added_date] => Array
(
[0] => 1332381418
[1] => 1332393792
)
)
[269] => Array
(
[uid] => 36
[user_email] => aaa#hotmail.com
[sid] => 269
[story_name] => Story B
[photo_url] => Array
(
[0] => story_photos/2012/0322/361332381406580761.jpg
)
[photo_added_date] => Array
(
[0] => 1332381406
)
)
[268] => Array
(
[uid] => 36
[user_email] => aaa#hotmail.com
[sid] => 268
[story_name] => Story A
[photo_url] => Array
(
[0] => story_photos/2012/0322/361332381393552719.jpg
)
[photo_added_date] => Array
(
[0] => 1332381393
)
)
)
Why not try this :
foreach ($stories as $key => $story){
if(is_array($story)){
foreach($story as $subkey => $subvalue){
if(is_array($subvalue)){
foreach($subvalue as $key => $subsubvalue){
echo $subsubvalue."<br />";
}
} else {
echo $subvalue."<br />";
}
}
} else {
echo $story."<br />";
}
}
Also, I am not sure because your question isn't really clear or specified.
Or
function echoArray( $array )
{
foreach ($array as $key => $cell)
{
if ( true == is_array($cell) )
{
echoArray($cell);
}
else
{
echo "$cell<br />";
}
}
}
It works for N dimensionnal array
An improved version to know the depth and use different css class for depth and to use set the tag in which the value should be added:
Eg: for depth 0 the class will by arrayclass_0, for depth 1 arrayclass_1, etc...
/**
$array : The array
$depth: The depth ( you should always set it to 0)
$cssclassprefix: The css class prefix you want to set
$tag: the default tag to use to render
$arraytagkey: An optionnal key in your array to extract the tag to use
*/
function echoArray( $array, $depth=0, $cssclassprefix='arrayclass_', $tag='div', $arraytagkey = '' )
{
if ( 0 != strcmp($arraytagkey) && isset($array[$arraytagkey]) )
{
$tag=$array[$arraytagkey];
}
foreach ($array as $key => $cell)
{
if ( true == is_array($cell) )
{
echoArray($cell, $depth+1, $cssclassprefix, $tag, $arraytagkey);
}
else
{
$matches = array();
if ( 0 != preg_match("/^(img|iframe|input)$/i",$tag) )
{
if ( 0 != strcasecmp('input',$tag) )
{
echo "<input class='$cssclassprefix$depth' value='$cell' />";
}
else
{
echo "<$tag class='$cssclassprefix$depth' src='$cell' />";
}
}
else if( 0 != preg_match("/^(br|hr)$/i",$tag) )
{
echo "$cell<$tag />";
}
else
{
echo "<$tag class='$cssclassprefix$depth'>$cell</$tag>";
}
}
}
}
This doesn't really answer your question, but note, your third loop is this:
foreach ($subvalue as $key => $subsubvalue){
But you've already used $key in the first loop:
foreach ($stories as $key => $story){
You should change the third to:
foreach ($subvalue as $subsubkey => $subsubvalue){
Are you just wanting to loop through and get access to the photo_urls and other data? If that's the case then here's a simple example of how you can access the data:
foreach($stories as $key => $story)
{
// here you can echo the $store['user_email'] etc
echo 'Email: ' . $story['user_email'] . '<br />';
// loop over the photo urls
foreach($story['photo_url'] as $photo_url)
{
echo 'Photo URL: ' . $photo_url . '<br />';
}
// loop over the photo added dates
foreach($story['photo_added_date'] as $photo_added_date)
{
echo 'Photo Date: ' . $photo_added_date . '<br />';
}
}
If you're wanting to recursively search the array then the other answers are what you want.

Categories