I've got an multidimensional array with some values.
[
1 => [
'label' => 'SEO',
'content' => 'Some content',
'group' => 'We can offer'
]
2 => [
'label' => 'Webdesign',
'content' => 'Some content',
'group' => 'We can offer'
]
3 => [
'label' => 'Contact',
'content' => 'Some content',
'group' => 'Who are we?'
]
4 => [
'label' => 'Logodesign',
'content' => 'Some content',
'group' => 'We can offer'
]
5 => [
'label' => 'Address',
'content' => 'Some content',
'group' => 'Who are we?'
]
]
The group element is a variety of user input. I want to sort all group elements what are the same into the same array. It's then going to be displayed. If there are only 2 elements with the same group value, then there will only be two columns (50% width on both) in a .row element in HTML, if there are 1 element, only one column (100% width). I'm trying to build a very simple CMS if anyone was wondering why. There may be more easier ways to do this, but I can't think of any.
Any help is appreciated.
EDIT:
I just got the array sorted i think. It looks right.
Now I just need to display it the right way.
$i = 0;
$count = count($data['sections']);
$content = [];
for ($i = 0; $i < $count; $i++) {
if (!in_array($data['sections'][$i]['group'], $content)) {
$content[] = $data['sections'][$i]['group'];
}
$content[$data['sections'][$i]['group']][] = ['label' => $data['sections'][$i]['label'], 'content' => $data['sections'][$i]['content']];
}
Group the array
Simply loop through the array and put all the elements into a new, nested array:
$content = array();
foreach($data['sections'] as $section) {
$content[$section['group']][] = $section;
}
This gives you an array $content of this format:
[
'We can offer' => [
1 => [
'label' => 'SEO',
'content' => 'Some content',
'group' => 'We can offer'
]
2 => [
'label' => 'Webdesign',
'content' => 'Some content',
'group' => 'We can offer'
]
...
]
'Who are we?' => [
...
]
]
Divide the width: Table display
So how do you output this so that every category gets equal width? First you need to loop through the nested array to print some HTML:
<div class="outer">
<?php foreach($content as $group) { ?>
<div class="row">
<?php foreach($group as $item) { ?>
<div class="item"><?php echo $item['content']; ?></div>
<?php } ?>
</div>
<?php } ?>
</div>
Have a look at this answer for one way to do it using CSS:
div.outer { display:table; }
div.row { display:table-row; }
div.item { display:table-cell; }
Divide the width: Explicit width
Another way to do it is to calculate the width in percentage in PHP and set it explicitly with the width attribute:
<?php foreach($content as $group) { ?>
<div class="row">
<?php $w = 100 / count($group); ?>
<?php foreach($group as $item) { ?>
<div class="item" width="<?php echo $w; ?>%">
<?php echo $item['content']; ?>
</div>
<?php } ?>
</div>
<?php } ?>
Related
i have an array i and i want to show the array values if the name of same array repeat in the another array and have true value
my arrays like this
$array1 = [
array(
'name' => internal_evidence
'price' => 30
'course_id' => 3
),
array(
'name' => international_evidence
'price' => 450
'course_id' => 3
),
array(
'name' => internal_evidence
'price' => 10
'course_id' => 1
),
array(
'name' => technical_evidence
'price' => 134
'course_id' => 3
),
];
$array2 = [
array(
'id' => 3
'name' => graphic
'price' => 150
'attr' => array(
'internal_evidence' => 'true',
'international_evidence' => 'false',
'technical_evidence' => 'true'
)
),
array(
'id' => 5
'name' => 3dmax
'price' => 300
'attr' => array(
)
),
array(
'id' => 1
'name' => ICDL
'price' => 480
'attr' => array(
'internal_evidence' => 'true',
)
),
];
i want to showing this all attr selected with true value in like this
also I want to sum price of array2 member and array1
<h2>graphic</h2>
<p>internal_evidence</p>
<p>technical_evidence</p>
<small>course price: 150</small>
<small>314</small> <!-- Price with selected evidence -->
<h2>3dmax</h2>
<small>course price: 300</small>
<!-- its not have attr evidence -->
<h2>ICDL</h2>
<p>internal_evidence</p>
<small>course price: 480</small>
<small>490</small> <!-- Price with selected evidence -->
i try this but its don`t work properly
$priceOfAttr = 0;
foreach($array2 as $key => $cat):
echo "<h2>{$cat['name']}</h2>";
foreach($array1 as $pr):
if($pr['course_id'] == $cat['id']):
foreach($cat['attr'] as $m => $optionV):
if($m == $pr['name'] && $optionV == "true"){
echo $m .'<br>';
$priceOfAttr += $pr['price'];
// echo "<small>{$cat['price']}</small><br>";
// echo $cat['price'] + $pr['price']. "<br>";
}
endforeach;
echo $priceOfAttr + $cat['price'] . '<br>';
endif;
endforeach;
echo '<br>';
endforeach;
I'd use a combination array_reduce and array_map to transform your data into what you need, then simply loop over that to display your view:
<?php
// Index your $array1 by [id][name]
$array1ByIdAndName = array_reduce($array1, static function ($byIdAndName, $entry) {
$byIdAndName[$entry['course_id']][$entry['name']] = $entry;
return $byIdAndName;
});
// Transform $array2's `attr` entries into attribute list + compute total price
$array2 = array_map(static function ($entry) use ($array1ByIdAndName) {
$entry['total_price'] = $entry['price'];
$entry['attr'] = array_reduce(array_keys($entry['attr']), static function ($attrs, $attrName) use ($array1ByIdAndName, &$entry) {
if ($entry['attr'][$attrName] === 'true') {
$attrs[] = $attrName;
$entry['total_price'] += $array1ByIdAndName[$entry['id']][$attrName]['price'];
}
return $attrs;
}, []);
return $entry;
}, $array2);
// Display your view
?>
<?php foreach ($array2 as $entry): ?>
<h2><?= $entry['name'] ?></h2>
<?php foreach ($entry['attr'] as $attrName): ?>
<p><?= $attrName ?></p>
<?php endforeach ?>
<small>course price : <?= $entry['price'] ?></small>
<?php if ($entry['total_price'] > 0): ?>
<small><?= $entry['total_price'] ?></small>
<?php endif ?>
<?php endforeach ?>
Demo: https://3v4l.org/nS3Gl
Not sure if I titled this question correctly. I'm having some trouble looping over a multi-demensional php array to build some HTML nodes. Here is the array I'm looping over:
$locations = array(
'CityName' => array(
array(
'title' => 'Title',
'phone' => '(555) 555-5555',
'address' => '1234 Fake st.',
'city' => 'Ventura',
'state' => 'CA',
'zip' => '93003',
'url' => 'http://www.google.com/'
),
array(
'title' => 'Title',
'phone' => '(555) 555-5555',
'address' => '1234 Fake st.',
'city' => 'Ventura',
'state' => 'CA',
'zip' => '93003',
'url' => 'http://www.google.com/'
),
),
'CityName2' => array(
array(
'title' => 'Title',
'phone' => '(555) 555-5555',
'address' => '1234 Fake st.',
'city' => 'Ventura',
'state' => 'CA',
'zip' => '93003',
'url' => 'http://www.google.com/'
),
array(
'title' => 'Title',
'phone' => '(555) 555-5555',
'address' => '1234 Fake st.',
'city' => 'Ventura',
'state' => 'CA',
'zip' => '93003',
'url' => 'http://www.google.com/'
)
)
);
Keep in mind I may have built this array incorrectly for what I'm trying to do. The HTML output for this loop should be:
<h4>CityName</h4>
<ul>
<li>
<p>Title</p>
<p>1234 Fake St.</p>
<p>Ventura, CA 93003</p>
<p>(555) 555-5555</p>
<p class="link">Visit Website</p>
</li>
<li>
<p>Title</p>
<p>1234 Fake St.</p>
<p>Ventura, CA 93003</p>
<p>(555) 555-5555</p>
<p class="link">Visit Website</p>
</li>
</ul>
<h4>CityName2</h4>
<ul>
...
</ul>
I think what I want to do is to be able to grab the individual pieces of data to plug into my HTML template.. like $location['title'], $location['phone'], etc. The PHP that I currently have will only go as far to loop over and echo out the keys or values from each individual location array.
<?php
// Printing all the keys and values one by one
$locationNames = array_keys($locations);
for($i = 0; $i < count($locations); $i++) {
echo "<h4>" . $locationNames[$i] . "</h4>";
echo "<ul>";
foreach($locations[$locationNames[$i]] as $key => $value) {
foreach($value as $key => $value) {
echo $value;
}
}
echo "</ul>";
}
?>
Use nested foreach loops amd drop the values in to the appropriate places:
<?php foreach ($locations as $location => $ldata) { ?>
<h4><?php echo $location; ?></h4>
<ul>
<?php foreach ($ldata as $attribute) { ?>
<li>
<p><?php echo $attribute['title']; ?></p>
<p><?php echo $attribute['address']; ?></p>
<p><?php echo $attribute['city'] . " ," . $attribute['state'] . " " . $attribute['zip']; ?></p>
<p><?php echo $attribute['phone']; ?></p>
<p class="link">Visit Website</p>
</li>
<? php } ?>
<?php } ?>
You just need nested (foreach) loops:
<?php foreach($locations as $cityname => $location):?>
<h4><?=$cityname?></h4>
<ul>
<?php foreach($location as $place:?>
<li>
<p><?=$place['title']?></p>
<p><?=$place['phone']?></p>
<!-- etc etc-->
</li>
<?php endforeach;?>
</ul>
<?php endforeach;?>
Something like this should work. I won't implement the HTML for you, but you it should be easy to do. This has the advantage that if you have dynamic keys in the inner array, you won't have to know them before hand.
foreach($locations as $key => $value) {
echo $key, PHP_EOL;
$data = $locations[$key];
$length = count($data);
for($i = 0; $i < $length; $i++) {
$values = $data[$i];
foreach($values as $key2 => $value2)
echo "\t", $key2, ": ", $value2, PHP_EOL;
}
}
Just a few tweaks to your code:
<?php
// Printing all the keys and values one by one
$locationNames = array_keys($locations);
for($i = 0; $i < count($locations); $i++) {
echo "<h4>" . $locationNames[$i] . "</h4>";
echo "<ul>";
foreach($locations[$locationNames[$i]] as $key => $value) {
echo "<li>"; // add list open tag <-- tweak #1
foreach($value as $key => $value) {
echo "<p>$value</p>"; // add paragraph tags <-- tweak #2
}
echo "</li>"; // add list close tag <-- tweak #3
}
echo "</ul>";
}
?>
PHP Sandbox example.
I have created a form for a POST request, but I need to send an array of information that must be not visible in that form.
The array consist in a not specified number of other arrays.
My array is called "$dati" and is made by a not bounded number of arrays with three information each.
The code of my form is:
<?= $this->Form->create(null,['type'=> 'post', 'url'=>['action'=>'selectForSell2',$rassegnaselezionata->id,$showselezionato->id,$proiezioneselezionata->id ]]) ?>
<?= $this->Form->input('stato', ['options' => ['tutti' => 'Tutti i Soci', 'firmato' => 'Soci Firmati', 'approvato' => 'Soci Approvati'] ] ); ?>
<?= $this->Form->input('campo', ['options' => ['cognome' => 'Cognome', 'nome' => 'Nome', 'codicefiscale' => 'Codice Fiscale'] ] ); ?>
<?= $this->Form->input('ricerca', ['label' => false, "class" => " form-control input-medium", "placeholder" => __('Ricerca'), 'visible'=>false]); ?>
<?= $this->Form->button(__('Submit')) ?>
<?= $this->Form->end() ?>
In my projects, I have the following element as hidden.ctp:
if (isset($model)) {
$model .= '.';
} else {
$model = '';
}
foreach ($fields as $field => $values) {
if (is_array($values)) {
echo $this->element('hidden', ['model' => $model . $field, 'fields' => $values]);
} else {
echo $this->Form->hidden($model . $field, ['value' => $values]);
}
}
Then you can just call it with
echo $this->element('hidden', ['fields' => $dati]);
If you want to hidden the value of the array then you can do it in this way.
<?php
$a = array("Name" => "Peter", "Age" => "41", "Country" => "USA");
?>
<input type="hidden" name="data[Menu][arr]" value="<?php pr($a); ?>">
It can produce output like,when you post the data.Check it.it is required or not
Array
(
[Menu] => Array
(
[arr] => Array( [Name] => sradha [Age] => 20 [Country] => IN)
)
)
//i want to loop an array to make dinamic chart
//form this
$this->widget('ext.Hzl.google.HzlVisualizationChart', array('visualization' => 'LineChart',
'data' => array(
0=>array('Task', 'Hours per Day'),
1=>array('Work', 11),
2=>array('Work', 11),
),
'options' => array('title' => 'My Daily Activity')));
?>
//to
$a=0;
$loop=array();
while ($a < 10)
{
$loop=$loop+array("a","1");
$a=$a+1;
}
$this->widget('ext.Hzl.google.HzlVisualizationChart', array('visualization' => 'LineChart',
'data' => $loop
'options' => array('title' => 'My Daily Activity')));
?>
//but this code is error, please help me :(
I assume you want $loop to be an array similar to the first example.
You need to change this:
$loop=$loop+array("a","1");
to this:
$loop[] = array("a","1");
This will add a new element to the array instead of overwriting it.
You can use:
$loop[] = array("a","1"); to add elements to the existing array.
And you are missing a comma after the 'data' => $loop
Try using:
$a=0;
$loop=array();
while ($a < 10)
{
$loop[] = array("a","1"); // "a" or $a ?
$a=$a+1;
}
$this->widget('ext.Hzl.google.HzlVisualizationChart', array('visualization' => 'LineChart',
'data' => $loop,
'options' => array('title' => 'My Daily Activity')));
?>
Here is the script I am running and I would like if there are 2 strings that the same to only display one string and not both. I dont know where to add the array_unique() I have added it to my script but it doesnt seem to work properlly, instead it is taking out all the strings with the same value Here is the script I am running and I would like if there are 2 strings that the same to only display one string and not both
//Get slider data from theme options
$company1 = $data['md_media_company_img1'];
$company2 = $data['md_media_company_img2'];
$company3 = $data['md_media_company_img3'];
$company4 = $data['md_media_company_img4'];
$company5 = $data['md_media_company_img5'];
$company6 = $data['md_media_company_img6'];
$company7 = $data['md_media_company_img7'];
$company8 = $data['md_media_company_img8'];
$company9 = $data['md_media_company_img9'];
$company10 = $data['md_media_company_img10'];
$company11 = $data['md_media_company_img11'];
$company12 = $data['md_media_company_img12'];
/*Slides Array*/
$company_name = array(
'company1' => array(
'name' => $company1,
),
'company2' => array(
'name' => $company2,
),
'company3' => array(
'name' => $company3,
),
'company4' => array(
'name' => $company4,
),
'company5' => array(
'name' => $company5,
),
'company6' => array(
'name' => $company6,
),
'company7' => array(
'name' => $company7,
),
'company8' => array(
'name' => $company8,
),
'company9' => array(
'name' => $company9,
),
'company10' => array(
'name' => $company10,
),
'company11' => array(
'name' => $company11,
),
'company12' => array(
'name' => $company12,
)
);
/*check if exist slide*/
$check_exist_company = 0;
$result = array_unique($company_name);
foreach($result as $company => $value) {
if (!empty ($value['name'])){
$check_exist_company = 1;
}
}
?>
<?php if($check_exist_company == 1) {// check if any slide image added in theme option, return custom slide?>
<?php $i = 1; ?>
<?php foreach($company_name as $company => $value) {
if (!empty ($value['name'])) {?>
<li><a class="nivoLink4" rel="<?php echo $i;?>" href="#"><?php echo $value['name'];?></a></li>
<?php ++$i ?>
<?php } ?>
<?php }?>
<?php } ?>
<!--/slider-->
You could just run array_unique() on the source array and just iterate over the result.