I have an array that is the result of a file uploading input.
The array $FILES looks like this:
Array (
[files] => Array
(
[name] => Array
(
[0] => 20180131023939.JPG
[1] => 20180131024005.JPG
[2] => 20180131024027.jpg
[3] => 20180131023722.JPG
[4] => 20180131023913.JPG
)
[type] => Array
(
[0] => image/jpeg
[1] => image/jpeg
[2] => image/jpeg
[3] => image/jpeg
[4] => image/jpeg
)
[tmp_name] => Array
(
[0] => /data/sites/web/mersbe/tmp/phpzJ6Avh
[1] => /data/sites/web/mersbe/tmp/phpMHduDZ
[2] => /data/sites/web/mersbe/tmp/phpiSohMH
[3] => /data/sites/web/mersbe/tmp/phpOoAJWp
[4] => /data/sites/web/mersbe/tmp/phpdr9n87
)
[error] => Array
(
[0] => 0
[1] => 0
[2] => 0
[3] => 0
[4] => 0
)
[size] => Array
(
[0] => 2461635
[1] => 402525
[2] => 1383589
[3] => 1154849
[4] => 441203
)
)
)
I want the entire array sorted by name. In this example 20180131023722.JPG should have the index of 0 after the sort. The other values should also sort accordingly..
array_multisort should be used but I don't know how..
Sorting arrays with this structure is very difficult. The transpose array is easy to sort. The array then has this structure:
array (
0 =>
array (
'name' => "20180131023939.JPG",
'type' => "image/jpeg",
'tmp_name' => "/data/sites/web/mersbe/tmp/phpzJ6Avh",
'error' => 0,
'size' => 2461635,
),
1 =>
array (..
After sorting, transposition can be done again if the original structure is needed.
function array_transpose(array $array) {
$newArr = [];
foreach($array as $keyRow => $subArr) {
foreach($subArr as $keyCol => $value) $newArr[$keyCol][$keyRow] = $value;
}
return $newArr;
}
//test data
$files = [
'name' => ['20180131023939.JPG','20180131024005.JPG',
'20180131024027.jpg','20180131023722.JPG','20180131023913.JPG'],
'type' => ['image/jpeg', 'image/jpeg', 'image/jpeg', 'image/jpeg', 'image/jpeg'],
'tmp_name' => ['/data/sites/web/mersbe/tmp/phpzJ6Avh',
'/data/sites/web/mersbe/tmp/phpMHduDZ',
'/data/sites/web/mersbe/tmp/phpiSohMH',
'/data/sites/web/mersbe/tmp/phpOoAJWp',
'/data/sites/web/mersbe/tmp/phpdr9n87'],
'error' => [0,0,0,0,0],
'size' => [2461635,402525,1383589,1154849,441203]
];
$arr = array_transpose($files);
//sort by name asc
usort($arr, function($a,$b){return $a['name'] <=> $b['name'];});
//transpose back
$files = array_transpose($arr);
Related
This question already has answers here:
Group subarrays by one column, make comma-separated values from other column within groups
(2 answers)
Closed last month.
here's how it looks in the PHP code:
<?php
$array = array(
array(
'name' => 'filter_amount',
'value' => '100-ml'
),
array(
'name' => 'filter_amount',
'value' => '200-ml'
),
array(
'name' => 'page_size',
'value' => '7'
)
);
print_r($array);
?>
Example of print_r() function output:
Array
(
[0] => Array
(
[name] => filter_amount
[value] => 100-ml
)
[1] => Array
(
[name] => filter_amount
[value] => 200-ml
)
[2] => Array
(
[name] => page_size
[value] => 7
)
)
I need to combine duplicates of filter_amount values from the array.
The values of these duplicates must be commas separated and the result should be the following code:
Array
(
[0] => Array
(
[name] => filter_amount
[value] => 100-ml,200-ml
)
[1] => Array
(
[name] => page_size
[value] => 7
)
[2] => Array
(
[name] => orderby
[value] => rating
)
[3] => Array
(
[name] => paged
[value] => 1
)
)
Since you want value to be concatenated by a comma, you'll have to make a cycle of it
<?php
//Allow me to change this variable name, just to not create confusion
$content = array(
array(
'name' => 'filter_amount',
'value' => '100-ml'
),
array(
'name' => 'filter_amount',
'value' => '200-ml'
),
array(
'name' => 'page_size',
'value' => '7'
)
);
//$content is your initial array
//$outputArray is the final worked-up array
$outputArray = [];
//Let's make a cycle going for every array inside $content
foreach ($content as $innerArray) {
//Does this $innerArray['name'] (filter_ammount) exist in $outputArray in an array
//consisting in key => value where the key is 'name' and equals
//what we look for that is(filter_ammount)?
$key = array_search($innerArray['name'], array_column($outputArray , 'name'));
//If not, let's place this array in the $output array
if ($key === false) {
array_push($outputArray, $innerArray);
} else {
//If exists, then $key is the $key of the $outputArray and let's add to its value
//our current value, that is in our $innerArray, concatenated with a comma
$outputArray[$key]['value'] .= ",". $innerArray['value'];
}
}
//Boom, magic
print_r($outputArray);
//Note: This is going to affect every duplicate it finds, as in:
//If you got 3 arrays with name 'filter_ammount' and 2 arrays with name
//'page_size', it's going to concatenate the filter_ammount and the 'page_size'.
//If you specifically just want filter_ammount,
//replace this -> $key = array_search($innerArray['name'], array_column($outputArray , 'name'));
//with this -> $key = array_search('filter_ammount', array_column($outputArray , 'name'));
?>
References
http://php.net/manual/en/function.array-search.php
http://php.net/manual/en/function.array-column.php
How to combine two items by 2 duplicate columns?
[root#localhost TEST]# php R00.php
Array
(
[0] => Array
(
[0] => S01
[1] => 172.16.20.222
[2] => 10.10.10.100
[3] => 445
)
[1] => Array
(
[0] => S02
[1] => 10.10.10.10
[2] => 192.168.100.100
[3] => 22
)
[2] => Array
(
[0] => S03
[1] => 10.10.10.10
[2] => 192.168.100.100
[3] => 22
)
[3] => Array
(
[0] => S04
[1] => 172.16.20.222
[2] => 10.10.10.100
[3] => 23
)
[4] => Array
(
[0] => S05
[1] => 100.100.100.100
[2] => 192.168.100.100
[3] => 22
)
[5] => Array
(
[0] => S06
[1] => 192.168.200.10
[2] => 192.168.100.100
[3] => 22
)
[6] => Array
(
[0] => S07
[1] => 10.10.10.10
[2] => 192.168.100.100
[3] => 22
)
[7] => Array
(
[0] => S08
[1] => 192.168.100.100
[2] => 10.10.100.106
[3] => 446
)
[8] => Array
(
[0] => S09
[1] => 172.16.20.223
[2] => 10.10.10.108
[3] => 447
)
[9] => Array
(
[0] => S10
[1] => 192.168.100.100
[2] => 10.10.10.109
[3] => 448
)
)
[root#localhost TEST]#
combine 1 or 2 items by 2 column duplicate below is result I need
Array
(
[0] => Array
(
[0] => S01 , S04
[1] => 172.16.20.222
[2] => 10.10.10.100
[3] => 445 , 23
)
[1] => Array
(
[0] => S02 , S03 , S07
[1] => 10.10.10.10
[2] => 192.168.100.100
[3] => 22
)
[3] => Array
(
[0] => S05 , S06
[1] => 100.100.100.100 , 192.168.200.10
[2] => 192.168.100.100
[3] => 22
)
[4] => Array
(
[0] => S08
[1] => 192.168.100.100
[2] => 10.10.100.106
[3] => 446
)
[5] => Array
(
[0] => S09
[1] => 172.16.20.223
[2] => 10.10.10.108
[3] => 447
)
[6] => Array
(
[0] => S10
[1] => 192.168.100.100
[2] => 10.10.10.109
[3] => 448
)
)
Try this:
<?php
$array = array(
array(
'name' => 'filter_amount',
'value' => '100-ml'
),
array(
'name' => 'filter_amount',
'value' => '200-ml'
),
array(
'name' => 'page_size',
'value' => '7'
)
);
$tmp = array();
foreach($array as $val) {
$tmp[$val['name']]['values'][] = $val['value'];
}
foreach($tmp as $k => $v) {
$item = implode(',', array_unique(explode(',', implode(',',$v['values']))));
$newArr[] = array('name' => $k, 'value' => $item);
}
echo '<pre>';
print_r($newArr);
echo '</pre>';
got it with the following crazy mess:
$name = array_column($array, 'name');
$value = array_column($array, 'value');
foreach($name as $nk=>$nv)
foreach($value as $vk=>$vv)
if($nk == $vk)
$a[$nv][] = $vv;
foreach($a as $k=>$v)
$b[$k] = implode(',', $v);
$z = 0;
foreach($b as $k=>$v)
{
$c[$z]['name'] = $k;
$c[$z]['value'] = $v;
$z++;
}
$c is the resulting array
Or using a medley of array functions:
<?php
$array = array(
array(
'name' => 'filter_amount',
'value' => '100-ml'
),
array(
'name' => 'filter_amount',
'value' => '200-ml'
),
array(
'name' => 'page_size',
'value' => '7'
)
);
$names = array_column($array, 'name');
$values = array_column($array, 'value');
$result = [];
foreach (array_unique($names) as $k)
$result[$k] = implode(", ", array_filter($values,
function($v, $indx) use ($names, $k) {
return $names[$indx] == $k;
}, ARRAY_FILTER_USE_BOTH));
print_r($result);
$result2 = [];
foreach ($result as $k=>$v) $result2[] = ['name'=>$k, 'value'=>$v];
print_r($result2);
Results in:
Array
(
[filter_amount] => 100-ml, 200-ml
[page_size] => 7
)
Array
(
[0] => Array
(
[name] => filter_amount
[value] => 100-ml, 200-ml
)
[1] => Array
(
[name] => page_size
[value] => 7
)
)
All of the other answers up to now are using two or more iterating techniques for this task. There only needs to be one loop.
Build an associative output array based on the name values as you iterate. If the associative key isn't set, then save the whole row. If it is set, then just append a comma then the new value data to the stored value element.
Using temporary keys allows isset() to swiftly check for existence. It will always outperform array_search() and in_array() because of how php treats arrays (as hash maps).
Remove the temporary keys when the loop is finished by calling array_values().
Code: (Demo)
$result = [];
foreach ($array as $row) {
if (!isset($result[$row['name']])) {
$result[$row['name']] = $row;
} else {
$result[$row['name']]['value'] .= ',' . $row['value'];
}
}
var_export(array_values($result));
Output:
array (
0 =>
array (
'name' => 'filter_amount',
'value' => '100-ml,200-ml',
),
1 =>
array (
'name' => 'page_size',
'value' => '7',
),
)
I have the following array
Array
(
[0] => Array
(
[text] => Array
(
[content] => I
[beginOffset] => 0
)
[partOfSpeech] => Array
(
[tag] => PRON
[aspect] => ASPECT_UNKNOWN
[case] => NOMINATIVE
[form] => FORM_UNKNOWN
[gender] => GENDER_UNKNOWN
[mood] => MOOD_UNKNOWN
[number] => SINGULAR
[person] => FIRST
[proper] => PROPER_UNKNOWN
[reciprocity] => RECIPROCITY_UNKNOWN
[tense] => TENSE_UNKNOWN
[voice] => VOICE_UNKNOWN
)
[dependencyEdge] => Array
(
[headTokenIndex] => 1
[label] => NSUBJ
)
[lemma] => I
)
...
I want to remove all elements that contain the string "_UNKNOWN" as they are not necessairy
how would I go about that?
Assuming all your 'UNKNOWN' are going to be in 'partOfSpeech', you can use this simple code to remove all the elements containing the string '_UNKNOWN':
$array = ['text' => ['content' => 'I', 'beginOffset' => 0], 'partOfSpeech' => ['tag' => 'PRON', 'aspect' => 'ASPECT_UNKNOWN', 'form' => 'FORM_UNKNOWN']]; // Example array
$array['partOfSpeech'] = array_filter($array['partOfSpeech'],
function($item) {
return strpos($item, '_UNKNOWN') === false;
});
print_r($array);
Output:
Array ( [text] => Array ( [content] => I [beginOffset] => 0 ) [partOfSpeech] => Array ( [tag] => PRON ) )
There are plenty of answers around but I just can't seem to get this right, at all, the post seems big but it just seems. Here is what I have, and what I've tried.
Array
(
[0] => image 1
[1] => image 2
)
Array
(
[name] => Array
(
[0] => 0.14997300-1503597010599f11d2249df30.jpg
[1] => 0.24654000-150113339659797a543c31f24.jpg
)
[type] => Array
(
[0] => image/jpeg
[1] => image/jpeg
)
[tmp_name] => Array
(
[0] => C:\Users\--\AppData\Local\Temp\php509E.tmp
[1] => C:\Users\--\AppData\Local\Temp\php509F.tmp
)
[error] => Array
(
[0] => 0
[1] => 0
)
[size] => Array
(
[0] => 55560
[1] => 9425
)
)
I've tried:
$instructions = $_POST['instructions'];
$image = $_FILES['instructions_image'];
$result = array();
foreach($instructions as $index => $key){
$t = array();
foreach($image as $img){
$t[] = $img;
}
$result[$key] = $t;
}
And the results is:
Array
(
[image 1] => Array
(
[0] => Array
(
[0] => 0.14997300 1503597010599f11d2249df30.jpg
[1] => 0.24654000 150113339659797a543c31f24.jpg
)
[1] => Array
(
[0] => image/jpeg
[1] => image/jpeg
)
[2] => Array
(
[0] => C:\Users\--\AppData\Local\Temp\phpBAD5.tmp
[1] => C:\Users\--\AppData\Local\Temp\phpBAE6.tmp
)
[3] => Array
(
[0] => 0
[1] => 0
)
[4] => Array
(
[0] => 55560
[1] => 9425
)
)
[image 2] => Array
(
[0] => Array
(
[0] => 0.14997300 1503597010599f11d2249df30.jpg
[1] => 0.24654000 150113339659797a543c31f24.jpg
)
[1] => Array
(
[0] => image/jpeg
[1] => image/jpeg
)
[2] => Array
(
[0] => C:\Users\--\AppData\Local\Temp\phpBAD5.tmp
[1] => C:\Users\--\AppData\Local\Temp\phpBAE6.tmp
)
[3] => Array
(
[0] => 0
[1] => 0
)
[4] => Array
(
[0] => 55560
[1] => 9425
)
)
)
I'm unsure why the results have 2 of the same in both indexes, but I was also wanting to know how we could keep the array key names provided by the $_FILES, ex: name, type, tmp_name, error and size.
Here is what I was expecting(I included additional information like the key names that I didn't provide nor did with my code, sorry about that, been at it for 12 hours non stop but just any explanation to set me on the right path would help me tremendously):
Array
(
[0] => Array
(
[text] => image 1,
[image_data] => Array (
[name] => 0.14997300 1503597010599f11d2249df30.jpg
[type] => image/jpeg
[tmp_name] => C:\Users\--\AppData\Local\Temp\php509E.tmp
[error] => 0
[size] => 55560
)
)
[1] => Array
(
[text] => image 2,
[image_data] => Array (
[name] => 0.24654000 150113339659797a543c31f24.jpg
[type] => image/jpeg
[tmp_name] => C:\Users\--\AppData\Local\Temp\php509E.tmp
[error] => 0
[size] => 9425
)
)
)
You need to write a custom script which combines these arrays by your logic. For this task you can use this array functions: array_combine, array_keys, array_column.
Example:
<?php
$a1 = ['image 1', 'image 2'];
$a2 = [
'name' => ['0.14997300-1503597010599f11d2249df30.jpg', '0.24654000-150113339659797a543c31f24.jpg'],
'type' => ['image/jpeg', 'image/jpeg'],
'tmp_name' => ['C:\Users\--\AppData\Local\Temp\php509E.tmp', 'C:\Users\--\AppData\Local\Temp\php509F.tmp'],
'error' => [0, 0],
'size' => [55560, 9425]
];
$result = [];
foreach ($a1 as $k => $v) {
$result[] = [
'text' => $v,
'image_data' => array_combine(array_keys($a2), array_column($a2, $k))
];
}
print_r($result);
Hope this one will be helpful. Here we are using simple foreach loop array_keys, array_combine and array_column
Try this code snippet here
$result=array();
foreach($images as $key => $image)
{
$result[]=array(
"text"=>$image,
"image_data"=>array_combine(
array_keys($instructions),
array_column($instructions,$key))
);
}
print_r($result);
This Will help you
$res=array(0 => 'image 1',1 => 'image 2');
$valu=array('name' => array(0=> '0.14997300-1503597010599f11d2249df30.jpg',1 => '0.24654000-150113339659797a543c31f24.jpg'
),'type' => array( 0 => 'image/jpeg',1 => 'image/jpeg'), 'tmp_name'=> array(0 => 'C:\Users\--\AppData\Local\Temp\php509E.tmp',1 => 'C:\Users\--\AppData\Local\Temp\php509F.tmp'),'error' => array(0 => 0,1 => 0),'size' => array(0 => 55560, 1 => 9425)
);
$newarr=array();
foreach($res as $key=>$val)
{
$newarr[$key]['text']=$val;
$newarr[$key]['image_data']=array('name'=>$valu['name'][$key],'type'=>$valu['type'][$key],'tmp_name'=>$valu['tmp_name'][$key],'error'=>$valu['error'][$key],'size'=>$valu['size'][$key]);
}
echo '<pre>';
print_r($newarr);
Try this
$fileData = array(
'name' => ['0.14997300-1503597010599f11d2249df30.jpg','0.24654000-150113339659797a543c31f24.jpg'],
'type' => ['image/jpeg','image/jpeg'],
'tmp_name' => ['C:\Users\--\AppData\Local\Temp\php509E.tmp','C:\Users\--\AppData\Local\Temp\php509F.tmp'],
'error' => [0,0],
'size' => [55560,9425]);
$someArr = array('image 1','image 2');
$fileData['somedata'] = $someArr;
function reFileData($fileData) {
$arr = array();
$keys = array_keys($fileData);
for ($i=0; $i < count($fileData['name']); $i++) {
foreach ($keys as $key) {
$arr[$i][$key] = $fileData[$key][$i];
}
}
return $arr;
}
print_r(reFileData($fileData));
I declare the following array
$job_scope = array( "proposal_id",
"will_provide" => array("0","Supervision","Labor","Material","Equpment"),
"general_scope",
"per_bid" => array("Yes","No","Omit"),
"job_type" => array("Painting","Sandblasting","Scappling")
);
I expect it to be created like
array([0] => 'proposal_id',
[1] => 'will_provide' => array([0] => "0",
[1] => "Supervision",
[2] => "Labor",
[3] => "Material",
[4] => "Equpment"),
[2] => 'general_scope',
[3] => 'per_bid' => array([0] => "Yes",
[1] => "No",
[2] => "Omit"),
[4] => 'job_type' => array([0] => "Painting",
[1] => "Sandblasting",
[2] => "Scappling")
But when I print the array it looks like
Array ( [0] => proposal_id [will_provide] => Array (
[0] => 0
[1] => Supervision
[2] => Labor
[3] => Material
[4] => Equpment )
[1] => general_scope [per_bid] => Array (
[0] => Yes
[1] => No
[2] => Omit )
[job_type] => Array (
[0] => Painting
[1] => Sandblasting
[2] => Scappling )
I would like the array to be created in the same format as the second section of code.
All you need to do is assign an empty array to the proposal_id and general_scope. So the code will look like this
$job_scope = array( "proposal_id" => array(),
"will_provide" => array("0","Supervision","Labor","Material","Equpment"),
"general_scope" => array(),
"per_bid" => array("Yes","No","Omit"),
"job_type" => array("Painting","Sandblasting","Scappling")
);
It will produce this array
Array (
[proposal_id] => Array ( )
[will_provide] => Array ( [0] => 0
[1] => Supervision
[2] => Labor
[3] => Material
[4] => Equpment
)
[general_scope] => Array ( )
[per_bid] => Array ( [0] => Yes
[1] => No
[2] => Omit
)
[job_type] => Array ( [0] => Painting
[1] => Sandblasting
[2] => Scappling
))
If you want to callback the value, (ex : call supervision value).
All you need to do is
print_r($job_scope['will_provide'][1])
and that will print the supervision value
use $new_job_scope = array_values($job_scope);
$job_scope = array(
"proposal_id",
"will_provide" => array(
"0",
"Supervision",
"Labor",
"Material",
"Equpment"
),
"general_scope",
"per_bid" => array(
"Yes",
"No",
"Omit"
),
"job_type" => array(
"Painting",
"Sandblasting",
"Scappling"
)
);
$new_job_scope = array_values($job_scope);
print_r($new_job_scope);
PhpFiddle
Create array first !!! Reassign at specified index with 2D array will be more clear to me
<?php
$arr = array('proposal_id','','general_scope','',''); //create array first
$arr[1] = array("will_provide" => array("0","Supervision","Labor","Material","Equpment"));
$arr[3] = array("per_bid" => array("Yes","No", "Omit"));
$arr[4] = array("job_type" => array("Painting","Sandblasting","Scappling"));
var_dump($arr);
?>
I think this process can serve you. I have just used a foreach loop to convert non-int key to int key:
$new_array = '';
foreach($job_scope as $k => $v){
if(is_int($k)){
$new_array[] = $v;
}else{
$new_array[] = [$k => $v];
}
}
print_r($new_array);
Output would be:
Array
(
[0] => proposal_id
[1] => Array
(
[will_provide] => Array
(
[0] => 0
[1] => Supervision
[2] => Labor
[3] => Material
[4] => Equpment
)
)
[2] => general_scope
[3] => Array
(
[per_bid] => Array
(
[0] => Yes
[1] => No
[2] => Omit
)
)
[4] => Array
(
[job_type] => Array
(
[0] => Painting
[1] => Sandblasting
[2] => Scappling
)
)
)
This is my array of timestamps. I would like to eliminate values within 30 seconds of each other, only keeping the value if there is not another value within 30 sec. Any help would be greatly appreciated!
Array
(
[99999] => Array
(
[0] => 1356399000
[1] => 1356398971
[2] => 1356399005
[3] => 1356413406
)
[99997] => Array
(
[0] => 1356399002
[1] => 1356399007
[2] => 1356398871
[3] => 1356398876
)
[99996] => Array
(
[0] => 1356399003
[1] => 1356399004
[2] => 1356399008
)
[99995] => Array
(
[0] => 1356399009
)
)
My expected output:
Array
(
[99999] => Array
(
[0] => 1356399000
[1] => 1356398971
[2] => 1356413406
)
[99997] => Array
(
[0] => 1356399002
[1] => 1356398871
)
[99996] => Array
(
[0] => 1356399003
)
[99995] => Array
(
[0] => 1356399009
)
)
Any solutions/advice would be greatly appreciated! Thanks!
Your output is wrong .. because 1356398971 + 30 = 1356399001 which is grater than 1356399000 if i understand you clearly this i what it should look like
$data = array(
99999 => array(
0 => 1356399000,
1 => 1356398971,
2 => 1356399005,
3 => 1356413406,
),
99997 => array(
0 => 1356399002,
1 => 1356399007,
2 => 1356398871,
3 => 1356398876,
),
99996 => array(
0 => 1356399003,
1 => 1356399004,
2 => 1356399008,
),
99995 => array(
0 => 1356399009,
),
);
echo "<pre>";
$data = array_map(function ($values) {
rsort($values);
$ci = new CachingIterator(new ArrayIterator($values));
$values = array();
foreach ( $ci as $ts ) {
if ($ci->hasNext()) {
abs($ci->current() - $ci->getInnerIterator()->current()) > 30 and $values[] = $ts;
} else {
$values[] = $ts;
}
}
sort($values);
return $values;
}, $data);
print_r($data);
Output
Array
(
[99999] => Array
(
[0] => 1356398971
[1] => 1356413406
)
[99997] => Array
(
[0] => 1356398871
[1] => 1356399002
)
[99996] => Array
(
[0] => 1356399003
)
[99995] => Array
(
[0] => 1356399009
)
)