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]);
}
}
Related
I am trying to convert following array:
array (size=6)
0 =>
array (size=1)
1 => string '611' (length=3)
1 =>
array (size=1)
1 => string '610' (length=3)
2 =>
array (size=1)
1 => string '608' (length=3)
3 =>
array (size=1)
1 => string '607' (length=3)
4 =>
array (size=1)
1 => string '606' (length=3)
5 =>
array (size=1)
1 => string '605' (length=3)
Expected output: 611, 610, 608, 607, 606, 605
I tried to do this:
foreach ($array as $sub) {
$str = implode(',', $sub);
}
but I got 605
Could you explain what I am doing wrong
In your code there is one error in foreach. You always replace previous value, you just need to do:
foreach ($array as $sub) {
$str .= implode(',', $sub);
}
I have this result from var_dump for a multidimensional array:
array (size=6)
'sambalpur.in.net' =>
array (size=2)
'classkey' => string 'indotnet' (length=8)
'status' => string 'available' (length=9)
'sambalpur.com' =>
array (size=2)
'classkey' => string 'domcno' (length=6)
'status' => string 'regthroughothers' (length=16)
'sambalpur.info' =>
array (size=2)
'classkey' => string 'dominfo' (length=7)
'status' => string 'regthroughothers' (length=16)
'sambalpur.net' =>
array (size=2)
'classkey' => string 'dotnet' (length=6)
'status' => string 'regthroughothers' (length=16)
'sambalpur.biz' =>
array (size=2)
'classkey' => string 'dombiz' (length=6)
'status' => string 'available' (length=9)
'sambalpur.in' =>
array (size=2)
'classkey' => string 'dotin' (length=5)
'status' => string 'regthroughothers' (length=16)
Now say I want to shift this specific array to the beginning of array:
'sambalpur.biz' =>
array (size=2)
'classkey' => string 'dombiz' (length=6)
'status' => string 'available' (length=9)
I have tried:
array_unshift($array,array('sambalpur.biz'));
But what I am getting is like this:
array (size=7)
0 =>
array (size=1)
0 => string 'sambalpur.biz' (length=13)
'sambalpur.in.net' =>
array (size=2)
'classkey' => string 'indotnet' (length=8)
'status' => string 'available' (length=9)
'sambalpur.com' =>
array (size=2)
'classkey' => string 'domcno' (length=6)
'status' => string 'regthroughothers' (length=16)
'sambalpur.info' =>
array (size=2)
'classkey' => string 'dominfo' (length=7)
'status' => string 'regthroughothers' (length=16)
'sambalpur.net' =>
array (size=2)
'classkey' => string 'dotnet' (length=6)
'status' => string 'regthroughothers' (length=16)
'sambalpur.biz' =>
array (size=2)
'classkey' => string 'dombiz' (length=6)
'status' => string 'available' (length=9)
'sambalpur.in' =>
array (size=2)
'classkey' => string 'dotin' (length=5)
'status' => string 'regthroughothers' (length=16)
What is the correct way to shift the array?
I thought I had done this before but couldn't find a duplicate:
$array = array_splice($array,
array_search('sambalpur.biz', array_keys($array)), 1) + $array;
Get a numerically indexed array of the keys with array_keys()
Search the returned array for sambalpur.biz with array_search()
Use the returned index to cut out that element with array_splice()
Add that to the existing array
Along the same line as Don't Panic:
$array = array_merge(array('sambalpur.biz' => $array['sambalpur.biz']), $array);
No need to unset as the order of insertion dictates which key overwrites the other so this one overwrites the previous one.
You can uksort the array, using the specific key you want to move to the beginning in the comparison function.
$key = 'sambalpur.biz';
uksort($array, function($a, $b) use ($key) {
if ($a == $key) return -1;
if ($b == $key) return 1;
return 0;
});
This should move that item to the beginning without changing the order of the other items.
Another possibility is to remove the child array, and then merge the main array back onto it.
$key = 'sambalpur.biz';
$x = $array[$key];
unset($array[$key]);
$array = array_merge([$key => $x], $array);
The issue seems to stem from array_unshift() reindexing elements you are passing. If you wanted to prepend your second array to your first array though and preserve indexes, you can use the + operator ($firstArray = $secondArray + $firstArray);
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);
I wish to add string keys to my inner PHP arrays. So, I want to convert this:
array (size=2)
0 => array (size=3)
0 => string 'X705' (length=4)
1 => string 'X723' (length=4)
2 => string 'Sue' (length=0)
1 => array (size=3)
0 => string 'X714' (length=4)
1 => string 'X721' (length=4)
2 => string 'John' (length=0)
to this:
array (size=2)
0 =>
array (size=3)
'code1' => string 'X705' (length=4)
'code2' => string 'X723' (length=4)
'name' => string 'Sue' (length=0)
1 =>
array (size=3)
'code1' => string 'X714' (length=4)
'code2' => string 'X721' (length=4)
'name' => string 'John' (length=0)
I think I need to use array_walk but cannot fathom it out. Any help appreciated.
You can use array_map for that purpose:
$newarray = array_map(function($x) {
return array("code1" => $x[0], "code2" => $x[1], "name" => $x[2]);
}, $array);
where $array is your input array.
Start with this:
foreach ($array as $key=>$item) {
$item['code1']=$item[0];
unset($item[0]);
$item['code2']=$item[1];
unset($item[1]);
$item['name']=$item[2];
unset($item[2]);
$array[$key]=$item;
}
I would use array_map() but here's an alternate:
foreach($array as &$v) {
$v = array_combine(array('code1','code2','name'), $v);
}
I have a array:
$array1 = array
0 =>
array
0 => string 'biodata' (length=7)
1 => string 'family_name' (length=11)
1 =>
array
0 => string 'biodata' (length=7)
1 => string 'first_name' (length=10)
2 =>
array
0 => string 'biodata_education' (length=17)
1 => string 'subject' (length=7)
3 =>
array
0 => string 'biodata_education' (length=20)
1 => string 'year' (length=5)
which need to converted like:
array
biodata =>
array
0 => string 'family_name' (length=7)
1 => string 'first_name' (length=11)
biodata_education =>
array
0 => string 'subject' (length=7)
1 => string 'year' (length=10)
as it can be done by simple iteration, I tried this one and done.
foreach($array1 as $tbl):
$table[$tbl[0]][] = $tbl[1];
endforeach;
<?php
//map the array using a foreach loop
foreach($array1 as $tbl)
{
$table[ $tbl[0] ][] = $tbl[1];
}