I've array multidimentional ..
Array (
[123] => Array ( [0] => 120 [1] => 200 [2] => 180 [3] => 130 )
[124] => Array ( [0] => 150 [1] => 155 [2] => 160 [3] => 165 )
[125] => Array ( [0] => 121 [1] => 120 [2] => 121 [3] => 121 )
)
I want to convert like this
120,200,180,130
150,155,160,165
121,120,121,121
how to code this guys ?
my code from stackoverflow too ..
echo join("','", array_map(function ($data) { return $data[0]; }, $data))
but .. the output
120, 150, 121 .. i want to get from 123
This should work for you:
(Here I just go through each innerArray with array_map() and implode() it and print it)
<?php
$arr = [
"123" => [120, 200, 180, 130],
"124" => [150, 155, 160, 165],
"125" => [121, 120, 121, 121]
];
array_map(function($v){
echo implode(",", $v) . "<br />";
}, $arr);
?>
Output:
120,200,180,130
150,155,160,165
121,120,121,121
You can simply iterate over all items in the $arrs and use implode to format every single array:
$arrs = Array (
123 => Array ( 0 => 120, 1 => 200, 2 => 180, 3 => 130 ),
124 => Array ( 0 => 150, 1 => 155, 2 => 160, 3 => 165 ),
125 => Array ( 0 => 121, 1 => 120, 2 => 121, 3 => 121 ),
)
foreach($arrs as $arr) {
echo implode(",",$arr)."\n";
}
"\n" means you append a new line in raw text. In case you want to use HTML for formatting, you should evidently use <br/>:
foreach($arrs as $arr) {
echo implode(",",$arr).'<br/>';
}
$newArr = array();
foreach($yourArr as $key =>$val)
{
$newArr[] = implode(",",$val);
}
foreach($newArr as $arr)
{
echo $arr."<br>";
}
output
120,200,180,130
150,155,160,165
121,120,121,121
Related
I have this two arrays:
$arr1 = Array ( 600 => 580, 500 => 480, 100 => 80 ) <- always 3
$arr2 = Array ( 'filt' => grey, 'or' => 90 ) <- this array is dynamic
How can I combine each key and value from first array with the other array? I mean, I need:
$array = ( 600 => 580, 'filt' => grey, 'or' => 90 )
$array = ( 580 => 480, 'filt' => grey, 'or' => 90 )
successively in for/foreach loop. How can I do this?
Thanks a lot, im new on php ^^ and sorry for my english :P
I think you need this. Just loop through the first array, copy the first array in the out array and add key, value of loop array into the out array.
As you mention:
$array = ( 600 => 580, 'filt' => grey, 'or' => 90 )
$array = ( 580 => 480, 'filt' => grey, 'or' => 90 )
I think you need to create different array each time. For each of the
element of first array you need different array as output.
$arr1 = array( "600" => 580, "500" => 480, "100" => 80 );
$arr2 = array( 'filt' => "grey", 'or' => 90 );
$out = array();
foreach($arr1 as $key => $value){
$out = $arr2;
$out[$key] = $value;
print_r($out);
}
Result
First Iteration:
Array
(
[filt] => grey
[or] => 90
[600] => 580
)
Second Iteration:
Array
(
[filt] => grey
[or] => 90
[500] => 480
)
Third Iteration:
Array
(
[filt] => grey
[or] => 90
[100] => 80
)
Try this:
$arr1 = array( '600' => '580', '500' => '480', '100' => '80' ) ;
$arr2 = array( 'filt' => 'grey', 'or' => '90' );
$arra_new = array();
foreach($arr1 as $key=>$arr) {
$temp = $arr2;
$temp[$key] = $arr;
$arra_new[] = $temp;
}
print '<pre>';print_r($arra_new);exit;
The solution using array_walk and array_replace functions:
$arr1 = Array(600 => 580, 500 => 480, 100 => 80);
$arr2 = Array('filt' => 'grey', 'or' => 90, 'and' => 150, 'if' => 10);
$combined = [];
array_walk($arr1, function($v, $k) use($arr2, &$combined) {
$combined[] = array_replace([$k => $v], $arr2);
});
print_r($combined);
The output:
Array
(
[0] => Array
(
[600] => 580
[filt] => grey
[or] => 90
[and] => 150
[if] => 10
)
[1] => Array
(
[500] => 480
[filt] => grey
[or] => 90
[and] => 150
[if] => 10
)
[2] => Array
(
[100] => 80
[filt] => grey
[or] => 90
[and] => 150
[if] => 10
)
)
try this method..
<?php
$arr1 = array(600 => 580, 500 => 480, 100 => 80 );
$arr2 = array( 'filt' => 'grey', 'or' => 90 );
$newArray =array();
foreach($arr1 as $key =>$val)
{
$temp =array();
$temp[$key]= $val;
$newArray[] =$temp+$arr2;
}
echo "<pre>"; print_r($newArray);
?>
This will Output:
Array
(
[0] => Array
(
[600] => 580
[filt] => grey
[or] => 90
)
[1] => Array
(
[500] => 480
[filt] => grey
[or] => 90
)
[2] => Array
(
[100] => 80
[filt] => grey
[or] => 90
)
)
i have a Multiple Dimensional array and i need to sum up values which have the same keys .
print_r($inputs)
Array
(
[0] => Array
(
[id] => colors
[power] => Array
(
[green] => 12
[red] => 5
[orange] => 9
[black] => 6
[white] => 5
[blue] => 11
)
)
[1] => Array
(
[id] => colors
[power] => Array
(
[green] => 20
[red] => 40
[orange] => 80
[black] => 60
[white] => 100
[blue] => 110
)
)
[2] => Array
(
[id] => glossycolor
[power] => Array
(
[green] => 20
[red] => 40
[orange] => 80
[black] => 60
[white] => 100
[blue] => 110
)
)
)
i need the result to be like
Array
(
[0] => Array
(
[id] => colors
[power] => Array
(
[green] => 32
[red] => 45
[orange] => 89
[black] => 66
[white] => 105
[blue] => 121
)
)
[1] => Array
(
[id] => glossycolor
[power] => Array
(
[green] => 20
[red] => 40
[orange] => 80
[black] => 60
[white] => 100
[blue] => 110
)
)
)
i tried to use array_shift to sort the values and sum the sub array values but i failed
$finalRate = array_shift($inputs);
foreach ($inputs as $val) {
foreach ($val as $key => $val) {
$finalRate[$key] += $val;
}
}
but failed and return empty array .
Assuming your arrays have always the same structure I'd go with:
$outcome = array();
foreach ($colors as $array) {
$id = $array['id'];
if (array_key_exists($id, $outcome)) {
foreach ($array['power'] as $color => $value) {
$outcome[$id]['power'][$color] += $value;
}
continue;
}
$outcome[$array['id']] = $array;
}
array_values($outcome);
$array1 = array_slice($input,0,1); //slicing first value of $input i.e Array([0]=>array)
$array2 = array_slice($input,1,1); //slicing second value of $input i.e Array([1]=>array)
$array = array_sum_values($array1,$array2); //summing values of two arrays
$input = array_splice($input,0,2,$array) //Removing [0] and [1] from $input and replacing with $array.
Please refer PHP manual for more details.
This probably isn't the most efficient way for going about this.. but it works and was easy to implement:
$arr = array(
0 => array(
'id' => 'colors',
'power' => array(
'green' => 12,
'red' => 5,
'orange' => 9,
'black' => 6,
'white' => 5,
'blue' => 11,
),
),
1 => array(
'id' => 'colors',
'power' => array(
'green' => 20,
'red' => 40,
'orange' => 80,
'black' => 60,
'white' => 100,
'blue' => 110,
),
),
2 => array(
'id' => 'glossycolors',
'power' => array(
'green' => 20,
'red' => 40,
'orange' => 80,
'black' => 60,
'white' => 100,
'blue' => 110,
),
),
);
foreach ( $arr as $k1 => $v1 ) {
foreach ( $arr as $k2 => $v2 ) {
if ( $k1 === $k2 ) continue;
if ( ! isset( $arr[ $k1 ] ) || ! isset( $arr[ $k2 ] ) ) continue;
if ( $v1['id'] === $v2['id'] ) {
foreach ( $v2['power'] as $power_k => $power_v ) {
$arr[$k1]['power'][$power_k] += $power_v;
}
unset( $arr[$k2] );
}
}
}
print_r( $arr );
And this results in:
Array
(
[0] => Array
(
[id] => colors
[power] => Array
(
[green] => 32
[red] => 45
[orange] => 89
[black] => 66
[white] => 105
[blue] => 121
)
)
[2] => Array
(
[id] => glossycolors
[power] => Array
(
[green] => 20
[red] => 40
[orange] => 80
[black] => 60
[white] => 100
[blue] => 110
)
)
)
So basically, it loops through the same array twice and sums the values of common 'id' elements, then removes the second copy from the array leaving only the original with the sum of the later. Cheers
I am trying to use an array to search for a value that is inside of an array and then take the full array that the value is in and add it to an array. Below is the array to get the value from:
Array (
[0] => Array ( [ID] => 138 [dimmer] => 5 [order] => 1 [double] => 0 [location1] => DSR [location2] => Stage Pockets )
[1] => Array ( [ID] => 139 [dimmer] => 6 [order] => 1 [double] => 0 [location1] => DSR [location2] => Stage Pockets )
[2] => Array ( [ID] => 140 [dimmer] => 7 [order] => 2 [double] => 0 [location1] => DSR [location2] => Stage Pockets )
[3] => Array ( [ID] => 141 [dimmer] => 8 [order] => 2 [double] => 0 [location1] => DSR [location2] => Stage Pockets )
)
I am trying to get the value of dimmer with the search function below:
function search($array, $key, $value)
{
$results = array();
if (is_array($array))
{
if (isset($array[$key]) && $array[$key] == $value)
$results[] = $array;
foreach ($array as $subarray)
$results = array_merge($results, search($subarray, $key, $value));
}
return $results;
}
Below it uses the $chan value which is an integer to use the function above to search an array. The foreach is then supposed to go through the array that is $patch and select the arrays out of the array above (only returns an empty array), although it doesn't do that unless you change $patch_single['dimmer'] with a string such as "7".
$patch = search($patch, 'Channel', $chan);
foreach ($patch as $patch_single) {
print_r($patch_single);
$dim_single = intval($patch_single['dimmer']);
echo $dim_single;
$dimmers = search($dimmers, 'dimmer', $dim_single);
}
The array that is being used to get $patch_single['dimmer'] is, when inside the foreach:
Array ( [ID] => 241 [Channel] => 100 [dimmer] => 7 )
Array ( [ID] => 242 [Channel] => 100 [dimmer] => 25 )
Thank you for your advice.
Hm, i see that you have 2 dimensional array. Why you just don't use this?
foreach($array as $row) {
if ($row['dimmer'] == $myValue) { $newArray[] = $row; }
}
Try this :
$arr = Array (Array ( "ID" => 138, "dimmer" => 5, "order" => 1, "double" => 0, "location1" => "DSR", "location2" => "Stage Pockets" ),
Array ( "ID" => 139, "dimmer" => 6, "order" => 1, "double" => 0, "location1" => "DSR", "location2" => "Stage Pockets" ),
Array ( "ID" => 140, "dimmer" => 7, "order" => 2, "double" => 0, "location1" => "DSR", "location2" => "Stage Pockets" ),
Array ( "ID" => 141, "dimmer" => 8, "order" => 2, "double" => 0, "location1" => "DSR", "location2" => "Stage Pockets" ));
$arr = array_filter($arr, function($ar) {
return ($ar['dimmer'] == '7' );
});
echo "<pre>";
print_r($arr);
Output :
Array
(
[2] => Array
(
[ID] => 140
[dimmer] => 7
[order] => 2
[double] => 0
[location1] => DSR
[location2] => Stage Pockets
)
)
ref: http://php.net/manual/en/function.array-filter.php
I have the following code, and I'd like to get away from the call-time pass-by-reference, (To convert from 5.2 to 5.3) but I'm not sure exactly sure what the correct way to do this would be (class, global variable, ?)
Here is a codepad that should have everything in it
http://codepad.org/ombgFPMR
<?php
function count_things($item, $key, $total) {
$total++;
}
$counts = array(100 => 1,
101 => 1,
102 => array(
106 => 1,
107 => 1
),
103 => 1,
104 => 1,
105 => array(
108 => 1,
109 => array(
110 => 1,
111 => 1,
112 => 1
)
)
);
foreach($counts as $key => $count) {
$total = 0;
if(is_array($count)) {
$total++;
/* The below is a logic error. Array elements that contain arrays do not get
the callback function called on them. Therefore, any children with children
of their own will not be counted. In the output of this paste,
the final key, $final_counts[105]['total'], should have a value of 6, but it
actually has a value of 5. */
array_walk_recursive($count, 'count_things', &$total);
} else {
$total = $count;
}
$final_counts[$key]['total'] = $total;
}
print_r($final_counts);
?>
Output looks like:
Array
(
[100] => Array
(
[total] => 1
)
[101] => Array
(
[total] => 1
)
[102] => Array
(
[total] => 3
)
[103] => Array
(
[total] => 1
)
[104] => Array
(
[total] => 1
)
[105] => Array
(
[total] => 5
)
)
You can use count with COUNT_RECURSIVE flag.
You should use closures for this, these were introduced in 5.3.0 so they should work.
<?php
$counts = array(
100 => 1,
101 => 1,
102 => array(
106 => 1,
107 => 1
),
103 => 1,
104 => 1,
105 => array(
108 => 1,
109 => array(
110 => 1,
111 => 1,
112 => 1
)
)
);
$final_counts = array();
foreach($counts as $key => $count) {
if(is_array($count)) {
$total = 1;
array_walk_recursive($count, function() use (&$total) {
$total++;
});
} else {
$total = $count;
}
$final_counts[$key]['total'] = $total;
}
print_r($final_counts);
I might be able to provide a better solution if you put your problem in context.
It feels I am going way over my head while discovering the ultimate usage of arrays.
I have two arrays, where the first has main keys, and the value is a count of files attached to that key.
The goal is to match the keys of this first array to the values in a second array, but still mainting (and show) the (value)count of Array-1 -- but for only the values in the second array.
Seems somewhat hazy perhaps, but here are the arrays. The second one has the values that should match the keys in the first.
(My problem is that I keep losing the values of array 1 with every attempt I make.)
Hope you can help me out with this one.
(working matches are keys like: 125, 2051 & 2214)
Array 1:
Array (
[6960] => 3
[2214] => 4
[2051] => 4
[6944] => 2
[6938] => 4
[1823] => 1
[766] => 6
[3993] => 4
[5896] => 6
[6927] => 2
[4220] => 3
[77] => 3
[83] => 1
[125] => 2
[6618] => 2
[196] => 1
[4072] => 12
[3718] => 1
[5918] => 1
[3388] => 10
[4500] => 13
[5968] => 2
[3000] => 2
[942] => 1
[4246] => 8
[5868] => 2
[6394] => 3
[1168] => 1
[2163] => 1
[1827] => 2
[2071] => 8
[4597] => 1
[1702] => 7
)
Array 2:
Array (
[0] => 1024
[1] => 1076
[2] => 111
[3] => 124
[4] => 125
[5] => 1301
[6] => 1409
[7] => 2051
[8] => 2214
[9] => 2636
[10] => 3246
[11] => 4838
[12] => 6946
[13] => 6955
[14] => 6961
[15] => 73
[16] => 74
[17] => 8
)
What about doing this:
<?php
$arr1 = array(1 => 1000, 500 => 1111, 1000 => 5000, 5000 => 5555);
$arr2 = array(1, 5000);
print_r(array_intersect_key($arr1, array_flip($arr2)));
OUTPUT:
(
[1] => 1000
[5000] => 5555
)
Or, using your data:
<?php
$arr1 = array(6960 => 3, 2214 => 4, 2051 => 4, 6944 => 2, 6938 => 4, 1823 => 1, 766 => 6, 3993 => 4, 5896 => 6, 6927 => 2, 4220 => 3, 77 => 3, 83 => 1, 125 => 2, 6618 => 2, 196 => 1, 4072 => 12, 3718 => 1, 5918 => 1, 3388 => 10, 4500 => 13, 5968 => 2, 3000 => 2, 942 => 1, 4246 => 8, 5868 => 2, 6394 => 3, 1168 => 1, 2163 => 1, 1827 => 2, 2071 => 8, 4597 => 1, 1702 => 7);
$arr2 = array(1024, 1076, 111, 124, 125, 1301, 1409, 2051, 2214, 2636, 3246, 4838, 6946, 6955, 6961, 73, 74, 8);
print_r(array_intersect_key($arr1, array_flip($arr2)));
OUTPUT:
Array
(
[2214] => 4
[2051] => 4
[125] => 2
)
array_interset_keys will find the intersection of arrays by keys, not values. Since your second array is an index based array (not an associative array) we need to first flip the keys and values using array_flip. Then the keys can be intersected.
Your question is somewhat unclear, but I think this is what you're looking for:
foreach( $array2 as $key)
{
$count = ( isset( $array1[ $key ]) ? $array1[ $key ] : 0);
echo $key . ' has ' . $count . ' files.';
}
Uhhmm.. i cant seem to understand what you want to imply.. but from the way i see it.. if you want to have the keys of array 1 as value to array 2.. just do this code..
foreach($array1 as $key=>$val) {
$array2[] = $key;
}
This should grab the KEYS of array1 and insert it to your array2[].
Hope this helps you.. Cheers :)
This should print out what you need:
foreach($array2 as $key=>$val) {
echo $val;
foreach($array1 as $key2 => $val2){
if($key == $val2){
echo $val2;
}
}
echo '\n'; // new line
}