Printing a multidimensional array - php

How do I print this array?
<?php
$datos = array(
array('AZUAY', array('P', 'Q'))
);
//print array
foreach ($datos as $dato1) {
foreach ($dato1 as $v2) {
echo $v2;
foreach ($v2 as $v3)
echo $v3;
}
echo "<br>";
}
?>

You can use printf() or var_dump() as a simple pretty-printer:
print_r($datos);
var_dump($datos);
Output:
print_r()
Array ( [0] => Array ( [0] => AZUAY [1] => Array ( [0] => P [1] => Q ) ) )
var_dump()
array(1) { [0]=> array(2) { [0]=> string(5) "AZUAY" [1]=> array(2) { [0]=> string(1) "P" [1]=> string(1) "Q" } } }

Something like this should work:
$datos = array(
array('AZUAY', array('P', 'Q'))
);
function printWeirdArray($array)
{
foreach($array as $i)
{
if(is_array($i))
{
printWeirdArray($i);
}
else
{
print $i."-";
}
}
}
printWeirdArray($datos);
The above code outputs:
AZUAY-P-Q-
Good luck!!

Related

Create new Array under same key from multiple Array in PHP [duplicate]

This question already has answers here:
Transpose 2d array, join second level with commas, and join first level with pipes
(5 answers)
Closed 7 months ago.
I have the following array.
$a = array("Algebra", "Arithmetic");
$b = array("08/01/2020", "08/02/2019");
$c = array("08/01/2020", "08/02/2019");
print_r($a);
print_r($b);
print_r($b);
and the output is
Array(
[0] => Algebra
[1] => Arithmetic
)
Array(
[0] => 08/01/2020
[1] => 08/01/2019
)
Array(
[0] => 08/02/2020
[1] => 08/02/2019
)
And I want Array in the following structure.
Array(
[0] => Algebra,08/01/2020,08/02/2020
[1] => Arithmetic,08/01/2019,08/02/2019
)
I have tried $results = array_merge_recursive($a, $b, $c); but its not giving desire output.
Thanks for help in advance.
A simple foreach loop will suffice
$a = array("Algebra", "Arithmetic");
$b = array("08/01/2020", "08/02/2019");
$c = array("08/01/2020", "08/02/2019");
foreach( $a as $i=>$v ) {
$new[] = sprintf( '%s,%s,%s', $v, $b[$i], $c[$i] );
}
Firstly, when you find yourself using sequentially-named variables it almost always means that they should actually be an array:
$a = array("Algebra", "Arithmetic");
$b = array("08/01/2020", "08/02/2019");
$c = array("08/01/2020", "08/02/2019");
$better_array = [$a, $b, $c];
var_dump($better_array);
Output:
array(3) {
[0]=>
array(2) {
[0]=>
string(7) "Algebra"
[1]=>
string(10) "Arithmetic"
}
[1]=>
array(2) {
[0]=>
string(10) "08/01/2020"
[1]=>
string(10) "08/02/2019"
}
[2]=>
array(2) {
[0]=>
string(10) "08/01/2020"
[1]=>
string(10) "08/02/2019"
}
}
Once they're in an proper array you can use array_column()
$out = [];
for($i=0, $c=count($better_array[0]); $i < $c; ++$i) {
$out[] = array_column($better_array, $i);
}
var_dump($out);
Output:
array(2) {
[0]=>
array(3) {
[0]=>
string(7) "Algebra"
[1]=>
string(10) "08/01/2020"
[2]=>
string(10) "08/01/2020"
}
[1]=>
array(3) {
[0]=>
string(10) "Arithmetic"
[1]=>
string(10) "08/02/2019"
[2]=>
string(10) "08/02/2019"
}
}
And if that comma-delimited string is what you actually want, then use implode():
$out = [];
for($i=0, $c=count($better_array[0]); $i < $c; ++$i) {
$out[] = implode(',', array_column($better_array, $i));
}
var_dump($out);
Output:
array(2) {
[0]=>
string(29) "Algebra,08/01/2020,08/01/2020"
[1]=>
string(32) "Arithmetic,08/02/2019,08/02/2019"
}
Lastly, you should avoid print_r() as it tends to produce misleading output. Eg: https://3v4l.org/ThSLb
You don't get anything built-in for this purpose. You need to build a custom function for this. You can try this-
<?php
$a = array("Algebra", "Arithmetic");
$b = array("08/01/2020", "08/01/2019");
$c = array("08/02/2020", "08/02/2019");
function mergeAssoc()
{
// You can get variable number of arguments|array by this.
$args = func_get_args();
$master = array();
foreach ($args as $arg)
{
foreach ($arg as $i => $v)
{
$master[$i][] = $v;
}
}
return $master;
}
$res = mergeAssoc($a, $b, $c);
print_r($res);
Note: It will return a multidimensional array. Not an array of comma-separated values.
Output:
Array
(
[0] => Array
(
[0] => Algebra
[1] => 08/01/2020
[2] => 08/02/2020
)
[1] => Array
(
[0] => Arithmetic
[1] => 08/01/2019
[2] => 08/02/2019
)
)
and if we use foreach then our desire output will be there with array separated by comma.
foreach ($res as $key => $value) {
$result[] = implode(',', $value);
}
and output of print_r($result); is
Array
(
[0] => Algebra,08/01/2020,08/02/2020
[1] => Arithmetic,08/01/2019,08/02/2019
)

Create an array based on multiple arrays by key

Please help me to combine four arrays in one.
I have to compare main array keys and if the key of the secondaries arrays coincides, add them to the new array.
I tried to combine different php functions, but without the expected result and nothing relevant.
This is the main array.
[0] => Array
(
[1] => "Category1"
[2] => "Category2"
[3] => "Category3"
[4] => "Category4"
)
These are the secondaries arrays
[1] => Array
(
[1] "user_test_[1]" key 1 map to --> [1] Category1
[2] "user_test_[2]" key 2 map to --> [2] Category2
[3] "user_test_[3]" key 3 map to --> [3] Category3
[4] "user_test_[4]" key 4 map to --> [4] Category4
)
[2] => Array
(
2: "user_prod_[2]" key 2 map to --> [2] Category2
4: "user_prod_[4]" key 4 map to --> [4] Category4
)
[3] => Array
(
[3] "user_uat_[3]" key 3 map to --> [3] Category3
[4] "user_uat_[4]" key 4 map to --> [4] Category4
)
New array results in php format:
$array_result = array(
'Category1'=>array(
'user_test_[1]'
),
'Category2'=>array(
'user_test_[2]',
'user_prod_[2]'
),
'Category3'=>array(
'user_test_[3]',
'user_uat_[3]'
),
'Category4'=>array(
'user_test_[4]',
'user_prod_[4]',
'user_uat_[4]'
)
);
Main array in in php format:
$array_cat = array(1=>"Category1", 2=>"Category2", 3=>"Category3", 4=>"Category4");
These are the secondaries arrays in php fromat:
$array_usrtest = array(1=>"user_test_[1]", 2=>"user_test_[2]", 3=>"user_test_[3]", 4=>"user_test_[4]");
$array_usruat = array(3=>"user_uat_[3]", 4=>"user_uat_[4]");
$array_usrprod = array(2=>"user_prod_[2]", 4=>"user_prod_[4]");
Expected results
array{
["Category1"]=>array(1)
{
[0]=>"user_test_[1]"
}
["Category2"]=>array(2)
{
[0]=>"user_test_[2]"
[1]=>"user_prod_[2]"
}
["Category3"]=>array(3)
{
[0]=>"user_test_[3]"
[2]=>"user_uat_[3]"
}
["Category4"]=>array(3)
{
[0]=>"user_test_[4]"
[1]=>"user_prod_[4]"
[2]=>"user_uat_[4]"
}
}
Here is simple solution, which it's not very good, because it is solution for your specific values, but it is good start point and if you want, you can improve it:
<?php
$array_cat = array(1=>"Category1", 2=>"Category2", 3=>"Category3", 4=>"Category4");
$array_usrtest = array(1=>"user_test_[1]", 2=>"user_test_[2]", 3=>"user_test_[3]", 4=>"user_test_[4]");
$array_usruat = array(3=>"user_uat_[3]", 4=>"user_uat_[4]");
$array_usrprod = array(2=>"user_prod_[2]", 4=>"user_prod_[4]");
$result = [];
foreach ($array_cat as $k => $v) {
$result[$v] = []; // fill result array with categories
}
$values = array_merge($array_usrtest, $array_usruat, $array_usrprod); // merge arrays into one (all items)
foreach ($values as $k => $value) { // iterate over all values
preg_match('!\d+!', $value, $match); // get index in [index]
$result['Category' . $match[0]][] = $value; // append values to category
}
echo '<pre>';
var_dump($result);
Result:
array(4) {
["Category1"]=>
array(1) {
[0]=>
string(13) "user_test_[1]"
}
["Category2"]=>
array(2) {
[0]=>
string(13) "user_test_[2]"
[1]=>
string(13) "user_prod_[2]"
}
["Category3"]=>
array(2) {
[0]=>
string(13) "user_test_[3]"
[1]=>
string(12) "user_uat_[3]"
}
["Category4"]=>
array(3) {
[0]=>
string(13) "user_test_[4]"
[1]=>
string(12) "user_uat_[4]"
[2]=>
string(13) "user_prod_[4]"
}
}
I would say merge all array and then build the category array
$arrs = array_merge($array_usrtest, $array_usruat, $array_usrprod);
$categories = array_fill_keys($array_cat, []);
array_walk($arrs, function($item) use (& $categories){
preg_match('#(.*)\[(\d)\]#', $item, $matches);
$key = $matches[2];
$categories['Category' . $key][] = $item;
});
print_r($categories);
Based on yours answers:
<?php
$array_cat = array(1=>"Category1", 2=>"Category2", 3=>"Category3", 4=>"Category4");
$array_usrtest = array(1=>"user_test_[1]", 2=>"user_test_[2]", 3=>"user_test_[3]", 4=>"user_test_[4]");
$array_usruat = array(3=>"user_uat_[3]", 4=>"user_uat_[4]");
$array_usrprod = array(2=>"user_prod_[2]", 4=>"user_prod_[4]");
$values = array_merge($array_usrtest, $array_usruat, $array_usrprod);
$result = [];
foreach ($array_cat as $key => $cat) {
foreach ($values as $k => $value) { // iterate over all values
preg_match('!\d+!', $value, $match); // get index in [index]
if ($key == $match[0]) {
$result[$cat][] = $value; //append values to category
}
}
}
echo '<pre>';
var_dump($result);
?>
Result:
array(4) {
["Category1"]=>
array(1) {
[0]=>
string(13) "user_test_[1]"
}
["Category2"]=>
array(2) {
[0]=>
string(13) "user_test_[2]"
[1]=>
string(13) "user_prod_[2]"
}
["Category3"]=>
array(2) {
[0]=>
string(13) "user_test_[3]"
[1]=>
string(12) "user_uat_[3]"
}
["Category4"]=>
array(3) {
[0]=>
string(13) "user_test_[4]"
[1]=>
string(12) "user_uat_[4]"
[2]=>
string(13) "user_prod_[4]"
}
}

array_combine removed duplicate array

Array_combine remove duplicate array
<?php
$a1=array("red","red");
$a2=array("blue","yellow");
print_r(array_combine($a1,$a2));
?>
This code give output : Array ( [red] => yellow )
But I want output like this: Array ( [red] => blue [red] => yellow )
The Andreas answer is correct. you can do this:
$a1 = ['red'];
$a2 = ['blue', 'yellow'];
$a3 = [];
foreach($a1 as $item1) {
foreach($a2 as $item2) {
$a3[$item1][] = $item2;
}
}
print_r($a3);
The output:
array(1) {
["red"]=>
array(2) {
[0]=>
string(4) "blue"
[1]=>
string(6) "yellow"
}
}

Combine multiple arrays with identica keys and different values

I have this two arrays that are generated in two foreach loops and I want to set the first array as keys and the second one as values.
after I use this code
foreach ($difference AS $j) {
$fv = $cate->getFilterValueByFeatureID($j);
foreach ($fv AS $z) {
$array = array(
$j => $z
);
var_dump($array);
}
}
this is what I get
array(1) {
[6]=>
int(15)
}
array(1) {
[6]=>
int(20)
}
array(1) {
[8]=>
int(26)
}
array(1) {
[8]=>
int(27)
}
array(1) {
[8]=>
int(33)
}
and I want this result
array(1){
[6] => array(
[0] => 15
[1] => 20
)
array(1){
[8] => array(
[0] => 26
[1] => 27
[2] => 33
)
Like this (untested)
$result = [];
foreach ($difference AS $j) {
$fv = $cate->getFilterValueByFeatureID($j);
foreach ($fv AS $z) {
if(!isset($result[$j])) $result[$j] = [];
$result[$j][] = $z;
}
}
var_dump($result);

How to update data if array is empty?

I want to update empty array .So I check is empty and want to update the original variable.So I tried by & but it doesn't change empty array $array2 .I had tried many hours but not working!!!
<?php
$array1 = array('one','two','three');
$array2 = array();
$array3 = array ('four');
$array4 = array ('five','six');
check([&$array1,&$array2,&$array3,&$array4]);
function check($arr){
foreach ($arr as $k=>$value) {
if(empty($value)){
$arr[$k][] = "nothing";
return $arr[$k];
}
else{
return $arr[$k];
}
}
}
var_dump($array2);
//actual output : empty
//expect output : 0 => string 'nothing'
?>
Have a look following code.
$array1 = array('one','two','three');
$array2 = array();
$array3 = array ('four');
$array4 = array ('five','six');
$arr11=check([&$array1,&$array2,&$array3,&$array4]);
function check($arr){
foreach ($arr as $k=>$value) {
print_r($value);
if(empty($value)){
$arr[$k] = "nothing";
}
else{
$arr[$k];
}
}
return $arr;
}
print_r($arr11);
var_dump($array2);
//actual output : empty
//expect output : 0 => string 'nothing'
It will display following result.
Array
(
[0] => Array
(
[0] => one
[1] => two
[2] => three
)
[1] => nothing
[2] => Array
(
[0] => four
)
[3] => Array
(
[0] => five
[1] => six
)
)
string(7) "nothing"
This will work for you:
<?php
$array1 = array('one','two','three');
$array2 = array();
$array3 = array ('four');
$array4 = array ('five','six');
list($array1, $array2, $array3, $array4) = check([$array1,$array2,$array3,$array4]);
function check($arr){
foreach ($arr as $k => $value) {
if(empty($value)){
$arr[$k] = "nothing";
}
}
return $arr;
}
var_dump($array2);
//actual output : string(7) "nothing"
//expect output : string(7) "nothing"
?>
You've written your function very strangely, actually.
UPDATE. Output of other arrays instead of only one.
var_dump($array1, $array2, $array3, $array4);
array(3) {
[0]=>
string(3) "one"
[1]=>
string(3) "two"
[2]=>
string(5) "three"
}
string(7) "nothing"
array(1) {
[0]=>
string(4) "four"
}
array(2) {
[0]=>
string(4) "five"
[1]=>
string(3) "six"
}

Categories