how to change value data in array PHP? [duplicate] - php

This question already has answers here:
How to cast array elements to strings in PHP?
(7 answers)
Closed last year.
I have a data array like this:
$data = array(1,2,3);
how to change the array like this:
$data = array('1','2','3');
Tanks Before

You have to change type of all array values, just like this
foreach ($data as $key => $val) {
$data[$key] = (string) $val;
}
or
function convert2string($n)
{
return (string) $n;
}
$a = [1, 2, 3];
$data = array_map('convert2string', $a);

Related

Dividing array by comparing flag using array functions in php [duplicate]

This question already has answers here:
Split associative array by key
(6 answers)
Get the first N elements of an array?
(5 answers)
php - get numeric index of associative array
(7 answers)
Closed 2 years ago.
There is an array in php
$sampleArray = array('first' => 'first', 'second' => 'second', 'third' => 'third');
I need to splitting the array based on value $check, if the $check is 'second', then the array need to be split like this
$requiredArray = array('third'=>'third')
$nonReqArray = array('first'=>'first','second'=>'second')
if the $check is 'third' then the $requiredArray will be empty. Please suggest the solution to this?
Tried with this
$afterResult = array_filter($sampleArray, function($key) use (&$requiredArray, &$nonReqArray, &$check) {
if($key > $check){
$requiredArray[$key] = $key;
}else{
$nonReqArray[$key] = $key;
}
return true;
},
ARRAY_FILTER_USE_KEY);
Usually solved with for loops. But we can solve by array_filter array function. The main issue is indexing is not numerical, so comparing is not easy.
$cmp = false;
$afterResult = array_filter($sampleArray, function($key) use (&$requiredArray, &$nonReqArray, &$check, &$cmp) {
if($cmp){
$requiredArray[$key] = $key;
}else{
$nonReqArray[$key] = $key;
}
if($key == $check){
$cmp =true;
}
return true;
},
ARRAY_FILTER_USE_KEY);
This worked for me.

Merge objects only by the ID [duplicate]

This question already has answers here:
Merging and group two arrays containing objects based on one identifying column value
(4 answers)
Closed 5 months ago.
I have 2 arrays that I want to merge by the id of the objects that in the arrays (2 different objects):
$object1 = new User();
$object2 = new User();
$object3 = new AdminUser();
$object4 = new AdminUser();
$object1->id = "1234";
$object1->name = "testUser1";
$object2->id = "34553";
//the id is like user1 but the username is different
$object3->id = "1234";
$object3->name = "testUser2";
$object4->id = "44234";
$array1 = [$object1,$object2];
$array2 = [$object3,$object1,$object4];
my wanted result is :
[$object1,$object2,$object4];
i tried :array_unique(array_merge($array1,$array2), SORT_REGULAR);
and I also tried :
$result = array_merge( $array1, $array2 );
$result = array_map("unserialize", array_unique(array_map("serialize", $result)));
but it didn't work
`
You can use array_reduce after merging those 2 arrays:
$mergedObjects = array_merge($array1, $array2);
$result = array_reduce($mergedObjects, function ($carry, $user) {
if (!isset($carry[$user->id])) {
$carry[$user->id] = $user;
}
return $carry;
}, array());
array functions are only ment for arrays, they do not parse anything inside objects that are within arrays. What you could do is push the object ids as array keys:
$mainArray = array();
foreach($objects as $object){ //or do it for every object if you create them manually
array_merge($mainArray, array($object->id => $object);
}
If the keys ( ids ) are duplicate, the latest takes priority.

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];
}

How can you sort each element inside of an array alphabetically [duplicate]

This question already has answers here:
PHP: How to sort the characters in a string?
(5 answers)
Closed 8 years ago.
For example we have the following words: hey, hello, wrong
$unsorted = array("eyh", "lhleo", "nrwgo");
I know that I could use asort to sort the array alphabetically, but I don't want that.
I wish to take the elements of the array and sort those, so that it would become something like this:
$sorted = array("ehy", "ehllo", "gnorw"); // each word in the array sorted
hey sorted = ehy
hello sorted = ehllo
wrong sorted = gnorw
As far as I know, the function sort will only work for arrays, so if you attempt to sort a word using sort, it will produce an error. If I had to assume, I will probably need to use a foreach along with strlen and a for statement or something similar, but I am not sure.
Thanks in advance!
function sort_each($arr) {
foreach ($arr as &$string) {
$stringParts = str_split($string);
sort($stringParts);
$string = implode('', $stringParts);
}
return $arr;
}
$unsorted = array("eyh", "lhleo", "nrwgo");
$sorted = sort_each($unsorted);
print_r($sorted); // returns Array ( [0] => ehy [1] => ehllo [2] => gnorw )
$myArray = array("eyh", "lhleo", "nrwgo");
array_walk(
$myArray,
function (&$value) {
$value = str_split($value);
sort($value);
$value = implode($value);
}
);
print_r($myArray);
Try this
$unsorted = array("eyh", "lhleo", "nrwgo");
$sorted = array();
foreach ($unsorted as $value) {
$stringParts = str_split($value);
sort($stringParts);
$sortedString = implode('', $stringParts);
array_push($sorted, $sortedString);
}
print_r($sorted);

need to map two arrays to one array [duplicate]

This question already has answers here:
Closed 10 years ago.
Possible Duplicate:
php values of one array to key of another array
I have the sorted array
$A = array(
0=>EUR,
1=>GBP,
2=>USD
);
$B = array(
0=>'0.88',
1=>'0'
);
I want to map to be like this allways put 'EUR'=>'1':
$C = array(
'EUR'=>'1',
'GBP'=>'0.88',
'USD'=>'0'
);
Could anyone tell me please?
$C = array_combine($A, array_merge(array(1), $B));
$C = array();
foreach ($A as $key => $value)
{
$C[$value] = $B[$key];
}

Categories