Grab value from complex array and output as simple associative array [duplicate] - php

This question already has answers here:
How to use array values as keys without loops? [duplicate]
(6 answers)
Closed 6 years ago.
Please check attached images. These image contains $complex_arr print and $simple_arr print results. I want to convert $complex_arr to $simple_arr output.
How is that possible? What is actually doing each of $complex_arr inside value will be converted as associative simple array like $simple_arr
$arr1 = array("asso1"=>"a", "asso2"=>"1");
$arr2 = array("asso1"=>"b", "asso2"=>"2");
$arr3 = array("asso1"=>"c", "asso2"=>"3");
$complex_arr = array($arr1,$arr2,$arr3);
$simple_arr = array("a"=>"1", "b"=>"2", "c"=>"3");
// print_r($complex_arr);
print_r($simple_arr);
Input:
Output:

You have to write it on our own... here is my idea:
public function makeItSimpler($arr){
$newarr = array();
foreach($arr as $complex){
$newarr[$complex['asso1']]=$complex['asso2'];
}
return $newarr;
}
Perhaps you can do it better... take look at "array-map" http://php.net/manual/de/function.array-map.php
Good Luck

foreach ($complex_arr as $key => $value) {
$simple_arr[$value['asso1']]=$simple_arr[$value['asso2']];
}

With php5.5 (where array_column becomes available) it is:
$simple_arr = array_combine(
array_column($complex_arr, 'asso1'),
array_column($complex_arr, 'asso2')
);
And even simplier (after I reread function manual):
$simple_arr = array_column($complex_arr, 'asso2', 'asso1');

Related

How to add an array together without using array_sum [duplicate]

This question already has answers here:
Sum values in foreach loop php [closed]
(5 answers)
Closed 3 years ago.
I'm tryng to learn PHP and I'm trying to do it by doing things the long way round.
So I came across array_sum() and I was wondering if there is a way to calculate an array without using it.
for example
$my_array = array(10, 80, 30);
I've only got as far as this and I'm stumped. I have looked on google but I haven't found anything.
$implode = implode(",", $my_array );
$explode = explode(",", $implode);
foreach($explode as $test)
{
}
I will always go for native functions as they are optimized as well as tested.[so basically i want to say that use array_sum()],
Apart from above, below is foreach() process to sum all values of a single-dimensional array[just for knowledge sake]
<?php
$result = 0; //create a variable with 0 value
foreach($explode as $test)
{
$result += $test; //add array value to the newly created value
}
echo $result; //print final sum of all array values
Output:-https://3v4l.org/0qMJu
Note:- please read #Markus Zeller comment under question

how to remove duplicate elements from an array of arrays in PHP [duplicate]

This question already has answers here:
How to remove duplicate values from a multi-dimensional array in PHP
(18 answers)
Closed 5 years ago.
"2017-08-31":["5948a0dd21146a43fdcfef5a","5948a0dd21146a43fdcfef5a"]
"2017-08-22":["5948a0dd21146a43fdcfef5a"]
"2017-08-09":["59461ceae6179b19403c6a19","59461ceae6179b19403c6a19"]
"2017-08-08":["59461ceae6179b19403c6a19","59461ceae6179b19403c6a19"]
I have an array like this, key is a date and multiple value associated with that date, but I need unique value with that key.how to do that ?
I've tried with array_unique but no luck!
Just use array_map and array_unique together.
<?php
$a = json_decode('{"2017-08-31":["5948a0dd21146a43fdcfef5a","5948a0dd21146a43fdcfef5a"],
"2017-08-22":["5948a0dd21146a43fdcfef5a"],
"2017-08-09":["59461ceae6179b19403c6a19","59461ceae6179b19403c6a19"],
"2017-08-08":["59461ceae6179b19403c6a19","59461ceae6179b19403c6a19"]}', true);
echo json_encode(array_map("array_unique", $a));
$array = your array
$array = array_map(function($val){return array_unique($val);}, $array);
You better do this using SQL if it's possible. Otherwise, you can try this:
$array = array_unique($array, SORT_REGULAR);

PHP - multidimensional array from arrays [duplicate]

This question already has answers here:
Transposing multidimensional arrays in PHP
(12 answers)
Closed 1 year ago.
I've got 6 arrays - 1 with name and 5 with some properties - which should be assigned to that name. All values are of course in order. I'd like to make a 2-dimensional array with will be later put into CSV and the result should be as on the table here:
I guess that i have to do 2 loops here, but I can't make them work. How to construct such array?
Solution found
I've connected all arrays:
$final_array = array($nazwa_array,$new_ilosc_array,$new_koszt_array,$new_cena_lifo_array,$new_cena_fifo_array,$new_rodzaj_array);
I've found a matrix transposition function, which returns array in correct order:
function transpose($array) {
array_unshift($array, null);
return call_user_func_array('array_map', $array);
}
$a = array();
foreach ( $names AS $key => $value ) {
$a[$key]['name'] = $value;
$a[$key]['property1'] = $value.'->'.$property1_array[$key];
$a[$key]['property2'] = $value.'->'.$property2_array[$key];
$a[$key]['property3'] = $value.'->'.$property3_array[$key];
$a[$key]['property4'] = $value.'->'.$property4_array[$key];
$a[$key]['property5'] = $value.'->'.$property5_array[$key];
}

Remove array duplicates PHP [duplicate]

This question already has answers here:
php remove duplicates from array
(5 answers)
Closed 9 years ago.
How do I remove duplicates from an array?
Let's say that my I have two arrays named $array and $new_array. $array has contents while $new_array is empty, as seen below:
$array = array(5,1,2,1,5,7,10);
$new_array = array();
I want $new_array to store the unique values of $array. It kind of goes like this:
$array = array(5,1,2,1,5,7,10);
$new_array = array(5,1,2,7,10); // removing the 1 and 5 after 2 since those numbers are already a duplicate of the preceding numbers.
echo $new_array; // Output: 512710
You can do it through PHP's array_unique function.
This function traverses through your provided array and returns an array with unique values (repeating values will be removed).
Code to return desired string:
$array = array(5,1,2,1,5,7,10);
$new_array = array_unique($array);
echo implode('', $new_array);
Use array_unique() and implode():
$array = array(5,1,2,1,5,7,10);
$new_array = array_unique($array);
echo implode('', $new_array);
Output:
512710

Display array elements as normal CSV - PHP [duplicate]

This question already has answers here:
Closed 10 years ago.
Possible Duplicate:
How to create comma separated list from array in PHP?
I have an array as follows;
$array = array(1,2,3,4,5);
I want to print or echo this variable as 1,2,3,4,5. What is the simplest method for this?? I printed array[0] first and then skipped first value and used foreach function to echo all remaining ",".$value.
Try following
echo implode(",", $array);
You can use the implode function.
In the example you showed, it'd be written like this:
implode(',', $array);
$array = array(1,2,3,4,5);
$result = implode(',', $array);
echo $result;

Categories