This question already has answers here:
How to convert a string to a multidimensional recursive array in PHP? [duplicate]
(1 answer)
how to merge multiple url/path into multidimensional array?
(1 answer)
Closed last month.
How to handle if I want to create an array like in following example:
$value="test";
$array=[];
$path=[2,17,513];
Now, I want to build an array based on the available data which should look like:
$array['2']['17']['513']='test';
Anyone any idea?
I tried array_shift to prepare but I miss the point where the single array data will be transformed to an array key....
$value="test";
$array=[];
$path = [2, 17, 513];
[$a, $b, $c] = explode(",", $path);
$array[$a][$b][$c] = $value;
print_r($array[$a][$b][$c]); //returns 'test'
Related
This question already has answers here:
Sort array not working
(2 answers)
Closed 10 months ago.
I'm trying to sort an array numerically. Here's my code
<?php
$data = '9#Saul,7#Jesse,1#Skyler,6#Walter';
$exp = explode(",",$data);
$expsort = sort($exp);
print_r($expsort);
?>
But it is not working. The output is showing only "1".
You are assigning the value of the sort function -which sorts the argument array itself, and it always returns true and thus you got 1 as a result.
So if you print your original exploded array, it will be sorted. Please note, sort overrides your original array
$data = '9#Saul,7#Jesse,1#Skyler,6#Walter';
$exp = explode(",",$data);
sort($exp);
print_r($exp);
This question already has answers here:
Implode a column of values from a two dimensional array [duplicate]
(3 answers)
Closed 7 months ago.
I have a multi-dimensional array that takes the form:
array = [value1, some number],[value2, some number]...
I need to loop through the array and echo the value followed by a tokenizer so the final output looks like:
value1!##$value2!##$
I know that I have to concatenate the return value with ."!##$" but I do not know how to loop through the array. Can anyone offer some help.
My array is being made from a MySQL Query as follows:
while($row = $results -> fetch_assoc()) {
$return_array[] = array(
$row['uid'],($row['column1] - $row['column2']));
}
Then I am perfoming a usort on the array
To be simple enough, you can use implode and array_column:
$array = [['value1', 123], ['value2', 234]];
echo implode('!##$', array_column($array, 0)) . '!##$';
This gives:
value1!##$value2!##$
Explanation:
implode - Joins array values using some specified value, here !##$
array_column - implode accepts one dimensional array, also you want only the first index of the array to be joined, so creating an array with just first index.
This question already has answers here:
Applying a function for each value of an array
(5 answers)
Closed 6 years ago.
Is there util that parses array content without the need of iterating it and parsing each value?
input: ['2','3','7']
output: [2, 3, 7]
This obviously iterates internally, but you don't have to code an iterator:
$output = array_map('intval', $input);
Maps every value in $input to the intval() function and returns the result. For things that cannot be converted into an integer you'll get 0 or for objects, a notice and that value will not be returned.
I can't tell if you want to remove 0 values or not from your comment, but if so:
$output = array_filter(array_map('intval', $input));
You can use array_map
array = ["2","3","4"];
$intArray = array_map('intval', $array);
This question already has answers here:
array_unique vs array_flip
(4 answers)
array_unique and then renumbering keys [duplicate]
(1 answer)
PHP arrays: delete duplicates and reorder keys [duplicate]
(2 answers)
array_unique does not re-sort the array
(2 answers)
Removing undefined array indexes after calling array_unique
(2 answers)
Closed 8 months ago.
I have two arrays:
$DocumentID = array(document-1, document-2, document-3, document-4,
document-5, document-4, document-3, document-2);
$UniqueDocumentID = array();
I want to push the unique objects inside of $documentid array to $UniqueDocumentID array.
I can't use array_unique() as it copies the key of its predecessor array and I want sequential keys inside the $UniqueDocumentID array.
You could foreach() through $DocumentID and check for the current value in $UniqueDocumentID with in_array() and if not present add it. Or use the proper tool:
$UniqueDocumentID = array_unique($DocumentID);
To your comment about wanting sequential keys:
$UniqueDocumentID = array_values(array_unique($DocumentID));
The long way around:
$UniqueDocumentID = array();
foreach($DocumentID as $value) {
if(!in_array($value, $UniqueDocumentID)) {
$UniqueDocumentID[] = $value;
}
}
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];
}