Find point in certain array values and replace with comma - php

I have array like this
Array
(
[0] => Array
(
[0] => Price
[1] => 308.5
[2] => 2009
)
[1] => Array
(
[0] => Price
[1] => 308.5
[2] => 2009
)
)
and i need to replace "." with "," in key [0][1], [1][1], [2][1] etc

This will work, notice how you have to use sub_array with a reference
foreach ($arr as &$sub_arr) {
foreach ($sub_arr as &$val) {
$val = str_replace('.', ',', $val);
}
}

try
foreach($arr as $v) {
foreach($v as $v1) {
$newarr[] = str_replace('.', ',', $v1);
}
}
print_r($newarr);

Related

Change value inside an array

I'd like to change the values of an array.
Currently my array looks like this:
Array
(
[0] => Array
(
[0] => 12-Multi_select-customfield-retina-ready+Yes
[1] => 12-Multi_select-customfield-retina-ready+N/A
[2] => 12-Multi_select-customfield-retina-ready+No
)
)
I want to remove everything before the + symbol, so in the end the new array will looke like this
Array
(
[0] => Array
(
[0] => Yes
[1] => N/A
[2] => No
)
)
This is my code:
$new_array = array();
foreach( $array as $key => $value ) {
$split = explode("+", $value[0]);
$new_array[] = $split[1];
}
Hoping that it would worked, but when I check the new array, it only shows one value.
Array
(
[0] => Yes
)
Any help in putting me in the right direction is much appreciated.
Please, check it:
<?php
$array[0][0] = '12-Multi_select-customfield-retina-ready+Yes';
$array[0][1] = '12-Multi_select-customfield-retina-ready+N/A';
$array[0][2] = '112-Multi_select-customfield-retina-ready+No';
echo '<pre>';
print_r($array);
$new_array = array();
foreach( $array[0] as $key => $value ) {
$split = explode("+", $value);
$new_array[] = $split[1];
}
print_r($new_array);
echo '</pre>';
Try This this work even if you have multiple key in the original array $original_array[0], $original_array[1] ... :
$original_array[0] = [
0 => '12-Multi_select-customfield-retina-ready+Yes',
1 => '12-Multi_select-customfield-retina-ready+N/A',
2 => '12-Multi_select-customfield-retina-ready+No'
];
print_r($original_array);
$new_array = [];
foreach ($original_array as $key => $value) {
foreach ($value as $index => $val) {
$split = explode("+", $val);
$new_array[$key][] = $split[1];
}
}
print_r($new_array);
Example :
Original array
Array
(
[0] => Array
(
[0] => 12-Multi_select-customfield-retina-ready+Yes
[1] => 12-Multi_select-customfield-retina-ready+N/A
[2] => 12-Multi_select-customfield-retina-ready+No
),
[1] => Array
(
[0] => 12-Multi_select-customfield-retina-ready+Yes
[1] => 12-Multi_select-customfield-retina-ready+N/A
[2] => 12-Multi_select-customfield-retina-ready+No
)
)
New Array
Array
(
[0] => Array
(
[0] => Yes
[1] => N/A
[2] => No
),
[1] => Array
(
[0] => Yes
[1] => N/A
[2] => No
)
)

How to Sort all data from array

how can i crawl this array with a for loop or foreach ,
this is the content of a single array:
Array ( [0] => 1 ) Array ( [0] => 1 [1] => 2 ) Array ( [0] => 1 [1] => 2 [2] => 3 )
If you mean what I think you mean...
try this:
$tempArray = array();
foreach($myarray as $k => $v){
foreach($v as $ke => $val ){
$tempArray[] = $val;
}
}
sort($tempArray);
Or if you want to avoid duplicates try:
$temp = array_unique($myArray);
sort($temp);

How to sum array values based on keys?

The first array
Array
(
[0] => Array
(
[1] => 2
)
[1] => Array
(
[1] => 2
)
[2] => Array
(
[2] => 1
)
[3] => Array
(
[3] => 1
)
)
I want output like
Array
(
[0] => Array
(
[1] => 4
)
[1] => Array
(
[2] => 1
)
[2] => Array
(
[3] => 1
)
)
How can i do this?
Seems like a good case for array_reduce():
$res = array_chunk(array_reduce($arr, function(&$current, $item) {
foreach ($item as $key => $value) {
if (!isset($current[$key])) {
$current[$key] = 0;
}
$current[$key] += $value;
}
return $current;
}, []), 1, true);
For the final result I'm using array_chunk(); it takes an array and creates single element sub arrays of each element.
$result = array();
foreach ($input as $subarray) {
foreach ($subarray as $key => $value) {
if (isset($result[$key])) {
$result[$key][$key] += $value;
} else {
$result[$key] = array($key => $value);
}
}
}
$result = array_values($result); // Convert from associative array to indexed array

2-dimensional Array - unset if not value

I have a 2-dimensional array and want to delete all elements, whose values are not "Name1". They should keep their index numbers (keys):
Array
(
[array001] => Array
(
[0] => Name1
[1] => Name2
[2] => Name3
[3] => Name1
)
[array002] => Array
(
[0] => Name2
[1] => Name1
[2] => Name4
)
[array003] => Array
....
)
will output
Array
(
[array001] => Array
(
[0] => Name1
[3] => Name1
)
[array002] => Array
(
[1] => Name1
)
[array003] => Array
....
)
Possible solutions could be achieved with a foreach loop, with preg_replace, when the array is converted into a string: $array = print_r($array,true);
none of them is working..
I found the solution by myself:
foreach($array as $key => $value) {
foreach($value as $innerkey => $innervalue){
if($innervalue != 'Name1'){
unset($array[$key][$innerkey]);
}
}
}
foreach($array as $key => $value) {
foreach ($value as $string) {
if ($string !== "Name1") {
unset($string);
}
}
}
try this:
function removeElementDifferentValue($array, $value){
foreach($array as $subKey => $val){
if($val != $value){
unset($array[$subKey]);
}
}
return $array;
}
$array = removeElementWithValue($array, 'Name1');
foreach($array as $key1 => $val1) {
foreach($val1 as $key2 => $val2) {
if(strcmp($val2,"Name1") != 0) {
unset($array[$key1][$key2]);
}
}
}

How to map array to element of other corespodning array?

I Have an array A
$A = Array
(
[0] => Array
(
[0] => A0;B0;C0;E0;;626.00;
[1] => A1;B1;C1;E1;;6212.00;
[2] => A2;B2;C3;E2;;226.00;
[3] => A3;B3;C3;E3;;632.00;
)
)
$B = Array
(
[0] => Array
(
[0] => REP00
[1] =>
[2] => REP02
[3] =>
)
)
I am going to write the
function map_array_element($A,$B){
$C = array();
foreach($A as $key=>$value){
foreach($value as $k=>$v){
$empty_case = str_replace(';;', '; ', $v);
$row = explode(';', $replace);
/*the idea to got the array of
$row=array(
0=>A,
1=>B,
2=>C,
3=>E,
4=> //this element will map to corresponding $B )
*/
//TODO
}
}
return $C;
}
Tthis function will return an new array like this that will
$C = Array
(
[0] => Array
(
[0] => A0;B0;C0;E0;REP00;626.00;
[1] => A1;B1;C1;E1;;6212.00;
[2] => A2;B2;C3;E2;REP02;226.00;
[3] => A3;B3;C3;E3;;632.00;
)
)
Who know to do this manipulate?
thanks
You are almost there. You need to take care of the right index of array C.
function map_array_element($A,$B){
$C = array();
foreach($A as $key=>$value){
foreach($value as $k=>$v){
$row = explode(';', $v);
$row[4] = $B[$key][$k];
$C[$key][$k] = implode(';',$row);
}
}
return $C;
}
See it

Categories