This question already has answers here:
How to Flatten a Multidimensional Array?
(31 answers)
Closed 3 years ago.
I have an array like this:
[12601] => Array (
['docUpload'] => html dom.txt
)
[12602] => Array (
['docUpload'] => PYTHON AND DJANGO ARE HUGE IN FINTECH.txt
)
[12603] => Array (
['docUpload'] =>
)
How to get it like this:
12601 => html dom.txt
12602 => PYTHON AND DJANGO ARE HUGE IN FINTECH.txt
can you help me please?
Use array_column() to get the values, then combine them with array_combine() and array_keys().
$values = array_column($array, 'docUpload');
$newArray = array_combine(array_keys($array), $values);
Live demo at https://3v4l.org/lG4KO
You can loop over the array by foreach()
Steps:
1) Take a new blank array. We are appending our results into this.
2) If the array is not empty, loop over the array by foreach
3) Use key value pairs. Key is the id in required array.
4) Value is an array with key docUpload to be the document name.
5) Append new element with id and value (docUpload).
6) Resulting array will be a single dimensional array.
Final Code:
$arr = [];
$arr[12401] = ['docUpload' => ''];
$arr[12601] = ['docUpload' => 'html dom.txt'];
$arr[12602] = ['docUpload' => 'PYTHON AND DJANGO ARE HUGE IN FINTECH.txt'];
$arr[12603] = ['docUpload' => ''];
$newArr = [];
if (! empty($arr)) {
foreach ($arr as $id => $docArr) {
$newArr[$id] = $docArr['docUpload'];
}
}
echo '<pre>';print_r($newArr);echo '</pre>';
Output:
Array
(
[12401] =>
[12601] => html dom.txt
[12602] => PYTHON AND DJANGO ARE HUGE IN FINTECH.txt
[12603] =>
)
Working Link:
You need to do as follows:
foreach ($arrayData as &$value) {
$value = isset($value['docUpload']) ? $value['docUpload'] : '';
}
It will result in following array:
[docUpload] => [
[12601] => html dom.txt
[12602] => PYTHON AND DJANGO ARE HUGE IN FINTECH.txt
];
Related
This question already has an answer here:
how to merge multiple url/path into multidimensional array?
(1 answer)
Closed 8 months ago.
I have a problem with creating a recursive array with PHP.
I need to format this string to a multidimensional array with the dot-separated elements indicating multiple levels of array keys.
$str = "code.engine,max_int.4,user.pre.3,user.data.4";
The output for this example would be:
$str = array(
"code" => "engine",
"max_int" => 4,
"user" => array(
"pre" => 3,
"data" => 4
)
);
I will start with an explode function, but I don't know how to sort it from there, or how to finish the foreach.
You could start to split using comma ,, then, split each item by dot ., remove the last part to get the "value", and the rest as "path". Finally, loop over the "path" to store the value:
$str = "code.engine,max_int.4,user.pre.3,user.data.4";
$array = [] ;
$items = explode(',',$str);
foreach ($items as $item) {
$parts = explode('.', $item);
$last = array_pop($parts);
// hold a reference of to follow the path
$ref = &$array ;
foreach ($parts as $part) {
// maintain the reference to current path
$ref = &$ref[$part];
}
// finally, store the value
$ref = $last;
}
print_r($array);
Outputs:
Array
(
[code] => engine
[max_int] => 4
[user] => Array
(
[pre] => 3
[data] => 4
)
)
This question already has answers here:
Convert multidimensional array into single array [duplicate]
(24 answers)
Restructure multidimensional array of column data into multidimensional array of row data [duplicate]
(3 answers)
Closed 5 years ago.
How can I convert a multidimensional array like the one below
Array(
[0] => Array(
[0]=001
[1]=002
[2]=003
)
[1] => Array(
[0]=America
[1]=Japan
[2]=South Korea
)
[2] => Array(
[0]=Washington DC
[1]=Tokyo
[2]=Seoul
)
)
into a single line array like the one below?
Array(
[0]=001,America,Washington DC
[1]=002,Japan,Tokyo
[2]=003,South Korea,Seoul
)
Here is simple code to work around,
foreach ($text as $key => $value) {
foreach ($value as $key1 => $value1) {
$result[$key1][] = $value1;
}
}
array_walk($result, function(&$item){
$item = implode(',', $item);
});
Here is the working link
array_walk — Apply a user supplied function to every member of an array
The variadiac php5.6+ version: (Offers the added benefits of not breaking on missing values and inserting null where values are missing.)
Code: (Demo)
var_export(array_map(function(){return implode(',',func_get_args());},...$text));
The non-variadic version:
Code: (Demo)
foreach($text as $i=>$v){
$result[]=implode(',',array_column($text,$i));
}
var_export($result);
Input:
$text = [
['001','002','003'],
['America','Japan','South Korea'],
['Washington DC','Tokyo','Seoul']
];
Output from either method:
array (
0 => '001,America,Washington DC',
1 => '002,Japan,Tokyo',
2 => '003,South Korea,Seoul',
)
Nearly exact duplicate page: Combining array inside multidimensional array with same key
This question already has answers here:
Reverse an associative array with preserving keys in PHP
(4 answers)
Closed 5 years ago.
I am developing a new website, and I have a quetion.
Input array:
Array ( [1319] => ####,[1316] => ###)
I have an array and I want to revese him, after the reverse the array would be like this:
Expected output:
Array ( [1316] => ###,[1319] => ####)
but when i'm using array_reverse function, it doesnt work for me, I got this array:
Array ( [0] => ###,[1] => ####)
Why it is happen?
For preserving keys you just pass second parameter to true in array_reverse.
Try this code snippet here
$array=Array ( 1319 => "####",1316 => "###");
print_r(array_reverse($array,true));
you can try this:
$a = []; //your array
$keys = array_keys($arr);
$values = array_values($arr);
$rv = array_reverse($values);
$newArray = array_combine($keys, $rv);
This question already has answers here:
How can I sort arrays and data in PHP?
(14 answers)
Custom key-sort a flat associative based on another array
(16 answers)
Closed 8 years ago.
is it possible to sort an array by keys using an custom order ?
i have an array with strings that represent the order.
$order = array('ccc','aaa','xxx','111');
$myarray = array('ccc' => 'value1','aaa' => 'value2','xxx' => 'value3',
'BBB' => 'value11','ddd' => 'value31')
now i want the array to be sorted with the elemnts with the key 'ccc' at the first position, the nthe elements with the key aaa ... and at the end should be the elements that are not in the sortlist.
is this possible ?
edit: the second 'CCC' was my fault - sorry
See this in action https://eval.in/118734
<?php
$order = array('ccc','xxx','aaa','111');
$myarray = array('ccc' => 'value1','aaa' => 'value2','xxx' => 'value3',
'ddd' => 'value31');
$temp = array();
foreach($order as $o) {
if(array_key_exists($o, $myarray)) {
$temp[$o] = $myarray[$o];
}
}
$new = array_merge($temp, $myarray);
print_r($new);
?>
I was just having a think about this as I was having a similar issue with array_multisort() and ksort().
However in your case if the snippet of code is correct, will not be possible as the second 'ccc' key with a value of 'value11' will overwrite the previous one.
php > $myarray = array('ccc' => 'value1','aaa' => 'value2','xxx' => 'value3','ccc' => 'value11','ddd' => 'value31');
php > print_r($myarray);
Array
(
[ccc] => value11
[aaa] => value2
[xxx] => value3
[ddd] => value31
)
.
This question already has answers here:
Remove duplicates from Array
(2 answers)
Closed 9 years ago.
I have an array like this
Array
(
[0] => u1,u2
[1] => u2,u1
[2] => u4,u3
[3] => u1,u3
[4] => u1,u2
)
I want to remove similar values from the array
I want an out put like
Array
(
[0] => u1,u2
[1] => u4,u3
[2] => u1,u3
)
I tried to loop thru the input array, sort the value of the indexes alphabetically and then tried array_search to find the repeated values. but never really got the desired output
any help apprecated
You cannot use array_unique() alone, since this will only match exact duplicates only. As a result, you'll have to loop over and check each permutation of that value.
You can use array_unique() to begin with, and then loop over:
$myArray = array('u1,u2', 'u2,u1', 'u4,u3', 'u1,u3', 'u1,u2');
$newArr = array_unique($myArray);
$holderArr = array();
foreach($newArr as $val)
{
$parts = explode(',', $val);
$part1 = $parts[0].','.$parts[1];
$part2 = $parts[1].','.$parts[0];
if(!in_array($part1, $holderArr) && !in_array($part2, $holderArr))
{
$holderArr[] = $val;
}
}
$newArr = $holderArr;
The above code will produce the following output:
Array (
[0] => u1,u2
[1] => u4,u3
[2] => u1,u3
)
Use array_unique() PHP function:
http://php.net/manual/en/function.array-unique.php
Use the function array_unique($array)
array array_unique ( array $array [, int $sort_flags = SORT_STRING ] )
php manual
since u1,u2 !== u2,u1
$array=array('u1,u2','u2,u1','u4,u3','u1,u3','u1,u2');
foreach($array as $k=>$v)
{
$sub_arr = explode(',',$v);
asort($sub_arr);
$array[$k] = implode(',',$sub_arr);
}
$unique_array = array_unique($array);
//$unique_array = array_values($unique_array) //if you want to preserve the ordered keys