PHP convert 2 dimensional array to string - php

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

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

How do I retrieve the values from a multidimensional array?

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

adding string keys to inner arrays

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

Mimicking a join and group by statement with multidimensional array

Source/Input
I have a multidimensional array:
array (size=2)
'array_one' =>
array (size=2)
0 =>
array (size=2)
0 => string 'ABC' (length=3)
1 => string 'ABC' (length=3)
'myid' =>
array (size=2)
0 => string 'ABC' (length=3)
1 => string 'ABC' (length=3)
'array_two' =>
array (size=2)
0 =>
array (size=2)
0 => string 'DEF' (length=3)
1 => string 'DEF' (length=3)
'myid' =>
array (size=2)
0 => string 'DEF' (length=3)
1 => string 'DEF' (length=3)
i want to combine the arrays but if an array element's key is the same i want to replace both element with a new element with a new (dynamically created using it's parent) name, i want the output to look something like this:
Expected Output
array (size=1)
'new_array' =>
array (size=2)
'myid_array_one' =>
array (size=2)
0 => string 'ABC' (length=3)
1 => string 'ABC' (length=3)
'myid_array_two' =>
array (size=2)
0 => string 'DEF' (length=3)
1 => string 'DEF' (length=3)
Method
This is what i have tried:
public function data_combine($data_set_arrays) {
$result = [];
$accociative = $data_set_arrays;
foreach($accociative as &$array) {
foreach($array as $key => $value) {
if(is_int($key)) {
unset($array[$key]);
}
}
}
foreach($accociative as $array) {
//make sure no elements have same name
$parent = key($array);
foreach($array as $key => $value) {
$duplicates = array_intersect_key($data_set_arrays, $array); {
foreach($duplicates as $key => $value) {
$array[$key.'_'.$parent] = $array[$key];
unset($array[$key]);
}
}
}
$result = array_merge_recursive($result, $array);
}
return $result;
}
but i can't see the wood for the trees, can someone lend a hand?

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

Categories