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));
Related
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);
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 following array as response from db. I am trying to convert this database response into multidimensional array as per my requirement.
Array
(
[0] => Array
(
[0] => Array
(
[_id] => C10359
[AE] => Array
(
[0] => 89785
[1] => 89786
[2] => 89857
[3] => 89859
)
)
[1] => Array
(
[_id] => C10428
[AE] => Array
(
[0] => 50191
[1] => 50203
[2] => 50230
[3] => 50244
)
)
)
[1] => Array
(
[0] => Array
(
[_id] => C10350
[AE] => Array
(
[0] => 89785
[1] => 89786
[2] => 89857
[3] => 89859
)
)
[1] => Array
(
[_id] => C10430
[AE] => Array
(
[0] => 50191
[1] => 50203
[2] => 50230
[3] => 50244
)
)
)
)
Now I need to convert above array in following way.
Array
(
[0] => Array
(
[C10359] => Array
(
[0] => 89785
[1] => 89786
[2] => 89857
[3] => 89859
)
[C10428] => Array
(
[0] => 50191
[1] => 50203
[2] => 50230
[3] => 50244
)
)
[1] => Array
(
[C10350] => Array
(
[0] => 89785
[1] => 89786
[2] => 89857
[3] => 89859
)
[C10430] => Array
(
[0] => 50191
[1] => 50203
[2] => 50230
[3] => 50244
)
)
)
following is way i am trying
array_map(function($arr) {
return $arr[0] ;
},$panel_result);
But it is not working.
Kindly suggest how can I convert in required formate.
This should do the trick :
$arr = array(
array(
array(
'_id' => 'C10359',
'AE' => array
(
89785,
89786,
89857,
89859,
),
),
array(
'_id' => 'C10428',
'AE' => array
(
50191,
50203,
50230,
50244,
),
),
),
);
$output = array();
foreach ($arr as $levelK => $level) {
if(!isset($output[$levelK])){
$output[$levelK] = array();
}
foreach ($level as $subLevel) {
$id = $subLevel['_id'];
if (!isset($output[$levelK][$id])) {
$output[$levelK][$id] = array();
}
foreach ($subLevel['AE'] as $val) {
$output[$levelK][$id][] = $val;
}
}
}
Hope this helps.
Use array_column() and pass third param as the index key.
$reqArray = array();
foreach ($yourArray as $key => $innerArray) {
$reqArray[] = array_column($innerArray, 'AE', '_id');
}
OR
Use array map()
$reqArray = array_map(function($a){
return array_column($a, 'AE', '_id');
},$arr);
I have build this array structure from dig query data.
[10] => Array
(
[id] => 150
[0] => 200.201.202.23
[1] => dns.name1.com
[2] => 200.201.202.24
[3] => dns.name2.com
[4] => 200.201.202.25
[5] => dns.name3.com
) `
I need something like:
[10] => Array
(
[0] => array ( [0] => 200.201.202.23
[1] => dns.name1.com
[id] => 150
)
[1] => array ( [0] => 200.201.202.24
[1] => dns.name2.com
[id] => 150
)
[2] => array ( [0] => 200.201.202.25
[1] => dns.name3.com
[id] => 150
)
) `
I'm not sure if this is possible?
Heres the code where i create the array:
At the first time from the dig i use array_push() to add content to it.
$temp = array();
$i = 0;
foreach ($digResult as $single){
if (preg_match('/(?:^|\s+)(\d+)(?:\s+|\n+|$)/', $single )){
$temp []["id"]= $single;
$i++;
}else {
$temp[$i][] = $single;
}
}
This will work for you :
<?php
$dataArray = array(10 => array
(
'id' => 150 ,
0 => '200.201.202.23' ,
1 => 'dns.name1.com',
2 => '200.201.202.24',
3 => 'dns.name2.com',
4 => '200.201.202.25',
5 => 'dns.name3.com',
)
);
$newArray = array();
$id = $dataArray[10]['id'];
for($i=0; $i< 6; $i++)
{
$newArray[10][] = array(0=>$dataArray[10][$i],1=>$dataArray[10][$i+1],'id'=> $id);
$i+=1;
}
print_r($newArray);
?>
This will output
Array
(
[10] => Array
(
[0] => Array
(
[0] => 200.201.202.23
[1] => dns.name1.com
[id] => 150
)
[1] => Array
(
[0] => 200.201.202.24
[1] => dns.name2.com
[id] => 150
)
[2] => Array
(
[0] => 200.201.202.25
[1] => dns.name3.com
[id] => 150
)
)
)
LIVE EXAMPLE : CLICK HERE
Try this:-
<?php
$array = array(
'id' => '150',
'0' => '200.201.202.23',
'1' => 'dns.name1.com',
'2' => '200.201.202.24',
'3' => 'dns.name2.com',
'4' => '200.201.202.25',
'5' => 'dns.name3.com'
);
$i = 0;
$arrayLenght = (count($array)-2);
$newArray = array();
while ($i <= $arrayLenght) {
$newArray[] = array(
"0" => $array[$i++],
"1" => $array[$i++],
"id" => $array['id']
);
}
echo '<pre>';
print_r($newArray);
echo '</pre>';
?>
Output:-
Array
(
[0] => Array
(
[0] => 200.201.202.23
[1] => dns.name1.com
[id] => 150
)
[1] => Array
(
[0] => 200.201.202.24
[1] => dns.name2.com
[id] => 150
)
[2] => Array
(
[0] => 200.201.202.25
[1] => dns.name3.com
[id] => 150
)
)
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
)
)