This question already has answers here:
How to Flatten a Multidimensional Array?
(31 answers)
Closed 10 months ago.
I think this has been up before, but could'nt find any answer to it. If it's already answered please point me in the right direction with a link.
I have an array that I wan't to remove the first levels of identifier. I think there is a function for this?
Example of how it is:
[0] => Array
(
[8] => Röd
)
[1] => Array
(
[8] => Blå
)
[2] => Array
(
[6] => Bobo
)
[3] => Array
(
[8] => Grön
)
[4] => Array
(
[7] => Sten
)
[5] => Array
(
[8] => Vit
)
[6] => Array
(
[7] => Guld
)
[7] => Array
(
[6] => Lyxig
)
What I wan't
[8] => Röd
[8] => Blå
[6] => Bobo
[8] => Grön
[7] => Sten
[8] => Vit
[7] => Guld
[6] => Lyxig
Try to merge array with splat operator:
print_r(array_merge(...$array));
The problem here is preserving the keys for the identifier you want. You have some names strings that have the same key (like Blå and Röd). You either need to store these in an array or be willing to lose the key.
Example with php5.3:
$processed = array_map(function($a) { return array_pop($a); }, $arr);
This will give you:
[0] => Röd
[1] => Blå
[2] => Bobo
[3] => Grön
[4] => Sten
[5] => Vit
[6] => Guld
[7] => Lyxig
It has become clear the keys on the inner array need to be preserved because they are some kind of id. With that said you must change the end structure you're going for because you can have 2 of the same key in a single array. The simplest structure then becomes:
[8] => Array
(
[0] => Röd,
[1] => Blå,
[2] => Vit,
[3] => Grön
)
[6] => Array
(
[0] => Bobo,
[1] => Lyxig
)
[7] => Array
(
[0] => Sten,
[1] => Guld
)
To get this structure a simple loop will work:
$processed = array();
foreach($arr as $subarr) {
foreach($subarr as $id => $value) {
if(!isset($processed[$id])) {
$processed[$id] = array();
}
$processed[$id][] = $value;
}
}
PHP array_column
$new_array = array_column($old_array,0);
This will retrieve the value at index 0 for each array within $old_array. Can also be using with associative arrays.
use :
public function remove_level($array) {
$result = array();
foreach ($array as $key => $value) {
if (is_array($value)) {
$result = array_merge($result, $value);
}
}
return $result;
}
which will return second level array values in the same order of the original array.
or you can use array_walk
$results = array();
array_walk($array, function($v, $k) use($key, &$val){
array_merge($results, $v);
});
Below code will also achieve the same result.
$resultArray = array_map('current',$inputArray);
OR
$resultArray = array_map('array_pop',$inputArray);
Note: I have ignored OP's expected result keys. Because it is not possible to have the same keys in the array. The last key will replace the previous one if the same.
foreach($array as $key=>$val) {
$newarr[$val] = $array[$key][$val];
}
untested!
Check this out this is what expected result
<?php
$arrData = array(
"5" => array
(
"8" => "Vit"
),
"6" => array
(
"7" => "Guld"
)
);
foreach($arrData as $key=>$value):
foreach($value as $k=>$v):
$data[$k] = implode(',',$arrData[$key]);
endforeach;
endforeach;
print_r($data);
?>
Related
i'm having trouble with combining array values. I have tried to combine them and make pairs something what i have done already but i want to erase some pairs that i do not want. Please some one to help me this happen .
// the Code I have now is:
$inputarray = array('Mussafiri', 'Fire', 'Ubungo', 'Mbezi');
$outputarray = array();
$i = 0;
foreach($inputarray as $values) {
$j = 0;
foreach($inputarray as $values2) {
if($values != $values2){
$outputarray[] = array($values => $values2);
}
$j++;
}
$i++;
}
print_r($outputarray);
//Output array is:
Array ( [0] => Array ( [Mussafiri] => Fire)
[1] => Array ( [Mussafiri] => Ubungo)
[2] => Array ( [Mussafiri] => Mbezi)
[3] => Array ( [Fire] => Mussafiri)
[4] => Array ( [Fire] => Ubungo)
[5] => Array ( [Fire] => Mbezi)
[6] => Array ( [Ubungo] => Mussafiri)
[7] => Array ( [Ubungo] => Fire)
[8] => Array ( [Ubungo] => Mbezi)
[9] => Array ( [Mbezi] => Mussafiri )
[10] => Array ( [Mbezi] => Fire)
[11] => Array ( [Mbezi] => Ubungo) )
NOTE: I DO NOT WANT THE FOLLOWING PAIRS TO APPEAR, I want element to pair with the next element and not pair with the previous element so i do not like index 3,6,7,8,9,10 and 11 to appear in the pair list i.e
[3] => Array ( [Fire] => Mussafiri)
[6] => Array ( [Ubungo] => Mussafiri)
[7] => Array ( [Ubungo] => Fire)
[8] => Array ( [Ubungo] => Mbezi)
[9] => Array ( [Mbezi] => Mussafiri)
[10] => Array ( [Mbezi] => Fire)
[11] => Array ( [Mbezi] => Ubungo)
If I understand correctly you're trying to create all unique pairs from the array, in which case something like this should work:
$pairs = array();
foreach ($inputarray as $key => $value) {
for ($i = $key + 1; isset($inputarray[$i]); $i++) {
$pairs[] = array($value => $inputarray[$i]);
}
}
The for loop sets $i as the key of the current array element + 1, so it'll never be referencing itself, and will never reference any previous array members.
for($i=0; $i<$inputarray.length;$i++){
$k=$i+1;
for($j=$k; $j<$inputarray.length;$j++){
$outputarray[] = array($inputarray[$i] => $inputarray[$j]);
}
}
I have array in my PHP, for example:
array
Array
(
[0] => Array
(
[0] =>
[1] =>
[2] =>
[3] =>
[4] =>
[5] => Grape
[6] => Apple
[7] => Pineaple
[8] => Avocado
[9] => Banana
)
)
and I need to fill the empty element (index 0 upto 4) with new value from an array or $variable.
Maybe for example, I get the data from another array:
newArray
Array
(
[0] => Array
(
[0] => Lemon
[1] => Lime
[2] => Mango
[3] => Watermelon
[4] => Starfruit
)
)
so I can get a result like this:
finalArray
Array
(
[0] => Array
(
[0] => Lemon
[1] => Lime
[2] => Mango
[3] => Watermelon
[4] => Starfruit
[5] => Grape
[6] => Apple
[7] => Pineaple
[8] => Avocado
[9] => Banana
)
)
Any help would be appreciated. Thanks
You can loop through your array, check if the index is empty and then, set it's value.
<?php
foreach($array as $index=>$value)
{
if(empty($array[$index]))
{
$array[$index] = $newValue;
}
}
<?php
$array=array("0"=>"","1"=>"","2"=>"","5"=>"Grape","6"=>"Apple","7"=>"Pineaple","8"=>"Avocado","9"=>"Banana");
echo "<pre>";print_r($array);echo"</pre>";
$newarray= array();
foreach($array as $key => $value){
if($value == ''){
//echo $key."<br>";
$value = 'Somevalue';
}
$newarray[] = $value;
//echo "<pre>";print_r($value);echo"</pre>";
}
echo "<pre>";print_r($newarray);echo"</pre>";
?>
Link
There is already a function in a standard library called array_replace. If the new values in the other array have the same indices you can use it:
$result = array_replace($array1, $array2);
If you just need to set up default values for empty elements use array_map:
$defaultValue = 'Foo';
$result = array_map(function ($item) use ($defaultValue) {
return $item ?: $defaultValue;
}, $array1);
Here is working demo.
Use only array_merge() function
For Example
print_r( array_merge($array, $newarray) );
I've an array in php something like below
Array
(
[0] => Array
(
[0] => 40173
[1] => 514081
[2] => 363885
[3] => 891382
),
[1] => Array
(
[0] => 40173
[1] => 5181
[2] => 385
[3] => 891382
)
)
Now I want to remove the parents indexes 0,1... and finally want to get all the values (only unique values).
Thanks.
One possible approach is using call_user_func_array('array_merge', $arr) idiom to flatten an array, then extracting unique values with array_unique():
$new_arr = array_unique(
call_user_func_array('array_merge', $old_arr));
Demo. Obviously, it'll work with array of any length.
$startArray = Array
(
[0] => Array
(
[0] => 40173
[1] => 514081
[2] => 363885
[3] => 891382
),
[1] => Array
(
[0] => 40173
[1] => 5181
[2] => 385
[3] => 891382
)
);
//Edited to handle more the 2 subarrays
$finalArray = array();
foreach($startArray as $tmpArray){
$finalArray = array_merge($finalArray, $tmpArray);
}
$finalArray = array_unique($finalArray);
Using RecursiveArrayIterator Class
$objarr = new RecursiveIteratorIterator(new RecursiveArrayIterator($yourarray));
foreach($objarr as $v) {
$new_arr[]=$v;
}
print_r(array_unique($new_arr));
Demo
OUTPUT:
Array
(
[0] => 40173
[1] => 514081
[2] => 363885
[3] => 891382
[5] => 5181
[6] => 385
)
$new_array = array_merge($array1, $array2);
$show_unique = array_unique($new_array);
print_r($show_unique);
array_merge is merging the array's, array_unique is removinge any duplicate values.
Try something like this:
$new_array = array();
foreach($big_array as $sub_array) {
array_merge($new_array, $sub_array);
}
$new_array = array_unique($new_array);
(code not tested, this just a concept)
Try this:
$Arr = array(array(40173, 514081, 363885, 891382),
array(40173,5181, 385,891382));
$newArr = array();
foreach($Arr as $val1)
{
foreach($val1 as $val2)
{
array_push($newArr, $val2);
}
}
echo '<pre>';
print_r(array_unique($newArr));
Output:
Array
(
[0] => 40173
[1] => 514081
[2] => 363885
[3] => 891382
[5] => 5181
[6] => 385
)
Refer: https://eval.in/124240
--
Thanks
how to shuffle arrays in array ? I tried lots of way but I can't achieve that. I think it's very simple but I'm stuck on that.
Array
(
[2] => Array
(
[0] => 12011190
[1] => 12011158
[2] => 12011583
[3] => 12012107
[4] => 12011222
[5] => 12010638
[6] => 12013836
[7] => 12012232
[8] => 12011256
[9] => 12010007
[10] => 12012531
[11] => 12012182
[12] => 12013253
)
[6] => Array
(
[0] => 12011565
[1] => 12010020
[2] => 12011352
[3] => 12014366
[4] => 12011879
[5] => 12011449
)
)
I want to shuffle within arrays. I hope explain...
As far as I know, you can do it like this (assuming you want to shuffle every sub-array independently):
foreach($array AS &$element) {
shuffle($element);
}
Or maybe like this:
array_walk($array, function(&$value, $key) {
shuffle($value);
});
Here is a recursive multi-level function you can use.
function shuffle_array($arr) {
if (!is_array($arr)) return $arr;
shuffle($arr);
foreach ($arr as $key => $a) {
if (is_array($a)) {
$arr[$key] = shuffle_array($a);
}
}
return $arr;
}
print_r(shuffle_array($array));
EDIT: Here is a portion of $preparedstring:
555555,Jones,Brian,NYC,1000,2011-10-21 00:00:00,Check,1542,0, ,Check, ,0, ,Check, ,; 6666666,Miler,Christopher,Chicago,1000,2011-10-26 00:00:00,Check,6406,0, ,Check, ,0, ,Check, ,;
I am trying to convert a HTML table to a multidimensional array. I have converted the table into a long string, each cell being delimited with a comma and each row being delimited with a semicolon.
I am not exactly sure how to build the multidimensional array from this string. This is what I have tried so far:
<?php
$outerARR = explode(";", $preparedstring);
$arr = array
(
foreach ($outerARR as $arrvalue) {
$innerarr = explode(",", $arrvalue);
$innerarr[0]=>array
(
$innerarr[];
)
}
);
?>
this gives me a syntax error near the
$arr = array
(
opening parenthesis.
Your approach to solving the problem is sadly very wrong, though there are many solutions to your problem, I would use something like the below.
How does the code work?
First we use explode to split our string up in smaller chunks, ; is our delimiter.
We pass this newly created array to array_map as it's second parameter.
array_map takes two parameters, the first one is a function that will be called for every member of the second paramater (which should be an array).
Inside our callback to array_map we use explode to once again split out the values, now with , as our delimiter.
$data = "1,2,3;4,5,6;7,8,9";
$ret = array_map (
function ($_) {return explode (',', $_);},
explode (';', $data)
);
print_r ($ret);
output
Array
(
[0] => Array
(
[0] => 1
[1] => 2
[2] => 3
)
[1] => Array
(
[0] => 4
[1] => 5
[2] => 6
)
[2] => Array
(
[0] => 7
[1] => 8
[2] => 9
)
)
It doesn't work, why?
Probably because you are using a version of PHP prior to 5.3, if so you can use this snippet instead:
function explode_by_comma ($_) {
return explode (',', $_);
}
$ret = array_map ('explode_by_comma', explode (';', $data));
<?php
//explode first dimension of the array to create an array of rows
$outerARR = explode(";", $preparedstring);
$arr = array();
//iterate through the newly created array
foreach ($outerARR as $arrvalue) {
//explode this row into columns
$innerarr = explode(",", $arrvalue);
//add the newly created array of columns to the output array as a new index
$arr[] = $innerarr;
}
?>
You're close, but arrays don't work that way. You can't put a foreach inside an array constructor like that. It should look like this:
$outerARR = explode(";", $preparedstring);
$arr = array();
foreach ($outerARR as $arrvalue){
$innerarr = explode(",", $arrvalue);
$arr[] = $innerarr;
}
Demo: http://codepad.org/I5wFFczR
$outerARR = explode(";", $preparedstring);
$a = array();
$y=0;
foreach ($outerARR as $arrvalue){
$x=0;
$innerarr = explode(",", $arrvalue);
foreach($innerarr as $v){
$a[$y][$x++] = $v;
}
$y++;
}
print_r($a);
Array
(
[0] => Array
(
[0] => 555555
[1] => Jones
[2] => Brian
[3] => NYC
[4] => 1000
[5] => 2011-10-21 00:00:00
[6] => Check
[7] => 1542
[8] => 0
[9] =>
[10] => Check
[11] =>
[12] => 0
[13] =>
[14] => Check
[15] =>
[16] =>
)
[1] => Array
(
[0] => 6666666
[1] => Miler
[2] => Christopher
[3] => Chicago
[4] => 1000
[5] => 2011-10-26 00:00:00
[6] => Check
[7] => 6406
[8] => 0
[9] =>
[10] => Check
[11] =>
[12] => 0
[13] =>
[14] => Check
[15] =>
[16] =>
)
)