I would like to build an Adjacency List from my list of sequence in a php. The thing is, that my list of sequince is in array and it looks like this:
$arr = array("1", "1.1", "1.2", "2", "2.1", "2.1.1", "2.1.2");
Now, I would like to transform it so that it would be in a Adjanency list model, like this:
$arr1 = array("0", "1", "1", "0", "4", "5", "5");
So, my $arr1 will be representing a 'parentId' in the table for a tree view(jsTree).
Can please somebody point me to the right directon, or where should I start looking for a solution.
Thank you.
You could do something like this:
for ($i = 0; $i < count($arr); $i++) {
$splitString = explode(',', $arr[i]); //split the string on the point
if (strlen($splitString) > 1) {
$arr1[i] = $splitString[1]; // take the part after the point
}
else {
$arr1[i] = "0"; // no part after the point, so default to 0
}
}
Related
This question already has answers here:
PHP - count specific array values
(10 answers)
Closed 1 year ago.
$cartItemsArray = ["", "1", "2", "3", "3", "4", "2", "3"];
for ($i = 0; $i < count($cartItemsArray); $i++) {
echo $cartItemsArray[$i] . "<br />";
}
Here $cartItemsArray is an array of some id's. I want to count the occurences of every element and save it in an array.
I want output like below:
Array = [1:"1", 2:"2", 3:"3", 4:"1"];
I want to save these array.
You can use an array like this.
$cartItemsArray = ["", "1", "2", "3", "3", "4", "2", "3"];
$return = [];
foreach ($cartItemsArray as $card) {
if (!isset($return[$card])) {
$return[$card] = 0;
}
$return[$card]++;
}
I have two arrays and need to join or merge them into one.
$array1 = array(
array("Account 1", "EUR", "100", "333"),
array("Account 2", "EUR", "200", "444")
);
$array2 = array(
array("Account 1", "EUR", "100", "111"),
array("Account 2", "EUR", "200", "222")
);
$array = array_merge($array1,$array2);
Then my outcome is:
[["Account 1","EUR","100","333"],["Account 2","EUR","200","444"],["Account 1","EUR","100","111"],["Account 2","EUR","200","222"]]
I would like to have like this:
[["Account 1","EUR","100","333","111"],["Account 2","EUR","200","444","222"]]
How to make it? Any advice would be appreciated.
It looks like the program should append the last element of array2[i] to array1[i]. You'll need a loop of course, and you may find PHP: end helpful. If that is not the intent of the program, then be sure to update the question with a more accurate problem statement or example.
This is how I resolved the problem:
for ($i = 0; $i < count($array1); $i++) {
$array1[$i][]=$array2[$i][3];
}
I have a search input in my wordpress site, which I want to display different placeholdes each time the page been refreshed. So I will have an array of the different values, the function that will loop randomly through the array, and I think my problem is with the result. I'm using a plugin for the search input, so to define for example that the placeholder would say 'hello', I need to do:
$item['textinput']= "hello";
So I need the result to be:
$item['textinput']= $placeholders;
I am adding the whole code:
$placeholders = array("yo i hio", "and him", "nayo jones");
function randominputs($input){
$i=1;
while($i<=20){
$randNum = rand(1,100);
if($input==$i){
$item['textinput']= $placeholders;
}
$i++;
}
}
Thanks
Use array_rand:
$placeholder = $placeholders[array_rand($placeholders)];
I would suggest adding the sizeof() call to your code.
For example if I wanted to call a random result from an array you could try...
$arr = array("1", "2", "3", "4", "5", "6", "7");
$len = sizeof($arr) - 1; // account for the 0 position
$rand = rand(0,$len);
echo $arr[$rand];
You can also use like:
$placeholders = array("yo i hio", "and him", "nayo jones");
$randomPlaceholder = array_rand($placeholders);
$item["textinput"] = $randomPlaceholder[0];
The title of this questions sounds a bit confusing, but I don't know how I should call it, so I will explain it further:
For example I got the following array:
[0]=> "id" [1]=> "5" [2]=> "value" [3]=> "8"
And so on, this array could be endless, but the number of content is even.
Now I want to have this array converted into an associative array where on pair is index => value. Like this:
[id] = "5" [value] = "8"
I thought about, that I foreach the array twice: first I set the indexes if count is odd and second I reset index and set the value if count is even. But there must be a better approach to do this.
Thanks for you help.
$array = array("id", "5", "value", "8");
$new_array = array();
for ($i = 1; $i < count($array); $i+=2) {
$new_array[$array[$i - 1]] = $array[$i];
}
The following code should serve your purpose:
$arr = array("id", "5", "value", "8");
$size = sizeof($arr);
$_arr = array();
for($i = 0; $i < $size; $i+=2){
$_arr[$arr[$i]] = $arr[$i+1];
}
DEMO
Why not a multidimensional array?
$arr = array(
array("id", "5"),
array("value", "8")
);
I need to find the minimum and maximum in a multidimensional array in PHP, I have what I thought would work below but it keeps giving me a parse error, this is homework and I am not asking anyone to do it for me but I am a beginner and any help would be appreciated.
<?php
/* 2 dimensional array in PHP - strictly an array of arrays */
$multable[] = array("11", "12", "15", "22", "41", "42");
$multable[] = array("6", "7", "16", "17", "22", "23");
$multable[] = array("1", "15", "16", "20", "22", "3");
<table>
<?php
/* display a table from a 2D array */
for ($j=0;$j<3;$j++) {
print "<tr>";
for ($k=0;$k<6;$k++) {
echo "<td>",$multable[$j][$k],"</td>";
}
print "</tr>";
$max_value = 0;
foreach ($multable as $myMax) {
if ($max_value<$myMax) {
$max_value = $myMax;
}
}
echo $max_value;
?>
</table>
There is also a one-liner for that:
$max = max( array_map("max", $multable) );
use max() and min() functions of php.
Max:
<?php
$multable = array();
$multable[] = array("11", "12", "15", "22", "41", "42");
$multable[] = array("6", "7", "16", "17", "22", "23");
$multable[] = array("1", "15", "16", "20", "22", "3");
$max = -99999999;
foreach($multable as $sub){
$tempMax = max($sub);
if($tempMax > $max){
$max = $tempMax;
}
}
echo $max;
?>
You can figure out min :)
Your foreach iteration only does one dimension - each $myMax is one of your six element lists and not an individual scalar value. That's why your comparison doesn't work and the conditional is never true, you are trying to compare a scalar with an array. What you call $myMax would more appropriately be called $currentRow
This is ok because PHP has some functions to find the min and max of an array
http://us.php.net/manual/en/function.min.php
http://us.php.net/manual/en/function.max.php
$max_value = 0; $min_value = $multable[0][0];
foreach ($multable as $currentRow)
{
// COMPARE CURRENT ROW's MIN/MAX TO MIN/MAX_VALUE
// AND MAKE NEW ASSIGNMENT IF APPROPRIATE
}
Or hand this in and see what your teacher says:
function fComp ($f) {return function ($a,$b) use ($f) {return $f($a, $f($b));};}
$max = array_reduce($multable, fComp('max'), $multable[0][0]);
$min = array_reduce($multable, fComp('min'), $multable[0][0]);
echo "max: $max <br />";
echo "min: $min";
PS - in your earlier iterations to make the HTML table, it would be good form to lose the constants. Use count to get the length of the array instead - or better yet - use foreach like you do later on. (Even with foreach you would still need two of them nested, it doesn't iterate a 2-dimensional array element-by-element)
For Minimum value
echo min(array_map("min", $multable));
For Maximum Value
echo max(array_map("max", $multable));
$minArray = array();
foreach($arrayVal as $arrI=> $arrK)
{
if($arrK == min($arrayVal ) )
{
array_push($minArray , $arrayVal );
}
}
print_r($minArray);
Here you go :)
I do not recommend calling min() or max() on each subarray, then calling the function again on the reduced array. This is making too many calls and won't be most efficient.
Instead, flatten the indexed array just once with a spread&merge technique, then call min() or max() just once on the flattened array.
Code: (Demo)
$flat = array_merge(...$multable);
printf(
'Min: %d, Max: %d',
min($flat),
max($flat)
);
Output:
Min: 1, Max: 42
If you only need one or the other outcome, then don't bother with the temporary variable.
echo min(array_merge(...$multable));
Or
echo max(array_merge(...$multable));