How do I retrieve the values from a multidimensional array? - php

Do you know a good method or tips on how do I retrieve the values (as string) from this multi dimensional array:
array (size=5)
0 =>
array (size=1)
'email' =>
array (size=1)
0 => string 'lavidabonita#gmail.com' (length=27)
1 =>
array (size=1)
'email' =>
array (size=1)
0 => string 'iancasillasbuffon#gmail.com' (length=27)
2 =>
array (size=1)
'email' =>
array (size=1)
0 => string 'eddynvg#hotmail.com' (length=19)
3 =>
array (size=1)
'email' =>
array (size=1)
0 => string 'dolphin23#dolphin.net' (length=21)
4 =>
array (size=1)
'email' =>
array (size=1)
0 => string 'dolphin#dolphin.org' (length=19)

try with this:
function fn($arg){
foreach ($arg as $key => $val){
if (is_array($val)){
fn($val);
}
else{
echo "$key : $val\n";
}
}
}
fn($data);

Related

Remove nested array in nested foreach PHP

I have a problem in presenting data from a database using nested foreach.
in the code that I worked on, I almost managed to provide data output from the transaction database as I expected in the form of a multidimensional array which in the second nested array contains the product sold and removes the duplicate data of the product sold.
and my problem is how to delete or not display the last nested array?
this is my code:
public function resultSetFP()
{
$this->execute();
$res = $this->stmt->fetchAll(PDO::FETCH_GROUP | PDO::FETCH_ASSOC);
foreach($res as $key => $value){
foreach($value as $v){
foreach($v as $b){
$res[$key][$b]=$b;
}
}
}
return $res;
}
output code:
'P2-6/01/2018/094909/0001' =>
array (size=5)
0 =>
array (size=1)
'Bid' => string 'Dagadu Bocah' (length=12)
1 =>
array (size=1)
'Bid' => string 'HirukPikuk' (length=10)
2 =>
array (size=1)
'Bid' => string 'HirukPikuk' (length=10)
'Dagadu Bocah' => string 'Dagadu Bocah' (length=12)
'HirukPikuk' => string 'HirukPikuk' (length=10)
'P2-6/01/2018/095825/0002' =>
array (size=4)
0 =>
array (size=1)
'Bid' => string 'Dagadu' (length=6)
1 =>
array (size=1)
'Bid' => string 'HirukPikuk' (length=10)
'Dagadu' => string 'Dagadu' (length=6)
'HirukPikuk' => string 'HirukPikuk' (length=10)
I expected to remove the third nested array
leaving the data that I marked:
'P2-6/01/2018/094909/0001' =>
array (size=5)
//removing this array
0 =>
array (size=1)
'Bid' => string 'Dagadu Bocah' (length=12)
1 =>
array (size=1)
'Bid' => string 'HirukPikuk' (length=10)
2 =>
array (size=1)
'Bid' => string 'HirukPikuk' (length=10)
//leaving this
'Dagadu Bocah' => string 'Dagadu Bocah' (length=12)
'HirukPikuk' => string 'HirukPikuk' (length=10)
Try that:
foreach ($res as $key => $value) {
foreach ($value as $i => $v) {
foreach ($v as $b) {
$res[$key][$b] = $b;
}
unset($res[$key][$i]);
}
}

MongoDb get only value but not key using php

i am using mongodb and my query returns only a key value
the following is the format of the var_dump
<pre>array (size=471)
0 =>
array (size=1)
'sno' => string '162230' (length=6)
1 =>
array (size=1)
'sno' => string '165333' (length=6)
2 =>
array (size=1)
'sno' => string '181312' (length=6)
3 =>
array (size=1)
'sno' => string '181313' (length=6)
4 =>
array (size=1)
'sno' => string '181314' (length=6)
5 =>
array (size=1)
'sno' => string '181315' (length=6)
6 =>
array (size=1)
'sno' => string '181316' (length=6)
7 =>
array (size=1)
'sno' => string '181317' (length=6)
8 =>
array (size=1)
'sno' => string '181318' (length=6)
9 =>
array (size=1)
'sno' => string '181319' (length=6)
10 =>
array (size=1)
'sno' => string '181320' (length=6)</pre>
I do not want key . all i want is a array of values without a loop.
Solved it.
array_column($arr,"sno")

Illegal string offset in foreach

I'm trying to loop in the second array with this code
<?php
foreach ($categories[1] as $category_cat) { ?>
<li><span><?php echo $category_cat['name']; ?></span></li>
<?php } ?>
And I get the "Illegal string offset in..." error. I know that addind the [1] the the foreach might be the error but how I could make the loop in the second array then ?
Array content
array (size=2)
0 =>
array (size=4)
'name' => string 'Brands' (length=7)
'children' =>
array (size=12)
0 =>
array (size=3)
...
1 =>
array (size=3)
...
2 =>
array (size=3)
...
3 =>
array (size=3)
...
4 =>
array (size=3)
...
5 =>
array (size=3)
...
6 =>
array (size=3)
...
7 =>
array (size=3)
...
8 =>
array (size=3)
...
9 =>
array (size=3)
...
10 =>
array (size=3)
...
11 =>
array (size=3)
...
'column' => string '1' (length=1)
'href' => string 'url here' (length=78)
1 =>
array (size=4)
'name' => string 'Catégories' (length=11)
'children' =>
array (size=7)
0 =>
array (size=3)
...
1 =>
array (size=3)
...
Just try to loop $categories[1]['children']:
foreach ( $categories[1]['children'] as $category_cat ) { }

How to combine/merge an multidimension array that has same keys in PHP?

So I have this array which the vardump looks like this:
array (size=4)
0 =>
array (size=1)
'field_4' =>
array (size=1)
0 => string 'item-1' (length=6)
1 =>
array (size=1)
'field_4' =>
array (size=1)
0 => string 'info-1' (length=6)
2 =>
array (size=1)
'field_5' =>
array (size=1)
0 => string 'item-2' (length=6)
3 =>
array (size=1)
'field_5' =>
array (size=1)
0 => string 'info-2' (length=6)
So I am trying to combine the array with the same key for example 'field_4' would be merge/combined into an array that has 2 items "item-1" and "info-1".
So the end result I would like is this:
array (size=2)
0 =>
array (size=1)
'field_4' =>
array (size=2)
0 => string 'item-1' (length=6)
1 => string 'info-1' (length=6)
1 =>
array (size=1)
'field_5' =>
array (size=1)
0 => string 'item-2' (length=6)
1 => string 'info-2' (lenght=6)
So is there a PHP convenience function to handle this or do I have to rebuild the array?
Thanks for looking.
Just iterate over the input array, building the merged array:
$merged = array();
foreach ($input as $a)
foreach ($a as $k => $v)
foreach ($v as $v2)
$merged[$k][] = $v2;
And then flatten it into your weird required output:
$flattened = array();
foreach ($merged as $k => $v)
$flattened[] = array($k => $v);
Input:
$input = array(
array('field_4' => array('item-1')),
array('field_4' => array('info-1')),
array('field_5' => array('item-2')),
array('field_5' => array('info-2')),
);
Output:
array(
array('field_4' => array('item-1', 'info-1')),
array('field_5' => array('item-2', 'info-2')),
)
When dumping output, print_r or var_export make for a much more readable example than var_dump

php nested array foreach or similar to iterate through a bidimensional array

i´d like how to iterate through this array to print something like this in an html list ( a foreach or any other loop should do the work...). I´m completely stuck..!
Caballos Soledad'
'Paseo a caballo'
...........
...........
...........
Rancho Valle
'Camping'
'Paseo a caballo'
array (size=10)
40 =>
array (size=2)
'proveedor' => string 'Caballos Soledad' (length=26)
'servicio' =>
array (size=1)
0 => string 'Paseo a caballo' (length=15)
41 =>
array (size=2)
'proveedor' => string 'Caballos Segundo' (length=25)
'servicio' =>
array (size=1)
0 => string 'Paseo a caballo' (length=15)
64 =>
array (size=2)
'proveedor' => string 'Cerro' (length=10)
'servicio' =>
array (size=1)
0 => string 'Camping' (length=7)
39 =>
array (size=2)
'proveedor' => string 'Caballos Nieto' (length=19)
'servicio' =>
array (size=1)
0 => string 'Paseo a caballo' (length=15)
174 =>
array (size=2)
'proveedor' => string 'Hacienda' (length=21)
'servicio' =>
array (size=2)
0 => string 'Paseo a caballo' (length=15)
1 => string 'Camping' (length=7)
49 =>
array (size=2)
'proveedor' => string 'Campo ' (length=10)
'servicio' =>
array (size=1)
0 => string 'Camping' (length=7)
241 =>
array (size=2)
'proveedor' => string 'Lodge' (length=12)
'servicio' =>
array (size=1)
0 => string 'Paseo a caballo' (length=15)
258 =>
array (size=2)
'proveedor' => string 'Modesto' (length=14)
'servicio' =>
array (size=1)
0 => string 'Paseo a caballo' (length=15)
294 =>
array (size=2)
'proveedor' => string 'Rancho Valle ' (length=18)
'servicio' =>
array (size=2)
0 => string 'Camping' (length=7)
1 => string 'Paseo a caballo' (length=15)
38 =>
array (size=2)
'proveedor' => string 'Caballos Jose ' (length=22)
'servicio' =>
array (size=1)
0 => string 'Paseo a caballo' (length=15)
I'b been searching for too long... but not find a proper way to do that.
Many thanks in advance
function my_print_r($array){
echo '<ul>';
foreach ($array as $key => $value){
echo '<li>' . $key;
if (is_array($value)){
my_print_r($value);
}
else echo ' : ' . $value;
echo '</li>';
}
echo '</ul>';
}
Hope this will work, just change the formatting as You like :)
So, i changed the format as i like
function my_print_r($array){
foreach ($array as $key => $value){
if (is_array($value)){
echo '<ul>';
my_print_r($value);
echo '</ul>';
}
else echo '<li> ' . $value;
}
echo '</li>';
// echo '</ul>';
}

Categories