int twodim[5][8];
int threedim[3][5][8];
How to create fixed length multi dimensional array in php as we create in c++ ?
To create fixed array you should use SplFixedArray from Standard PHP Library extension:
The SplFixedArray class provides the main functionalities of array. The main differences between a SplFixedArray and a normal PHP array is that the SplFixedArray is of fixed length and allows only integers within the range as indexes. The advantage is that it allows a faster array implementation.
It's not very PHP-like, but you can use this function (based on Grzegorz' SplFixedArray hint) to generate fixed sized arrays of any dimension.
function createFixedArray() {
$args = func_get_arg(0);
$array = new SplFixedArray($args[0]);
if (isset($args[1])) {
$newArgs = array_splice($args, 1);
for ($i=0; $i<$args[0]; $i++) {
$array[$i] = createFixedArray($newArgs);
}
}
return $array;
}
Example use:
$fixedArray = createFixedArray(array(2, 2, 2));
$fixedArray[0][0][0] = 0;
$fixedArray[0][0][1] = 1;
$fixedArray[0][1][0] = 1;
$fixedArray[0][1][1] = 0;
$fixedArray[1][0][0] = 1;
$fixedArray[1][0][1] = 0;
$fixedArray[1][1][0] = 0;
$fixedArray[1][1][1] = 1;
var_dump($fixedArray);
Generates
object(SplFixedArray)#1 (2) {
[0]=>
object(SplFixedArray)#2 (2) {
[0]=>
object(SplFixedArray)#3 (2) {
[0]=>
int(0)
[1]=>
int(1)
}
[1]=>
object(SplFixedArray)#4 (2) {
[0]=>
int(1)
[1]=>
int(0)
}
}
[1]=>
object(SplFixedArray)#5 (2) {
[0]=>
object(SplFixedArray)#6 (2) {
[0]=>
int(1)
[1]=>
int(0)
}
[1]=>
object(SplFixedArray)#7 (2) {
[0]=>
int(0)
[1]=>
int(1)
}
}
}
Related
I checked this question and answers:
How to group a multidimensional array by a particular subarray value?
He wanted to group results by 'level'. But how would you do it to group it by 'level' first and then by 'type'?
Its pretty straight forward. Loop through $items array. Get each item's level and type and if they are not set yet, initialize them with an empty array. Then just push the "cust" value into the array.
I have given the code below.
I am assuming "$items" is an array which contains the input.
$g = [];
foreach($items as $k => $v) {
$l = $v["level"];
$t = $v["type"];
$c = $v["cust"];
if(!isset($g[$l])) {
$g[$l] = [];
}
if(!isset($g[$l][$t])) {
$g[$l][$t] = [];
}
$g[$l][$t][] = [
"cust" => $c
];
}
var_dump($g);
The output of this code would be like below:
array(3) {
[1]=>
array(1) {
["standard"]=>
array(2) {
[0]=>
array(1) {
["cust"]=>
string(6) "XT8900"
}
[1]=>
array(1) {
["cust"]=>
string(6) "XT8944"
}
}
}
[3]=>
array(1) {
["premier"]=>
array(2) {
[0]=>
array(1) {
["cust"]=>
string(6) "XT8922"
}
[1]=>
array(1) {
["cust"]=>
string(6) "XT8816"
}
}
}
[7]=>
array(1) {
["standard"]=>
array(1) {
[0]=>
array(1) {
["cust"]=>
string(6) "XT7434"
}
}
}
}
[P.S.]: You can also use sort to solve this problem easily. That's another way of solving this problem.
I have a variable list of arrays, the list depend of user input. each time the program is called a new set of single arrays is generated.
I been searching for solutions but apparently all solution refer to array of arrays.
Originally the data are from database using:
for ($i = 0; $runrwos= mysql_fetch_assoc($run); $i++) {}
after the loop data are inserted in an array:
$data[$i] = array();
Using a foreach () {} function data are manipulated with mathematical operations to get the desired outcome.
The result will output two string of data,
$A is a numeric data
$B has alphabetic value
$explodA = explode(" ", $A);
$explodB = explode(" ", $B);
Then I combine the result
$new = array_combine($explodA, $explodB);
when I check result I with
var_dump($new);
I get the following result:
array(1) { [1204]=> string(8) "Home2017" }
array(1) { [1183]=> string(8) "Home2018" }
array(1) { [1204]=> string(4) "Stan" }
array(1) { [1204]=> string(7) "Jun2017" }
array(1) { [1173]=> string(9) "APRIL2017" }
.......................................
array(1) { [953]=> string(7) "UNE2018" }
array(1) { [1171]=> string(6) "MAY201" }
I need to sort this data as follow:
array(1) { [953]=> string(7) "UNE2018" }
array(1) { [1171]=> string(6) "MAY201" }
array(1) { [1173]=> string(9) "APRIL2017" }
array(1) { [1183]=> string(8) "Home2018" }
array(1) { [1204]=> string(8) "Home2017" }
array(1) { [1204]=> string(4) "Stan" }
array(1) { [1204]=> string(7) "Jun2017" }
Can someone direct in the right direction?
I guess the solution is simple but I cannot resolve
I have a array and I need unique contents. How can I get rid of duplicates in this $tmparray:
array(176) {
[0]=>
array(2) {
[0]=>
string(22) "/ads/67006/didi"
[1]=>
string(73) "/Content/Pictures/Scaled/7b5c69572fdb1569ced695c278072ae0.jpg"
}
[1]=>
array(2) {
[0]=>
string(22) "/ads/67006/didi"
[1]=>
string(73) "/Content/Pictures/Scaled/7b5c69572fdb1569ced695c278072ae0.jpg"
}
[2]=>
array(2) {
[0]=>
string(22) "/ads/67006/didi"
[1]=>
string(73) "/Content/Pictures/Scaled/7b5c69572fdb1569ced695c278072ae0.jpg"
}
[3]=>
array(2) {
[0]=>
string(19) "/ads/67010/sylvesta"
[1]=>
string(73) "/Content/Pictures/Scaled/83ebba04b8eabd0458cc6dbbb85581da.jpg"
}
[4]=>
array(2) {
[0]=>
string(19) "/ads/67010/sylvesta"
[1]=>
string(73) "/Content/Pictures/Scaled/83ebba04b8eabd0458cc6dbbb85581da.jpg"
}
[5]=>
array(2) {
[0]=>
string(19) "/ads/67010/sylvesta"
[1]=>
string(73) "/Content/Pictures/Scaled/83ebba04b8eabd0458cc6dbbb85581da.jpg"
}
But I want it to look like: (Only unique contents.)
array(176) {
[0]=>
array(2) {
[0]=>
string(22) "/ads/67006/didi"
[1]=>
string(73) "/Content/Pictures/Scaled/7b5c69572fdb1569ced695c278072ae0.jpg"
}
[1]=>
array(2) {
[0]=>
string(19) "/ads/67010/sylvesta"
[1]=>
string(73) "/Content/Pictures/Scaled/83ebba04b8eabd0458cc6dbbb85581da.jpg"
}
}
I have tried with:
array_unique($tmparray);
array_unique can't do what I want. Anyone have a idea how to solve this?
your question seems duplicate of this
How to remove duplicate values from a multi-dimensional array in PHP
i guess array_map will solve your problem
$input = array_map("unserialize", array_unique(array_map("serialize", $input)));
You can use this code:
$newarray= array();
foreach ($tmparray as $value) {
if (!in_array($value,$newarray)) {
$newarray[ ] = $value;
}
}
var_dump($newarray);
I assume that by duplicate you mean two items where either elements are matching, since you are mapping ads to their pictures, therefore:
$target = array();
$elementCount = count($tmparray);
$newElementCount = 0;
for ($i = 0; $i < $elementCount; $i++) {
$found = false;
for ($j = 0; (!$found) && (j < $newElementCount); j++) {
$found = $target[$j][0] === $tmparray[$i][0] || $target[$j][1] === $tmparray[$i][1];
}
if (!$found) {
$target[$newElementCount++]=$tmparray[$i];
}
}
PHP array_unique is used only for single dimensional arrays, for multidimensional you can do this by serializing multidimensional array, like below
<?php
$dataArray = [
0=>["/ads/67006/didi","/Content/Pictures/Scaled/7b5c69572fdb1569ced695c278072ae0.jpg"],
1=>["/ads/67010/sylvesta","/Content/Pictures/Scaled/83ebba04b8eabd0458cc6dbbb85581da.jpg"],
2=>["/ads/67006/didi","/Content/Pictures/Scaled/7b5c69572fdb1569ced695c278072ae0.jpg"],
3=>["/ads/67010/sylvesta","/Content/Pictures/Scaled/83ebba04b8eabd0458cc6dbbb85581da.jpg"],
];
$serilaized = [];
$newArr = [];
### serilaize each node of multi array and create a new single dimention array..
foreach($dataArray as $val){
$serilaized[] = serialize($val);
}
### now perform array unique..
$serilaized_unique = array_unique($serilaized);
## unserialize each node of uniqur array..
foreach($serilaized_unique as $val){
$newArr[] = unserialize($val);
}
echo "<pre>";print_r($newArr);
?>
This will give you:
Array
(
[0] => Array
(
[0] => /ads/67006/didi
[1] => /Content/Pictures/Scaled/7b5c69572fdb1569ced695c278072ae0.jpg
)
[1] => Array
(
[0] => /ads/67010/sylvesta
[1] => /Content/Pictures/Scaled/83ebba04b8eabd0458cc6dbbb85581da.jpg
)
)
In short you can perform this in single line code with array_map
$dataArray = array_map('unserialize', array_unique(array_map('serialize', $dataArray)));
I got it working with this line:
$tmparray = array_unique($tmparray, SORT_REGULAR);
I have seen the following: array_merge() How can I add a 'range' of an array name and the answers don't work for me.
I have an array that I am looping through in order to slice and convert certain currency strings to float numbers. I then have to array_merge them back together in order to work with the array and have been dynamically naming them so that I don't overwrite the previous array_merge. After doing so, I then need to combine all of the dynamically named arrays into one array.
Initially I had the following code, which worked great when I only had 3 nested arrays in the $order['product'] array. However, this number varies, and the code needs to do so as well.
$nr = 1;
foreach ($order['product'] as $product) {
$product_total = array_slice($product, 1);
array_walk($product_total, "convertCurrencyStringtoNumber");
${"final_product" . $nr} = array_merge($product, $product_total);
$nr++;
};
$arrays = array($final_product1, $final_product2, $final_product3);
var_dump($arrays);
This results in the following array:
array(3) {
[0]=> array(2) {
["source_code"]=> string(10) "408000-025"
["total"]=> float(18) }
[1]=> array(2) {
["source_code"]=> string(10) "408000-025"
["total"]=> float(17) }
[2]=> array(2) {
["source_code"]=> string(10) "408000-025"
["total"]=> float(2.75) } }
How do I implement a varied number of dynamically named arrays in the line:
$arrays = array($final_product1, $final_product2, $final_product3);
I attempted the following, but the array is nested incorrectly. Feel free to fix this code or come up with a better solution.
$nr = 1;
$i = 1;
foreach ($order['product'] as $product) {
$product_total = array_slice($product, 1);
array_walk($product_total, "convertCurrencyStringtoNumber");
${"final_product" . $nr} = array_merge($product, $product_total);
if ($nr > 0) {
$arrays = $final_product1;
for ($i = 2; $i <= $nr; $i++) {
$arrays = array_merge($arrays, ${"final_product" . $nr});
}
} else {
echo "There are no products in this order";
}
$nr++;
};
var_dump($arrays);
This results in the incorrectly nested array:
array(2) {
[0]=> array(2) {
[0]=> array(2) {
["source_code"]=> string(10) "408000-025"
["total"]=> float(18) }
[1]=> array(2) {
["source_code"]=> string(10) "408000-025"
["total"]=> float(17) } }
[1]=> array(2) {
["source_code"]=> string(10) "408000-025"
["total"]=> float(2.75) } }
Simply replace your dynamically single-named variables with an array:
$final_product = array();
foreach ($order['product'] as $product) {
$product_total = array_slice($product, 1);
array_walk($product_total, "convertCurrencyStringtoNumber");
$final_product[] = array_merge($product, $product_total);
};
var_dump($final_product);
Unless I'm missing something here... this should be as easy and simple as:
$final_array=[];
foreach ($order['product'] as $product) {
$final_array[]['total'] = (float) $product['whatever value 1 is...'];
$final_array[]['source_code'] = $product['source_code'];
}
var_dump($final_array);
If you need to apply convertCurrencyStringtoNumberbecause it does something weird to the variable then changethe seccond line to:
$final_array[]['total'] = convertCurrencyStringtoNumber(array_slice($product, 1));
This is the problem:
I have the following array (from $wpdb->get_results()):
array(6) {
[0]=> array(1) {
[0]=> string(7) "1102006"
}
[1]=> array(1) {
[0]=> string(7) "1102006"
}
[2]=> array(1) {
[0]=> string(7) "8092007"
}
[3]=> array(1) {
[0]=> string(8) "23062012"
}
[4]=> array(1) {
[0]=> string(8) "29072000"
}
[5]=> array(1) {
[0]=> string(8) "30082008"
}
}
And I would like to find the lowest integer from 10,000 on that is NOT in this array. In this case the answer would be 10,000 as 10,000 is not in the array.
Thanks
This is how I interpreted your question.
Starting from 10000, find the first available number that is NOT within your data array.
<?php
$data = array(
array('1102006'),
array('1102006'),
array('8092007'),
array('23062012'),
array('29072000'),
array('30082008')
);
// flatten the array to a single dimension
function flatten(&$v) { $v = $v[0]; }
array_walk($data, 'flatten');
// minimum number
$num = 10000;
// while a value has not been found
while (!isset($value))
{
// check if the current number is in our data (exclusion list)
if (array_search($num, $data) === false)
$value = $num;
// increment for our next search
$num++;
}
echo $value;
If you are just after the minimum value in that array, flatten the array in the previous answer and use min:
echo min($data);