I am trying to get the key from an array to make divs become:
<div id="item">
<div id="item-1">
<div id="item-2">
<div id="item-3">
My array code is:
$a = $something['no'];
$array = array_fill(0, $a, 'value');
foreach($array as $key => $value){
if($key == '0') {
$x = 'item';
} else {
$x = 'item-'.$key;
}
$insert .= '<div id="'.$x.'">';
// content here
$insert .= '</div>';
}
$key isn't being sent, I tried echoing $key and got no return, is there something I am missing?
Print_r on the array shows:
Array ( [0] => value [1] => value [2] => value )
Fixed Code:
$a = $something['no'];
$array = array_fill(0, $a, 'value');
foreach($array as $key => $value){
if($key == '0') {
$x = 'item';
} else {
$x = 'item-'.$key;
}
$insert .= '<div id="'.$insert = $x.'">';
// content here
$insert .= '</div>';
}
You could minimize your code with a for loop:
$something['no'] = 4;
$html = '';
for ($i = 0; $i < $something['no']; $i++) {
$x = !$i ? '' : "-{$i}";
$content = "woof woof";
$html .= "<div id=\"item{$x}\">".$content."</div>\n";
}
print_r($html);
Saves on assigning a variable and running a separate process to fill an array with numbers which you could just add up to anyway
Related
I intend the php function to return me the list of all the keys if the value equal or are less than the variable
<?php
$t = 35;
$jsonobj = '{"ABC":35,"DEF":36,"GEH":34}';
$obj = json_decode($jsonobj);
foreach($obj as $key => $value){
if ($t <= $value) {
{echo $key . " => " . $value . "<br>";}}
else{
echo "No result match";
}
}
?>
Decode your JSON as array an use array_filter() to filter out elements:
$jsonobj = '{"ABC":35,"DEF":36,"GEH":34}';
$array = json_decode($jsonobj, true);
print_r(array_filter($array, "lessthan"));
function lessthan($var) {
return $var <= 35;
}
Will output
Array
(
[ABC] => 35
[GEH] => 34
)
Edit: if you want to use/keep your $t you can do like so:
<?php
$t = 35;
$jsonobj = '{"ABC":35,"DEF":36,"GEH":34}';
$array = json_decode($jsonobj, true);
print_r(array_filter($array, function ($var) use ($t) {
return $var <= $t;
}));
If you want to maintain your style of coding then I recommend looping the array and adding to a new array if true.
$t = 35;
$jsonobj = '{"ABC":35,"DEF":36,"GEH":34}';
$obj = json_decode($jsonobj);
$match =[];
foreach($obj as $key => $value){
if ($t <= $value) {
$match[] = $key . " => " . $value;
}
}
if(count($match) > 0){
echo implode("<br>\n", $match);
}else{
echo "no matches";
}
See it running here:
https://3v4l.org/NDuD7
Here is my array output in browser which i need to print as row and column associatively in PHPEXCEL.Please suggest me how to export it as row and columns.
FirstArray
(
[0] => EY>Yes
[1] => Media Type>Category B
[2] => Coverage Type>Quote
[3] => Industry Programs>Communications
[4] => Score>1
)
code -
while ($i < count($labels)) {
$j = 0;
while ($j < count($dd1)) {
// for($j=0;$j<count($dd1);$j++) {
$temp = explode(">", $dd1[$j]);
if ($temp[0] == $labels[$i]) {
$name = explode(">", $dd1[$j]);
echo '<pre>First';
print_r($name);
}
$j++;
}
complete code i am trying to print $name array as row and column -
$inn_table = "";
for($i=0;$i<count($labels);$i++) {
for($j=0;$j<count($dd1);$j++) {
$temp = explode(">",$dd1[$j]);
if($temp[0]==$labels[$i]) {
$name = explode(">",$dd1[$j]);
//here i need to print my $name array data
}
}
echo $inn_table;
At first step you can collect once all column names and create first static row, which will work as columns:
foreach($labels[0] as $ind=>$label){
$letter = range('A', 'Z')[$ind];
$tmp = explode('>',$label);
$col_names[] = $tmp[0];
echo $letter.'1'."\r\n";
//$objPHPExcel->getActiveSheet()->setCellValue($letter.'1',$tmp[0]);
echo "Column -> $tmp[0] \r\n";
}
Now you can work with other data:
foreach ($labels as $ind=>$item){
$index = $ind + 2;
foreach($item as $ind2=>$data){
$letter = range('A', 'Z')[$ind2];
echo "$letter$index \r\n";
$val = explode('>',$data);
//$objPHPExcel->getActiveSheet()->setCellValue("$letter$index",$val[1]);
echo "Value at $index -> $val[1] \r\n\r\n";
}
echo "\r\n\r\n";
}
Demo
Note: this code is OK for A..Z range column indexes, for others you need to update this code.
How can I make a two dimensional array with the following example using for loop:
$test = array ('D','D','D','D','C','C','D','D');
output should be like this:
$output = array( 0 => array('D','D','D','D'), 1 => array('D','D'));
thanks for your help.
Here is my code:
$test = array('D','D','D','D', 'C','C','D', 'D');
$output = array();
$myarray = array();
for ($i= 0; $i < count($test); $i++){
if($test[$i] == 'D'){
array_push($myarray , $test[$i]);
} else {
array_push($output,$myarray);
}
}
//OUTPUT: $output = (array( 0 => array('D','D','D','D'), 1 => array('D','D','D','D'));
this can be implemented using only one foreach loop.
<?php
$test = array ('D','C','D','D','D','D','C','C','D','D','C','D');
$temp = array();
$result = array();
foreach($test as $value){
if($value != 'D' && !empty($temp)){
array_push($result, $temp);
$temp = array();
}
else{
array_push($temp, $value);
}
}
if(!empty($temp)){
array_push($result, $temp);
}
print_r($result);
I have some array like this how can i split the key and value in php ?
[A0E4NL014XVM273] => Array
(
[0] => qexixdb
)
[A0E4UK024XVM014_Clone] => Array
(
[0] => pe8w3100
[1] => pe8w3100
)
Tried Query
foreach($vm_array as $vmkey=> $vmvalue){
$varray[] = $vmvalue;
/*foreach($vmvalue as $vmvalue=> $vmvalufmarray){
$vm_array[$vmkey][] = $vmvalufmarray.',';
}*/
}
Expected Output
[A0E4UK024XVM014_Clone] => pe8w3100,pe8w3100
You need to make a function called implode().
foreach($vm_array as $vmkey=> $vmvalue){
$varray[$vmkey] = implode(",", $vmvalue);
}
print_r($varray);
Try using PHP's implode on the inner array;
foreach($vm_array as $vmkey => $vmvalue){
$vm_array[$vmkey] = implode(",", $vmvalue);
}
I tried your example and simulated my own, if my understanding is right, if you want to retain them in the array.
$test = array("[A0E4NL014XVM273]" => array("qexixdb"),"[A0E4UK024XVM014_Clone]" => array("pe8w3100","pe8w3100"));
echo "<pre>";
var_dump($test);
echo "</pre>";
$new_arr_ = array();
foreach($test as $id => $value) {
$new_val = "";
for($i = 0, $ctr_val = count($value); $i < $ctr_val; $i++) {
$old_id = "";
if($id != $old_id) {
if($i < ($ctr_val - 1)) {
$new_val .= $value[$i] . ",";
} else {
$new_val .= $value[$i];
}
$old_id = $id;
}
}
$new_arr_[] = array($id => $new_val);
}
echo "<pre>";
var_dump($new_arr_);
echo "</pre>";
Or if just one array:
$new_arr_ = array();
foreach($test as $key=> $value){
$new_arr_[key] = implode(",", $value);
}
echo "<pre>";
var_dump($new_arr_);
echo "</pre>";
Hope it helps.
I have an array which contains 3 different arrays.
$arrayAll = (
0 => $array1,
1 => $array2,
2 => $array3
);
How can I loop through $arrayAll, displaying the first element of each sub-array(array1,array2,array3) on each itteration?
So, the output will be:
$array1[0],$array2[0],$array3[0],
$array1[1],$array2[1],$array3[1],
$array1[2],$array2[2],$array3[2]
and so on.. until all sublements are fetched.
EDIT:
$addsContent = $Adds->selectAdds(10);
$sharedArticlesContent = $SharedContent->getSharedContent($topic_selected, $filter_selected);
$blogPostsContent = $BlogPosts->getRecentBlogPostsByTopic("business");
$contentArray = array(
$sharedArticlesContent,
$addsContent ,
$blogPostsContent
);
foreach($contentArray as $value)
{
if(count($value)>$maxLength)
{
$maxLength = count($value);
}
}
for($i=0; $i<$maxLength; $i++)
{
foreach($contentArray as $value)
{
if(isset($value[$i]))
{
if($value==$sharedArticlesContent){
$data = $value[$i];
foreach($sharedArticlesContent as $data){
$post_id = $data['id'];
$uploaded_by = $data['uploaded_by'];
$text = $data['text'];
$image = $data['image'];
require 'template1.php';
}
}elseif($value==$addsContent){
//template2
}else{
//template3
}
}
}
}
$maxLength = 0;
foreach($arrayAll as $value)
{
if(count($value)>$maxLength)
{
$maxLength = count($value);
}
}
for($i=0; $i<$maxLength; $i++)
{
foreach($arrayAll as $value)
{
if(isset($value[$i]))
{
echo $value[$i];
}
}
}