i have this array structure:
$ar = [product_info] => Array
(
[pname] => Array
(
[0] => Выделенный сервер DE-22
[1] => Hello 4
[2] => Hello World
)
[pid] => Array
(
[0] => 217
[1] => 342
[2] => 343
)
i want to iterate it like this (in one loop):
foreach ($ar['product_info'] as $item) {
echo $item['pname'];
echo $item['pid']
}
How can i do this? need help
i think a normal for solve it.
$total = count($ar['product_info']['pid']);
for($i=0; $i<$total; $i++){
echo $ar['product_info']['pname'][$i];
echo $ar['product_info']['pid'][$i];
}
<?php
$ar = array(
'product_info' => array(
'pname' => array(
'Product 1',
'Product 2',
'Product 3',
'Product 4 > test <',
),
'pid' => array(
217,
342,
343,
666,
),
),
);
foreach($ar['product_info']['pid'] as $productIndex => $productId)
{
$productName = $ar['product_info']['pname'][$productIndex];
echo '<a href="?product_id=' . intval($productId) . '">';
echo htmlspecialchars($productName);
echo '</a>';
echo '<br />' . PHP_EOL;
}
You need to iterate over the inner arrays, which are int-indexed:
$size=sizeof($ar['product_info']['pname'];
for ($i=0;$i<$size;$i++) {
echo $ar['product_info']['pname'][$i];
echo $ar['product_info']['pid'][$i];
}
Related
$list = array(
array(
'header_name' => '1.',
'header_database_name' => '2',
'heading' => 'personal',
),
array(
'header_name' => '11. ',
'header_database_name' => '22',
'heading' => 'professional',
),
array(
'header_name' => '33',
'header_database_name' => '333',
'heading' => 'personal',
),
);
foreach($list as $li)
{
?>
"> ';}
else ($li['heading'] == 'performance')
{echo $li['header_name'].'';}
}
I need to print this array by grouping it by heading for example the one with heading as personal should be displayed under the personal title i.e:
Personal
1
33
Professional
11
my php code is like this
foreach($list as $li)
{
?>
"> ';}
else ($li['heading'] == 'performance')
{echo $li['header_name'].'';}
}
It is printing by group but i also need heading to be added but only one time at the top for example now if i add heading to it it prints as personal 1
professional 2
peronal 3
but i need as
personal
1
2
professional
2
You need to sort array if you want grouping of data, use usort
function headingSort ($x, $y) {
return strcasecmp($x['heading'], $y['heading']);
}
usort($myArray, 'headingSort');
Use like
$new_array = array();
foreach($list as $k=>$v)
{
if(!in_array($v['header_name'], $new_array))
$new_array[$v['heading']][] = $v['header_name'];
else
$new_array[$v['heading']] = $v['header_name'];
}
print_r($new_array);
Try this :
$list = array(
array(
'header_name' => '1.',
'header_database_name' => '2',
'heading' => 'personal',
),
array(
'header_name' => '11. ',
'header_database_name' => '22',
'heading' => 'professional',
),
array(
'header_name' => '33',
'header_database_name' => '333',
'heading' => 'personal',
)
);
$personal = array();
$professional = array();
for($i=0;$i<count($list);$i++){
if($list[$i]['heading']=="professional"){
array_push($professional,$list[$i]);
}else{
array_push($personal,$list[$i]);
}
}
$new = array(
"personal"=>$personal,
"professional"=>$professional
);
echo "<pre>";
print_r($new);
Result will be like :
Array
(
[personal] => Array
(
[0] => Array
(
[header_name] => 1.
[header_database_name] => 2
[heading] => personal
)
[1] => Array
(
[header_name] => 33
[header_database_name] => 333
[heading] => personal
)
)
[professional] => Array
(
[0] => Array
(
[header_name] => 11.
[header_database_name] => 22
[heading] => professional
)
)
)
The solution using array_column(available since PHP 5.5), array_unique, array_keys and implode functions:
$headings = array_column($list, 'heading', 'header_name');
foreach (array_unique($headings) as $v) {
echo ucfirst($v). PHP_EOL;
echo implode("\n", array_keys($headings, $v)). PHP_EOL;
}
The output:
Personal
1
33
Professional
11
I am trying to display a multidimension php array as a table... can someone help?
This is my array:
Array
(
[1] => Array
(
[0] => Array
(
[c1] => UA07
[s1] => 6
[c2] => Ultimate Force
[s2] => 8
)
[1] => Array
(
[c1] => UF HEROES
[s1] => 6
[c2] => OLD School
[s2] => 4
)
[2] => Array
(
[c1] => Winners 05
[s1] => not_played
[c2] => World XI
[s2] => not_played
)
[3] => Array
(
[c1] => Outlaw
[s1] => 4
[c2] => UWK
[s2] => 3
)
)
[2] => Array
(
[0] => Array
(
[c1] => Ultimate Force
[s1] => 2
[c2] => UF HEROES
[s2] => 4
)
[1] => Array
(
[c1] => BY
[s1] => 0
[c2] => Outlaw
[s2] => 0
)
)
[3] => Array
(
[0] => Array
(
[c1] => UF HEROES
[s1] => 5
[c2] => Outlaw
[s2] => 1
)
)
)
This array currently contains 3 rounds, but could in theory contain more... and within each round it contains each game, and within each game it contains the results of those games...
C1/C2 are competitors and S1 / S2 are the scores...
How do I display each array in a column and move on to the next array and show them in columns as well.. lining them up like a tournament bracket
Any thoughts would be very helpful.
I am trying to achieve a format like this:
http://new.playdat.com/tournament-results.php "Bracket"
As others have said in the comments, you need a nested loop. Since you didn't specify a table format, I just created an example. You can run this at http://writecodeonline.com/php/
/** Simulating your data **/
$rounds = array();
$rounds[1][] = array('c1' => 'UA07', 's1' => 6, 'c2' => 'Ultimate Force', 's2' => 8);
$rounds[1][] = array('c1' => 'UF HEROES', 's1' => 6, 'c2' => 'Old School', 's2' => 4);
$rounds[2][] = array('c1' => 'Ultimate Force', 's1' => 2, 'c2' => 'UF HEROES', 's2' => 4);
$rounds[2][] = array('c1' => 'BY', 's1' => 0, 'c2' => 'Outlaw', 's2' => 0);
$table = <<<EOD
<table>
<thead>
<tr>
<th>Round</th>
<th>C1</th>
<th>S1</th>
<th>C2</th>
<th>S2</th>
</tr>
</thead>
<tbody>
EOD;
foreach ($rounds as $roundNum=> $round){
foreach ($round as $game) {
$table .= "<tr>";
$table .= "<td>{$roundNum}</td>";
$table .= "<td>{$game['c1']}</td>";
$table .= "<td>{$game['s1']}</td>";
$table .= "<td>{$game['c2']}</td>";
$table .= "<td>{$game['s1']}</td>";
$table .= "</tr>";
}
}
$table .= <<<EOD
</tbody>
</table>
EOD;
echo $table;
Dynamic tournament bracket:
Your array:
$rounds = array(
//Round 1
array(
array(
'c1' => 'UA07 ',
's1' => 6 ,
'c2' => 'Ultimate Force ',
's2' => 8 ,
),
array(
'c1' => 'UF HEROES',
's1' => 6,
'c2' => 'OLD School',
's2' => 4,
),
array(
'c1' => 'Winners 05 ',
's1' => 'not_played',
'c2' => 'World XI',
's2' => 'not_played',
),
array(
'c1' => 'Outlaw',
's1' => 4,
'c2' => 'UWK',
's2' => 3,
),
),
//Round 2
array(
array(
'c1' => 'Ultimate Force',
's1' => 2,
'c2' => 'UF HEROES',
's2' => 4,
),
array(
'c1' => 'BY',
's1' => 0,
'c2' => 'Outlaw',
's2' => 0,
),
),
//Round 3
array(
array(
'c1' => 'UF HEROES',
's1' => 5,
'c2' => 'Outlaw',
's2' => 1,
),
)
);
The generator:
$roundCount = 1;
$totalRounds = count($rounds);
echo '<table border="1"><tr>';
for($i = 1;$i <= $totalRounds;$i++)
{
echo '<td>Round'.$i.'</td>';
}
echo '</tr><tr>';
foreach($rounds as $round)
{
$matches = count($round);
echo '<td>';
foreach($round as $match)
{
echo $match['c1'].' - '.$match['s1'].'<br>';
echo $match['c2'].' - '.$match['s2'].'<br><hr>';
}
$roundCount++;
echo '</td>';
}
echo '</tr></table>';
Results:
Generator 2:
$roundCount = 1;
$totalRounds = count($rounds);
echo '<table border="1"><tr>';
for($i = 1;$i <= $totalRounds;$i++)
{
echo '<td>Round'.$i.'</td>';
}
echo '</tr><tr>';
foreach($rounds as $round)
{
$matches = count($round);
echo '<td>';
foreach($round as $match)
{
echo '<table border="1">';
echo '<tr><td>'.$match['c1'].'</td><td>'.$match['s1'].'</td></tr>';
echo '<tr><td>'.$match['c2'].'</td><td>'.$match['s2'].'</td></tr>';
echo '</table>';
}
$roundCount++;
echo '</td>';
}
echo '</tr></table>';
Generator with winner:
$roundCount = 1;
$totalRounds = count($rounds);
echo '<table border="1"><tr>';
for($i = 1;$i <= $totalRounds;$i++)
{
echo '<td>Round'.$i.'</td>';
}
echo '<td>Winner</td>';
echo '</tr><tr>';
foreach($rounds as $round)
{
$matches = count($round);
echo '<td>';
if($roundCount == $totalRounds)
{
if($round[0]['s1'] > $round[0]['s2']) {
$finalist = array($round[0]['c1'],$round[0]['s1']);
} else {
$finalist = array($round[0]['c2'],$round[0]['s2']);
}
}
foreach($round as $match)
{
echo '<table border="1">';
echo '<tr><td>'.$match['c1'].'</td><td>'.$match['s1'].'</td></tr>';
echo '<tr><td>'.$match['c2'].'</td><td>'.$match['s2'].'</td></tr>';
echo '</table>';
}
$roundCount++;
echo '</td>';
}
echo '<td>';
echo '<table border="1">';
echo '<tr><td>'.$finalist[0].'</td><td>'.$finalist[1].'</td></tr>';
echo '</table>';
echo '</td>';
echo '</tr></table>';
How can I get [jobNo] using loop from array below?
Array
(
[date] => 2014-01-13
[totcomdraft] => 400
[comdraft] => 0
[0] => Array
(
[jobNo] => 1401018618
[dateType] => 1
[comdraft] => 200
)
[1] => Array
(
[jobNo] => 1401018615
[dateType] => 1
[comdraft] => 100
)
[2] => Array
(
[jobNo] => 1401018617
[dateType] => 1
[comdraft] => 100
)
)
Try this
foreach($array as $key=>$val){
if(is_array($val)){
echo $val["jobNo"];
echo "<br />";
}
}
for( $i = 0; $ < count($array); $i++ )
{
print $array[$i]['jobNo'] . "<br>";
}
try with array in-built function :-
$result_array=array_map(function($input_array)
{
return $input_array['desired_column'];
},$input_array_original
);
Use This
foreach($array as $key=>$val){
if(is_array($val)){ // check this value in array
echo $val["jobNo"];
echo "<br />";
}
}
This code should work. I have tested.
$array = array
(
'date' => '2014-01-13',
'totcomdraft' => 400,
'comdraft' => 0,
'0' => array
(
'jobNo' => 1401018618,
'dateType' => 1,
'comdraft' => 200
),
'1' => array
(
'jobNo' => 1401018615,
'dateType' => 1,
'comdraft' => 100
),
'2' => array
(
'jobNo' => 1401018617,
'dateType' => 1,
'comdraft' => 100
)
);
for($i=0; $i<3; $i++){
echo 'Job no:' . $array[$i]['jobNo']."<br>";
}
Output:
Job no:1401018618
Job no:1401018615
Job no:1401018617
I use the https://github.com/humanmade/Custom-Meta-Boxes/ to make this custom repeatable custom fields to wordpress. The given arrays are like this
Array
(
[0] => Array
(
[photoset-caption] => Array
(
[cmb-field-0] => Caption1
[cmb-field-1] => Caption2
[cmb-field-2] => Caption3
[cmb-field-3] => Caption4
)
[photoset-image] => Array
(
[cmb-field-0] => 17
[cmb-field-1] => 16
[cmb-field-2] => 15
[cmb-field-3] => 14
)
)
[1] => Array
(
[photoset-caption] => Array
(
[cmb-field-0] => Caption1
[cmb-field-1] => Caption2
[cmb-field-2] => Caption3
[cmb-field-3] => Caption4
)
[photoset-image] => Array
(
[cmb-field-0] => 17
[cmb-field-1] => 16
[cmb-field-2] => 15
[cmb-field-3] => 14
)
)
)
The loop it try to make is this.
// get the custom fields for this post
$photoset = get_post_meta($post->ID, 'photoset_group_fields', false );
echo '<div class="photoset">';
echo '<div class="photoset-row">';
foreach($photoset as $photosetloop){
echo '<figure class="photoset-item">';
echo '<div>' . wp_get_attachment_image($photosetloop['photoset-image'], 'large' ) . '</div>';
echo '<figcaption>' . $photosetloop['photoset-caption'] .'</figcaption>';
echo '</figure>';
}
echo '</div>';
echo '</div>';
So the loop haves .photoset-item and inside it have image and caption.
My question how to I foreach it, thanks.
I did update for the array, i have group that I loop.
The simplest way it's to make two foreachs, you can do it recursive with a function if you want as well.
<?php
$photos =array(
'photoset-caption' => array(
'cmb-field-0' => 'Caption1',
'cmb-field-1' => 'Caption2',
'cmb-field-2' => 'Caption3',
'cmb-field-3' => 'Caption4'
),
'photoset-image' => array(
'cmb-field-0' => 17,
'cmb-field-1' => 16,
'cmb-field-2' => 15,
'cmb-field-3' => 14
)
);
foreach($photos as $k => $photo){
echo '<h1>'.$k.'</h1>';
foreach($photo as $key => $value){
echo $key.': '. $value.'<br/>';
}
echo '<hr/>';
}
Check an example here: example
Not the most elegant way to do it, but it does work in this case.
$arr = Array
(
"photoset-caption" => Array
(
"cmb-field-0" => "Caption1",
"cmb-field-1" => "Caption2",
"cmb-field-2" => "Caption3",
"cmb-field-3" => "Caption4"
),
"photoset-image" => Array
(
"cmb-field-0" => 17,
"cmb-field-1" => 16,
"cmb-field-2" => 15,
"cmb-field-3" => 14
)
);
$count = count($arr["photoset-caption"]);
for($i = 0; $i < $count; ++$i) {
echo 'Loop #'.$i.'<br>';
echo $arr["photoset-caption"]["cmb-field-".$i], '<br>';
echo $arr["photoset-image"]["cmb-field-".$i], '<br>';
echo '<br>';
}
You can paste it here:
http://writecodeonline.com/php/
I have a function that build a Tree Array. Example:
Array
(
[0] => Array
(
[id] => 12
[address] => 'Ukraine'
[parent_id] => 0
[children] => Array
(
[0] => Array
(
[id] => 11
[address] => Crimea
[parent_id] => 12
[children] => Array
(
[0] => Array
(
[id] => 16
[address] => Yalta
[parent_id] => 11
)
)
)
)
)
I have function to print Tree (I want to get output with levels indented):
function printTree($data) {
foreach ($data as $item) {
if ($item['parent_id'] != 0)
echo ' - ' . $item['address'] . "<br>";
else
echo $item['address'] . "<br>";
if (isset($item['children'])) {
printTree($item['children']);
}
}
}
But my result is only with one levels indets, because my if is not correct:
Ukraine
- Crimea
- Yalta
I need to get with all levels indents. What do I need to change in my if statement?
Ukraine
- Crimea
- Yalta
Rewrite your code this way:
function printTree($data, $level = 0) {
foreach ($data as $item) {
if ($item['parent_id'] != 0) {
/* here we corrects indent: */
echo str_repeat(' ', $level) . ' - ' . $item['address'] . "<br>";
} else {
echo $item['address'] . "<br>";
}
if (isset($item['children'])) {
printTree($item['children'], $level + 1);
}
}
}
You just add a depth variable to control how many blanks you want add before each child item:
<?php
$data = array(
0 => array(
'id' => 12,
'address' => 'Ukraine',
'parent_id' => 0,
'children' => array(
0 => array(
'id' => 11,
'address' => Crimea,
'parent_id' => 12,
'children' => array(
0 => array(
'id' => 16,
'address' => Yalta,
'parent_id' => 11,
)
)
)
)
)
);
printTree($data);
function printTree($data, $depth = 0) {
foreach ($data as $item) {
if ($item['parent_id'] != 0)
echo str_repeat(' ', $depth), "- {$item['address']}\n";
else
echo "{$item['address']}\n";
if (isset($item['children'])) {
printTree($item['children'], ++$depth);
}
}
}
Output:
Ukraine
- Crimea
- Yalta
Working demo.