I want to create an assosiative array which it will have as one of its elements an other array (not assosiative)
$temp= array(
'elem1' => $data1,
'elem2' => $data2,
'elem3' => $data5,
);
$data2 is a simple array already constructed. However, I cannot find the right syntax for doing that. By applying the classic following I cannot get the values of elem2.
while ($element=each($temp))
{
echo $element['key'];
echo " - ";
echo $element['value'];
echo "<br/>";
}
It's not quite clear from your question, but depending on the structure of $data2, printing its values separated by commas may suffice:
foreach ($temp as $key => $value) {
echo $key, ' - ';
if (is_array($value)) {
echo implode(',', $value);
}
else {
echo $value;
}
echo '<br/>';
}
try this
$data = ['elem1' => '1', 'elem2' => ['red','blue','orange' ],'elem3' => '3'];
$tmp ='';
foreach ($data as $key => $value ) {
$tmp .= $key.' - ' ;
if (is_array($value)) {
$tmp.= implode(',', $value).' ';
}else {
$tmp .= $value. ' ';
}
}
echo json_encode($tmp);
You can echo the values in this way:
foreach($temp as $key => $value) {
echo $key;
echo " - ";
if(is_array($value)) {
echo "Array: # ";
foreach($value as $value_key => $value_value) {
echo $value_key." - ".$value_value." # ";
}
} else {
echo $value;
}
echo "<br/>";
}
Related
I have a nested array on this link Array Sample
I am using code below to parse this, but second and beyond depth it's returning nothing. However tried with recursive function.
printAllValues($ArrXML);
function printAllValues($arr) {
$keys = array_keys($arr);
for($i = 0; $i < count($arr); $i++) {
echo $keys[$i] . "$i.{<br>";
foreach($arr[$keys[$i]] as $key => $value) {
if(is_array($value))
{
printAllValues($value);
}
else
{
echo $key . " : " . $value . "<br>";
}
}
echo "}<br>";
}
}
What I am doing Wrong? Please help.
Version of J. Litvak's answer that works with SimpleXMLElement objects.
function show($array) {
foreach ($array as $key => $value) {
if (!empty($value->children())) {
show($value);
} else {
echo 'key=' . $key . ' value=' . $value. "<br>";
}
}
}
show($ArrXML);
You can use recurcive function to print all values:
function show($array) {
foreach( $array as $key => $value) {
if (is_array($value)) {
show($value);
} else{
echo 'key=' . $key . ' value=' . $value. "<br>";
}
}
}
show($ArrXML);
This might be duplicate of several questions.
I am having an dynamic array
Array
(
[gender] => Male
[age] => Array
(
[0] => 18-25 years
[1] => 26-32 years
)
[name] => Nagesh
[emailid] => nagesh#ccomsys.net
)
I want to store this array in HTML table as key in one column and value in another
Example.
__________________________________
Gender | Male
_______|__________________________
Age | 18-25 years, 26-32 years
_______|__________________________
Name | Nagesh
_______|__________________________
Email | nagesh#ccomsys.net
_______|__________________________
I am getting key and value for associative array but how can I implode age array.
I tried below code
$post = $this->request->post;
$age_array = array_column($post, 'age');
$age = '';
foreach($post as $key => $value) {
if($key == "age"){
$age = implode(",", $age_array);
}
echo $age. "<br>";
echo $key. "<br>";
if($key != "age"){
echo $value. "<br>";
} else {
echo $age;
}
}
Thanks advance.
Just a few minor adjustments:
I added ucfirst() to the first column to match your desired output.
I used an inline conditional to check if array values were arrays, to appropriately imploded array before echoing.
Code: (Demo)
$post=[
'gender'=>'Male',
'age'=>['18-25 years','26-32 years'],
'name'=>'Nagesh',
'emailid'=>'nagesh#ccomsys.net'
];
echo '<table border=1>';
foreach($post as $k=>$v){
echo '<tr>';
echo '<td>',ucfirst($k),'</td><td>',(is_array($v)?implode(', ',$v):$v),'</td>';
echo '</tr>';
}
echo '</table>';
Unrendered Output:
<table border=1>
<tr>
<td>Gender</td><td>Male</td>
</tr>
<tr>
<td>Age</td><td>18-25 years, 26-32 years</td>
</tr>
<tr>
<td>Name</td><td>Nagesh</td>
</tr>
<tr>
<td>Emailid</td><td>nagesh#ccomsys.net</td>
</tr>
</table>
Rendered output available via runnable snippet:
<table border=1><tr><td>Gender</td><td>Male</td></tr><tr><td>Age</td><td>18-25 years, 26-32 years</td></tr><tr><td>Name</td><td>Nagesh</td></tr><tr><td>Emailid</td><td>nagesh#ccomsys.net</td></tr></table>
It is working like what you expecting:
foreach($post as $key => $value) {
echo $key. "<br>";
if($key == "age"){
$age = implode(",", $post[$key]);
echo $age. "<br>";
}
if($key != "age"){
echo $value. "<br>";
} else {
//echo $age;
}
}
If you get multiple arrays means it will work. I changed small things in your condition:
foreach($post as $key => $value) {
echo $key. "<br>";
if(is_array($post[$key])){
$age = implode(",", $post[$key]);
echo $age. "<br>";
}
if(!is_array($post[$key])){
echo $value. "<br>";
} else {
//echo $age;
}
}
You have an array of data and you want to display it on a table. Instead of using implode(), why not just concatenate the two strings together?
Array
(
[gender] => Male
[age] => Array
(
[0] => 18-25 years
[1] => 26-32 years
)
[name] => Nagesh
[emailid] => nagesh#ccomsys.net
)
PHP code:
$post = $this->request->post;
foreach($post as $key => $value) {
if($key == "age"){
$age = $value[0].','.$value[1];
}
echo $age. "<br>";
echo $key. "<br>";
if($key != "age"){
echo $value. "<br>";
} else {
echo $age;
}
}
Replace the following code:
if($key == "age"){
$age = implode(",", $age_array);
}
with
if($key == "age"){
$age = implode(",", $value);
}
You need to code as follows...
foreach($post as $value){
$gender = $value['gender'];
$age = implode(',',$value['age']);
$name = $value['name'];
$email = $value['email'];
}
Change Your foreach loop with this one.
foreach($test as $key => $value) {
$age = '';
if($key == "age"){
$age = implode(",", $value);
}
echo $age. "<br>";
echo $key. "<br>";
if($key != "age"){
echo $value. "<br>";
} else {
echo $age;
}
}
$output = [];
//$array - your input data
foreach ($array as $key => $item) {
$output[$key] = (is_array($item)) ? implode(', ', $item) : $item;
}
//print output
I have the following array:
$array=array("string",array(1,2,3),true,"php");
and I want to print indexes like:
0=>string
1.0=>1
1.1=>2
1.2=>3
2=>true
3=>php
<?php
$array=array("string",array(1,2,3),true,"php");
foreach($array as $key=>$value)
{
if(is_array($value))
{
foreach($value as $childkey=>$childvalue)
{
echo $key . "." . $childkey . "=>" . $childvalue . "\n";
}
}
elseif(is_bool($value))
{
echo $key . "=>" . ($value ? "true" : "false") . "\n";
}
else
{
echo $key . "=>" . $value . "\n";
}
}
Output:
0=>string
1.0=>1
1.1=>2
1.2=>3
2=>true
3=>php
Try this
<?php
$array=array("string",array(1,2,3),true,"php");
foreach($array as $key=>$value){
if(is_array($value)){
foreach($value as $key1=>$value1){
echo $key.".".$key1." => ".$value1."</br>";
}
}
else{
echo $key." => ".$value."</br>";
}
}
<?php
$array=array("string",array(1,2,3),"true","php");
foreach($array as $key=>$value){
if(is_array($value)){
foreach($value as $key1=>$loop){
echo $key.'.'.$key1 .'=>'.$loop."<br>";
}
}else{
echo $key .'=>'.$value."<br>";
}
}
?>
Try this code
$array=array("string",array(1,2,3),true,"php");
foreach($array as $key=>$val){
if(is_array($val)){
foreach($val as $key1=>$val1){
echo $key.'.'.$key1 .'=>'.$val1.'<br/>';
}
}else{
echo $key .'=>'.$val.'<br/>';
}
}
You can try this-
<?php
$arr=array("string",array(1,2,3),true,"php");
$res=convArray($arr);
foreach($res as $k=>$v){
echo $k."=>".$v."\n";
}
function convArray($arr)
{
foreach($arr as $k1=>$v1){
if(is_array($v1)){
foreach($v1 as $k2=>$v2){
$res[$k1.'.'.$k2]=$v2;
}
}else{
$res[$k1]=$v1;
}
}
return $res;
}
?>
Try this:
<?php
$array=array("string",array(1,2,3,array('a','b','c')),true,"php");
$kt = array();
function showarray($arr,$k) {
global $kt;
foreach($arr as $key => $v) {
$nk = $k == '' ? $key:$k.'.'.$key;
if(is_array($v)) {
showarray($v,$nk);
} else {
$kt[$nk] = $v;
}
}
}
showarray($array,"");
print_r($kt);
I built a foreach which loops through a specific folder and gets an array:
foreach ($watchFolder as $key => $value) {
echo '<pre>';
print_r($value);
echo '</pre>';
if((in_array('xml', $watchFolder))) {
echo "true";
} else {
echo "false";
}
foreach ($value as $key => $valueSub) {
$currentWatchPath2 = $currentWatchPath . '\\' . $key;
foreach ($valueSub as $content) {
$currentWatchPath3 = $currentWatchPath2 . '\\' . $content;
}
}
}
If I print out the $watchfolder I get this:
Array (
[test] => Array([videos] => Array()
[xml] => Array())
[test2] => Array([json] => Array([0] => test.json)
[videos] => Array())
)
If I try this:
dd(in_array('xml', $value));
it returns null, whereas it should return true or?
I am really confused at the moment, so I appreciate every tips and suggestion.
xml is a key of $value array so I think you should check like this:
foreach ($watchFolder as $key => $value) {
echo '<pre>';
print_r($value);
echo '</pre>';
if(isset($value['xml'])) { // <--- here is the changed.
echo "true";
} else {
echo "false";
}
foreach ($value as $key => $valueSub) {
$currentWatchPath2 = $currentWatchPath . '\\' . $key;
foreach ($valueSub as $content) {
$currentWatchPath3 = $currentWatchPath2 . '\\' . $content;
}
}
}
In replace of in_array('xml', $watchFolder), it would be used in_array('xml', $value)
foreach ($watchFolder as $key => $value) {
echo '<pre>';
print_r($value);
echo '</pre>';
if((in_array('xml', $value))) {
echo "true";
} else {
echo "false";
}
foreach ($value as $key => $valueSub) {
$currentWatchPath2 = $currentWatchPath . '\\' . $key;
foreach ($valueSub as $content) {
$currentWatchPath3 = $currentWatchPath2 . '\\' . $content;
}
}
}
my problem is, i have this script
foreach ($arr1 as $k => $val) {
if (in_array($val, $arr2)) {
echo 'Obsazeno <br>';
} else {
echo $val . "<BR>";
}
}
Where $arr1 is generated array of days and $arr2 is array from mysql results. Actually it works fine. If one of days from $arr1 is in database, it will echo "Obsazeno" instead of day value.
But i need small upgrade. Script check if $val is in $arr2 and i need small counting contidion that will do this:
If $val is in $arr2 once - it will echo $val.
If $val is in $arr2 twice - it will echo $val.
But if $val is in $arr2 thrice - it will echo "Obsazeno".
I hope you understand my question.
I quest, it is possible by array_count_values. I try it by myself but no success.
You can achieve this with array_count_values.
$values = array_count_values($arr2);
foreach ($arr1 as $k => $val) {
if (array_key_exists($val, $values) && $values[$val] >= 3) {
echo 'Obsazeno <br>';
} else {
echo $val . '<br>';
}
}
try
$i=0;
foreach($arr1 as $k=>$val) {
if(in_array($val,$arr2)) {
$i++;
if($i == 3) {
echo 'Obsazeno <br>';
}
else {
echo $val."<BR>";
}
}
}
$data = array();
foreach($arr1 as $k=>$val)
{
if(!array_key_exists($val,$data)){
$data[$val] = 0;
}
if(in_array($val,$arr2))
{
$data[$val]++;
if($data[$val]==3){
echo 'Obsazeno <br>';
}else{
echo $val."<BR>";
}
}
else
{
echo $val."<BR>";
}
}