PHP Array within an Array - php

$row[]; // Declare array. PRETEND ITS AN ARRAY
$row2[]; // Declare another
$row3[]; // Declare one more
$rowarray[];
$rowarray[0] = $row
$rowarray[1] = $row2
$rowarray[2] = $row3 // Store array in an array
My Questions:
1. Is this valid or even useful?
2. If I do this, how do I access $row[0] $row[1] etc.

The concept is valid, but the syntax is not -- arrays are not explicitly declared like that in PHP. A correct way to initialize this would be something like:
$row1 = array(1, 2, 3);
$row2 = array(4, 5, 6);
$row3 = array(7, 8, 9);
$rowarray = array($row1, $row2, $row3);
Or, equivalently and more succinctly:
$rowarray = array(
array(1, 2, 3),
array(4, 5, 6),
array(7, 8, 9)
);
$rowarray[1][2]. Indexes are in order, so, given the example data I used, this would be 6 (element 2 of the array which is element 1 of $rowarray).

What you're looking for is a Multi-dimensional array.
For example:
$a1 = array(
array(1, 2, 3, 4),
array(1, 2, 3, 4)
);

Related

Inserting multiple values from an array using array_unshift() method

array_unshift is using for inserting one or many values at the beginning of an array. Now I have an array-
$data = [1, 3, 4];
Another array is needed to insert at the beginning of the $data array.
$values = [5, 6];
Here I want to insert the values 5, 6 at the beginning of the $data array, and the resulting $data would be-
$data = [5, 6, 1, 3, 4];
Note: I know there is a way like array_unshift($data, ...$values); but this is working from php7.3.x AFAIK. I need to do it below php7.3.
Is there any way to do this without looping the $values array in the reverse order?
Function array_merge exists since PHP 4:
<?php
$data = [1, 3, 4];
$values = [5, 6];
$data = array_merge($values, $data);
print_r($data);
Live PHP sandbox
You should use array_merge instead of array_unshift.
$data = [1, 3, 4];
$values = [5, 6];
$result = array_merge($values, $data); // the sequence of the array inside array_merge will decide which array should be merged at the beginning.

How to remap an array to save it to couples

I need to remap an array from
array(1,2,3,4,5,6);
to output in couples like this:
array(1,2)
array(3,4)
array(5,6)
which I can do with foreach, but is the an built in php function that does this for you?
Use array_chunk:
<?php
$in = array(1, 2, 3, 4, 5, 6);
$out = array_chunk($in, 2);

PHP merge 2 arrays with different number of elements, $keys from one, $values from another

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);

PHP array_intersect() till the first match

I have 2 arrays to compare and find if there is at least a single value in common.
This works just fine:
$arr1 = array(1, 2, 3, 4, 5);
$arr2 = array(2, 3, 4, 5, 6);
if (array_intersect($arr1, $arr2)) {
// good, at least one match found
}
However, the question is performance. It doesn't make sense to continue looping thru the arrays after the first match was found. Is there a native PHP function or a useful snippet to achieve this?
Will a combination of foreach() and in_array() do the trick?
How about this?
foreach ($arr1 as $key => $val) {
if (in_array($val, $arr2)){
// do something, maybe return so you wouldn't need break
break;
}
}
Just compare the first value?
$arr1 = array(1, 2, 3, 4, 5);
$arr2 = array(2, 3, 4, 5, 6);
if (array_intersect($arr1, $arr2)[0]) {
// good, at least one match found
}

dynamically build and populate table with php array

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

Categories