i have an multidimensional array given below - php

i have an array like this
$x = array("1" =>array("PHP","code"), "foo" =>array("bar", 5 ,11));
for($i=1;$i<count($x);$i++)
{
foreach($x[$i] as $key=>$value){
echo $key.$value;
}
echo "<br>";
}
and i want output like this:
1.php
1.code
foo.bar
foo.5
foo.11
how could i get this out put.

You are almost there:
foreach ($x AS $key => $val) {
foreach ($val AS $sub) {
echo $key . '.' . $sub . '<br>';
}
}

Loop through each array, like so;
<?php
$x = array("1" =>array("PHP","code"),
"foo" =>array("bar", 5 ,11)
);
foreach($x as $key => $a) {
foreach($a as $c) {
echo $key . "." . $c . PHP_EOL;
}
}
https://eval.in/198869

<?php
$x = array("1" =>array("PHP","code"), "foo" =>array("bar", 5 ,11));
foreach($x as $m=>$n){
foreach($n as $y=>$x){
$arrayList[]= $m.".".strtolower($x);
}
}
echo "<pre>";
print_r($arrayList);
echo "</pre>";
?>

You have to put the line break <br> inside the inner loop, like this:
$x = array( "1" => array("PHP","code"), "foo" => array("bar", 5 ,11));
foreach($x as $key=>$val){
foreach($val as $value){
echo $key.".".$value."<br>";
}
}

Related

How to Print multilevel array index in php

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);

Php foreach in_array keeps returning false

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;
}
}
}

Array inside assosiative array

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/>";
}

Count duplicate values in PHP array

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>";
}
}

PHP - using operators to compare array values

If I have an array:
$array = array ( [rock] => 40, [pop] => 30, [rap] => 20 ) etc...
how can I do something like:
foreach key in $array
{
if (array_value > 30) echo "> 30:" . $array_key . "<br>";
if (array_value < 30) echo "< 30:" . $array_key . "<br>";
}
So that the result would be:
> 30:rock<br>
< 30:pop<br>
< 30:rap<br>
Thanks! I hope this makes sense...
foreach ($array as $key => $value) {
if ($value ...) echo $key...
else if ($value ...) echo $key...
...
}
deceze's answer is correct in general, but more specifically, the following code should work:
foreach ($array as $key => $value) {
if ($value > 30) {
echo '> 30:' . $key . '<br>';
} elseif ($value <= 30) { // Changed this to <= to cover the case of $value = 30
echo '< 30:' . $key . '<br>';
}
}

Categories