let's say I have these two arrays:
$array1 = array(1, 2, 3, 4, 5);
$array2 = array(6, 7, 8, 9, 10, 11, 12, 13, 14, 15);
As you can see, my arrays have different lengths. What I'm trying to do is to input these array values into an HTML table with the first column containing the values coming from $array1 and the second column containing the values coming from $array2. So, in this case right here, I should have a table of 10 rows (because $array2 contains 10 elements) and 2 columns (because I have 2 arrays). Also, I cannot know in advance which array is going to have more elements than the other (so, $array1 could be bigger than $array2, they could also have equal sizes). So, depending on which array has more elements, the number of rows in my table should adjust accordingly.
Any idea please?
Thank you
$array2 = array(6, 7, 8, 9, 10, 11, 12, 13, 14, 15);
$array1 = array(1, 2, 3, 4, 5);
$a=count($array1);
$b=count($array2);
echo "<table border=1><tr><th>Array1</th><th>Array2</th></tr>";
if($a > $b)
{
for($i=0;$i<$a;$i++)
{
echo "<tr><td>".$array1[$i]."</td>";
echo "<td>".$array2[$i]."</td></tr>";
}
}
if($b > $a)
{
for($i=0;$i<$b;$i++)
{
echo "<tr><td>".$array1[$i]."</td>";
echo "<td>".$array2[$i]."</td></tr>";
}
}
echo "</table>";
Try thinking of some thing like below
this will give you atleast an idea of how to iterate them.
$array = array($array1,$arry2);
for($i = 0; $i < $array.length; $i++)
{
$rows = $array[$i];
for($j=0; $j< $rows.length; $rows++){
}
}
i hope you can figure out the logic required from this . in case it doesnt work out post comment and we will walk through
Related
Can someone help me for my College Exam. I tried to search but Im totally newbie in php and Im still studying
Here's what i want, Can you give me some Idea or function so that I can arrange an array of positive integers to form the largest numerical string?
For Example:
$arrnew = [1, 2, 3, 4, 5, 6, 7, 8, 9, 10];
assert(getLargestNumStr($arrnew) == 98765432110, 'Basic test 9-8-7-6-5-4-3-2-1-10');
Hope you can help me.
This is a nice problem, this could be solved by this observation:
let's consider two elements in the array x and y, so assume that for the two numbers created by appending these two elements: xy > yx => in the final result, x will always be in front of y, otherwise, we could easily create a larger number by swapping the position of x and y in the result.
=> We could simply create a custom sort based on this observation, when compare two number x and y.
Give a try with below code if it solve your problem...
$array = [1, 2, 3, 4, 5, 6, 7, 8, 9, 10];
$text='';
foreach($array as $arr){
$text.=$arr;
}
$string = str_split($text, "1");
$new_text = implode(",", $string);
$output=explode(",",$new_text);
rsort($output);
$final = '';
foreach($output as $out){
$final.=$out;
}
echo $final;
Try sorting the array in descending order with something like arsort() and concatinate the Array-elements to a string. This should give you the highest possible number.
Try this
$arrnew = [1, 2, 3, 4, 5, 6, 7, 8, 9, 10];
arsort($arrnew);
$str = str_replace(',','',join(',',$arrnew));
$arr1 = str_split($str);
arsort($arr1);
print_r($arr1);
echo implode('',$arr1);
Demo
Closed. This question needs to be more focused. It is not currently accepting answers.
Want to improve this question? Update the question so it focuses on one problem only by editing this post.
Closed 5 years ago.
Improve this question
I've got many answer about this question using other language but i want an answer in php language. Any one help me please
This is my array look like
$array = [1, 2, 3, 4, 4, 1, 2, 5, 5, 11, 11];
Use array_count_values() like below:-
<?php
$array = [1, 2, 3, 4, 4, 1, 2, 5, 5, 11, 11];
$array_count_values = array_count_values($array);// get how many times a value appreas inside array
foreach($array_count_values as $key=>$val){ // now iterate over this newly created array
if($val ==1){ // if count is 1
echo $key. " in array come only one time.\n"; // this means value appears only one time inside array
}
}
Output:- https://eval.in/867433 OR https://eval.in/867434
If you want values in an array:-
<?php
$array = [1, 2, 3, 4, 4, 1, 2, 5, 5, 11, 11,13]; // increased one value to show you the output
$array_count_values = array_count_values($array);
$single_time_comming_values_array = [];
foreach($array_count_values as $key=>$val){
if($val ==1){
$single_time_comming_values_array[] = $key;
}
}
print_r($single_time_comming_values_array);
Output:- https://eval.in/867515
Here, you can use something like this-
<?php
function appearedOnce($arr)
{
$result = 0;
for($i=0; $i<sizeof($arr); $i++)
{
$result = $result ^ $arr[$i];
}
return $result;
}
$num = array(1, 2, 3, 4, 4, 1, 2, 5, 5, 11, 11);
print_r(appearedOnce($num)."\n")
?>
My initial response was to take a more pedestrian approach which works as you may note from this example. Then I chanced upon a related discussion.
Another approach involves sorting the array and then inspecting pairs of numbers for duplicates. The following code is a result of coupling the OP's array with my translation of the presumably C source code of Michael Martin into PHP, as follows:
<?php
$arr = [1, 2, 3, 4, 4, 1, 2, 5, 5, 11, 11];
sort($arr);
for($i = 0, $max = count($arr); $i < $max; $i++){
// is single number last element in array?
if($i == count($arr)-1)
$singleNum = $arr[$i];
// If adjacent elements the same, skip
if($i < count($arr)-1 && $arr[$i] == $arr[$i+1]){
$i++;
}
else
{
// found single number.
$singleNum = $arr[$i];
}
}
var_dump($singleNum);
See live code
I have five 2-D Arrays, (You can assume it like five spreadsheets), I want to take the intersection of the five (I want the common rows of all five in a new spreadsheet) but now I find out that one of the array is a NULL Array, (I find that instead of a spreadsheet, it's just a blank piece of paper), so now I want to ignore it and Intersect the rest of the Arrays... Please suggest a method of doing that...
P.S. - I know it is against the property of intersection because we all know that Intersection of anything with NULL is NULL, but that is not what I want...
P.S. - I also don't know how many Arrays can be empty, I just assumed it to be 1 as an Example, It can be 2, 3, 4 or Even 5... Yes, If it is 5, then returning NULL is perfect but not in any other case... Suppose if 4 Arrays are NULL, then It should return the 5th Array...
Language Used : PHP
You can use array_filter() and a custom implementation of in_array() to check if your array is null
Solution :
$array1 = array(6, 10, 11, 12);
$array2 = array(6, 741, 18, 9, 110, 11, 12);
$array3 = array(8, 10, 11, 20);
$array4 = null;
$array5 = array(9, 10, 11, 12);
function in_array_custom($item, $array)
{
if($array === null){
return true;
}
return in_array($item, $array);
}
function intersect($item)
{
global $array2;
global $array3;
global $array4;
global $array5;
return in_array_custom($item, $array2) && in_array_custom($item, $array3) && in_array_custom($item, $array4) && in_array_custom($item, $array5);
}
print_r(array_filter($array1, "intersect"));
Live example
I share the global solution :
<?php
$arrays = array(
array(6, 10, 11, 12),
array(6, 741, 18, 9, 110, 11, 12),
array(8, 10, 11, 20),
null,
array(9, 10, 11, 12)
);
function in_array_custom($item, $array)
{
if($array === null){
return true;
}
return in_array($item, $array);
}
function in_arrays($item, $arrays)
{
foreach($arrays as $array)
{
if(!in_array_custom($item, $array)) {
return false;
}
}
return true;
}
function intersect($item)
{
global $arrays;
return in_arrays($item, $arrays);
}
print_r(array_filter($arrays[0], "intersect"));
Live example
Here, there is one little issue, that If the first array ($array1) is null, then the code will not work, but that issue can be resolved by taking $array1 as a Union of all the SIX Arrays, (5 Arrays and 1 Union Array), And the Data is shifted to the next array, i.e $array2 now holds the data of $array1, $array3 = $array2 and so on...
P.S. - Union of Arrays can be done like this $array1 = $array2 + $array3 + $array4 + $array5 + $array6;
I want to merge 2 arrays together that have a different number of elements, using the keys from one and the values from another where/if the keys match. The array that contains the desired values may have less elements in it although I would like to retain the resulting empty keys from the original array. For example:
//Array that contains keys I would like to retain
$arr1 = array(1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12);
//Array that contains values I'd like to use
$arr2 = array(01=>123, 03=>123, 05=>123, 07=>123, 09=>123, 11=>123);
//The desired result with some valueless elements
$results = array(01=>123, 02, 03=>123, 04, 05=>123, 06, 07=>123, 08, 09=>123, 10, 11=>123, 12);
As you can see the results array retains 12 elements but only applies values to where the keys from the 2 arrays match.
I have tried $results = array_intersect_key($arr1 + $arr2, $arr2); among other PHP functions as well as:
for ($i=1; $i < count($arr1); $i++) {
if (isset($arr2[$i])) {
$arr3[] = $arr2[$i];
} else {
$arr3[] = $arr1[$i];
}
}
print_r($arr3);
No luck as yet.
Thanks in advance!
For arrays like this
$arr1 = array(1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12);
$arr2 = array(1=>123, 3=>123, 5=>123, 7=>123, 9=>123, 11=>123);
this should work.
foreach ($arr1 as $key) {
$results[$key] = isset($arr2[$key]) ? $arr2[$key] : null;
}
or using some other PHP functions:
$results = array_replace(array_fill_keys($arr1, null), $arr2);
//Array that contains keys I would like to retain
$arr1 = array(1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12);
//Array that contains values I'd like to use
$arr2 = array(1=>123, 3=>123, 5=>123, 7=>123, 9=>123, 11=>123);
$result = array_fill_keys(
$arr1,
null
);
array_walk(
$result,
function(&$value, $key) use($arr2) {
$value = (isset($arr2[$key])) ? $arr2[$key] : null;
}
);
var_dump($result);
First set your $arr1's values to false to retain just the keys:
$arr1 = array_fill_keys(array_keys($arr1), false); Or if you're generating $arr1 yourself, define it as such to start with: $arr1 = Array(false, false, false, false /* etc. */);
Now use the array union operator:
$result = $arr2 + $arr1;
The + operator returns the right-hand array appended to the left-hand array; for keys that exist in both arrays — from the docs: http://php.net/manual/en/language.operators.array.php)
Then if required sort the array by key: ksort($array);
i am trying to get the 1st 5 largest values from a numeric array...i have tried using the rsort() function to list the array values from highest to lowest but cant get a way to pick the 1st 5 from the result.
Using array_slice:
$a = array ( 1, 3, 4, 2, 4, 5, 10, 7, 6, 8, 0 );
rsort($a);
$largest = array_slice($a, 0, 5);
Check this out
$array_b4_change=array("knittl", "limón", "naranja", "plátano", "manzana" , "vikas" ,"wazzzy");
rsort($array_b4_change);
Use
array_slice($array_b4_change, 0, 5);
After you rsort just slice the array using array_slice:
$ouput = array_slice($array, 0, 5);
If you already have the array organized you could output it with
for ($i = 0; $i <= 4; $i++) {
print $array[$i];
}