I have print_r result like this :
Array
(
[0] => A, B, C, D
[1] => 15,20,24,19
)
how to make them like this :
Array
(
[A] => 15
[B] => 20
[C] => 24
[D] => 19
)
Great thanks for help :)
Try this:
$a = array('A', 'B', 'C', 'D');
$b = array(15, 20, 24, 19);
$c = array();
foreach ($a as $index => $value) {
if (isset($b[$index])) {
$c[$value] = $b[$index];
}
}
var_dump($c);
You need explode() and array_combine(). Assuming your initial array, $arr:
$new_arr = array_combine(explode(', ', $arr[0]), explode(',', $arr[1]));
See demo
Try to explode() your array index by comma and combine both array with keys and values using array_combine()
$a = explode(',',$arr[0]);
$b = explode(',',$arr[1]);
$new = array_combine($a,$b);
print_r($new); //Array ( [A] => 15 [ B] => 20 [ C] => 24 [ D] => 19 )
array_combine is the way
<?php
$myArray = array(
array('A', 'B', 'C', 'D'),
array(15, 20, 24, 19)
);
$combinedArray = array_combine($myArray[0], $myArray[1]);
Related
I have array something like:
Array (
[0]=>{"a":"b", "c":"d", "h":"e"}
[1]=>{"a":"b", "f":"g"}
)
How i can removing duplicates here? I'm trying array_unique, but it's not working.
Expected result:
Array (
[0]=>{"a":"b", "c":"d", "h":"e"}
[1]=>{"f":"g"}
)
In this scenario, you need to be careful with duplicate keys yet different values. So match to remove duplicates has to be on combination of both key and value.
To do this, we can collect all keys in an array say $map and have all values visited for this keys inside that key array.
Now, we can just do an in_array check to get hold of whether we got some key-value pair like this before or not.
Snippet:
$arr = [
[
'a' => 'b',
'c' => 'd',
'h' => 'e'
],
[
'a' => 'b',
'f' => 'g',
'c' => 'f'
],
[
'a' => 'd',
'c' => 'd'
]
];
$map = [];
foreach($arr as $index => $data){
foreach($data as $key => $value){
if(!isset($map[$key])) $map[$key] = [];
if(in_array($value,$map[$key])) unset($arr[$index][$key]);
else $map[$key][] = $value;
}
}
print_r($arr);
Demo: https://3v4l.org/RWcMu
You can do it with array_diff() and unset() functions. But, you need to decode this JSON values firstly:
foreach($ar as $in => &$js){
$ar[$in] = json_decode($js,true);
}
After this $ar has a view like:
Array
(
[0] => Array
(
[a] => b
[c] => d
[h] => e
)
[1] => Array
(
[a] => b
[f] => g
)
)
Here you can apply array_diff() function:
$diff = array_diff($ar[1],array_diff($ar[1],$ar[0]));
It will collect duplicates from [1] index in [0]:
Array
(
[a] => b
)
Now you can unset these values from [1] index:
foreach($diff as $ind=>$uns){
unset($ar[1][$ind]);
}
And finally you can change JSON view back:
foreach($ar as $in=>&$js){
$ar[$in] = json_encode($js);
}
Result would be:
Array
(
[0] => {"a":"b","c":"d","h":"e"}
[1] => {"f":"g"}
)
Demo
If input elements are objects, then use this loop at the first step:
foreach($ar as $in=>&$obj){
$ar[$in] = (array)$obj;
}
Demo
I am trying to find a way to transform a string type variable into an array type variable. To be more precise, what i am looking for is the change this (example):
$v = "['1', 'a', ['2', 'b', ['3'], 'c']]";
note that this is not a json-formatted string.
into this:
$v = ['1', 'a', ['2', 'b', ['3'], 'c']];
Note the double-quotes in the first example, $v is a string, not an array, which is the desired effect.
Simple solution using str_replace(to prepare for decoding) and json_decode functions:
$v = "['1', 'a', ['2', 'b', ['3'], 'c']]";
$converted = json_decode(str_replace("'",'"',$v));
print_r($converted);
The output:
Array
(
[0] => 1
[1] => a
[2] => Array
(
[0] => 2
[1] => b
[2] => Array
(
[0] => 3
)
[3] => c
)
)
$v = "['1', 'a', ['2', 'b', ['3'], 'c']]";
eval("\$v = $v;");
var_dump($v);
PS: make sure $v string doesn't contain unexpected code.
This should work:
$json = "['1', 'a', ['2', 'b', ['3'], 'c']]";
$json = str_replace("'",'"',$json);
$result_array = json_decode($json); // This is your array
I have an array of some values which I need to convert to new array and sum every value with all previous values. For example (array length, keys and values always differ), this is what I have:
Array
(
[0] => 1
[1] => 1
[2] => 5
[3] => 1
[4] => 1
[7] => 1
[8] => 3
[9] => 1
)
and this is what I need:
Array
(
[0] => 1
[1] => 2
[2] => 7
[3] => 8
[4] => 9
[7] => 10
[8] => 13
[9] => 14
)
I tried many different ways but always stuck at something or realized that I'm wrong somewhere. I have a feeling that I'm trying to reinvent a wheel here, because I think there have to be some simple function for this, but had no luck with finding solution. This is last way I tried:
$array = array( "0"=> 1, "1"=> 1, "2"=> 5, "3"=> 1, "4"=> 1, "7"=> 1, "8"=> 3, "9"=> 1 );
$this = current($array);
$next = next($array);
$end = next(end($array));
$sum = 0;
$newArray = array();
foreach ($array as $val){
if($val != $end){
$sum = ($this += $next);
array_push($newArray, $sum);
}
}
print_r($newArray);
..unfortunately wrong again. I spend lot of time finding ways how not to get where I need to be, can someone kick me into right direction, please?
Suggest you to use array_slice() & array_sum()
$array = array( "0"=>1,"1"=>1,"2"=>5,"3"=>1,"4"=>1,"7"=>1,"8"=>3,"9"=>1);
$keys = array_keys($array);
$array = array_values($array);
$newArr = array();
foreach ($array as $key=>$val) {
$newArr[] = array_sum(array_slice($array, 0, $key+1));
}
$newArr = array_combine($keys, $newArr);
print '<pre>';
print_r($newArr);
print '</pre>';
Output:
Array
(
[0] => 1
[1] => 2
[2] => 7
[3] => 8
[4] => 9
[7] => 10
[8] => 13
[9] => 14
)
Reference:
array_slice()
array_sum()
array_combine()
array_keys()
let assume your array variable is $a;
You can use simple for loop
for($i=1;$i<sizeof($a);$i++)
{
$a[$i]=$a[$i]+$a[$i-1];
}
You can just go over the array sum and add to a new array (in a simple way):
$array = array( "0"=> 1, "1"=> 1, "2"=> 5, "3"=> 1, "4"=> 1, "7"=> 1, "8"=> 3, "9"=> 1 );
var_dump($array);
$newArray = array();
$sum = 0;
foreach ($array as $a){
$sum += $a;
$newArray[]=$sum;
}
var_dump($newArray);
$sums = array_reduce($array, function (array $sums, $num) {
return array_merge($sums, [end($sums) + $num]);
}, []);
In other words, this iterates over the array (array_reduce) and in each iteration appends (array_merge) a number to a new array ($sums) which is the sum of the previous item in the array (end($sums)) and the current number. Uses PHP 5.4+ array syntax ([]).
You complicated too much. Loop trough all elements of array and add current element to sum. Then assign that sum to new array.
$array = array( "0"=> 1, "1"=> 1, "2"=> 5, "3"=> 1, "4"=> 1, "7"=> 1, "8"=> 3, "9"=> 1 );
$sum = 0;
$newArray = array();
foreach ($array as $key=>$val){
$sum += $val;
$newArray[$key]=$sum;
}
Make sure to get indexes right.
I have two arrays and want to find the first match for either of arrayTwos values in arrayOne.
arrayOne ( [0] = C [1] = A [2] = B [3] = D [4] = B [5] = C)
and
arrayTwo ( [0] = A [1] = B [2] = C )
With these values I would want to return the value "C" as it is the first value in arrayTwo to appear in arrayOne.
I'm thinking I could use for loops and if statements to run through but re there any functions in PHP I could use to simplify this?
Use array_search
$keys = array_search($second_array, $first_array);
Ref : http://in3.php.net/array_search
array_search
$valuekeys = array_search($secondarray, $arrayone);
use array_intersect
$arrayOne = array('C', 'A', 'B', 'D', 'B', 'C');
$arrayTwo = array('A', 'C');
$result = array_intersect($arrayOne , $arrayTwo);
echo $result[0];
Use array_intersect. This will do the job. http://www.php.net/manual/en/function.array-intersect.php Note the difference between using array_intersect($array1, $array2) and array_intersect($array2, $array1)
You can use array_intersect():
$arr1 = array( 0 => 'C', 1 => 'A', 2 => 'B', 3 => 'D', 4 => 'B', 5 => 'C');
$arr2 = array( 0 => 'A', 1 => 'B', '2' => 'C' );
$arr3 = array_intersect($arr1,$arr2);
var_dump($arr3[0]);
string(1) "C"
From a function I am given a multidimensional array like this:
array(
[0] => array(
[0] => 7,
[1] => 18
),
[1] => array(
[0] => 12,
[1] => 7
),
[2] => array(
[0] => 12,
[1] => 7,
[2] => 13
)
)
I need to find duplicate values in the 3 arrays within the main array. For example, if value 7 repeats in the 3 arrays, return 7.
<?php
$array = array(array(7,18), array(12,7), array(12, 7, 13));
$result = array();
$first = $array[0];
for($i=1; $i<count($array); $i++){
$result = array_intersect ($first, $array[$i]);
$first = $result;
}
print_r($result);//7
?>
use my custom function
function array_icount_values($arr,$lower=true) {
$arr2=array();
if(!is_array($arr['0'])){$arr=array($arr);}
foreach($arr as $k=> $v){
foreach($v as $v2){
if($lower==true) {$v2=strtolower($v2);}
if(!isset($arr2[$v2])){
$arr2[$v2]=1;
}else{
$arr2[$v2]++;
}
}
}
return $arr2;
}
$arr = array_icount_values($arry);
echo "<pre>";
print_r($arr);
exit;
OUPUT
Array
(
[7] => 3
[18] => 1
[12] => 2
[13] => 1
)
hope this will sure help you.
$input = array("a" => "green", "red", "b" => "green", "blue", "red");
$result = array_unique($input);
print_r($result);
use this code
$array = array(0 => 'blue', 1 => 'red', 2 => 'green', 3 => 'red');
$key = array_search('green', $array); // $key = 2;
You will need to loop through the first array, and for each value in it, see if it is in_array().
$findme = array();
foreach ($array[0] as $key => $value)
{
if (in_array ($value, $array[1]) && in_array ($value, $array[2]))
{
$findme[] = $value;
}
}
// $findme will be an array containing all values that are present in all three arrays (if any).