I am extracting data from an xls document which returns as an array. The array keys however is not numeric, but resembles the cell column alphabetical digit. So effectively I have an array like this:
// Example array:
$array = array(
[0] => array(
"A" => "Name",
"B" => "Surname",
"C" => "",
"D" => "Telephone"
),
[1] => array(
"A" => "Name",
"B" => "Surname",
"C" => "",
"D" => "Telephone"
),
[2] => array(
"A" => "",
"B" => "",
"C" => "",
"D" => ""
)
);
Now the problem with the returned array is it does not filter the array at all, and I may have rows and rows of empty values in the rows.
I need a way to either clean up the array completely so remove empty rows and digits, but this may give me some issues with array consistancy later.
What I am looking for is a function which can chop rows in an array by a alphebitical digit. So effectively something like array_chop($array","C"); which would then effectively unset all the remainder rows key and value pairs from D onwards.
Is this possible? Is there a php function already I can use? I am sorry that I cannot provide sample code but I have no idea where to start. The array is substancially large, so I am looking for a memory efficient solution.
Thank you in advance!
If you want to copy only a small first portion of the column, this must be efficient:
<?php
$array = array(
0 => array(
"A" => "Name",
"B" => "Surname",
"C" => "",
"D" => "Telephone"
),
1 => array(
"A" => "Name",
"B" => "Surname",
"C" => "",
"D" => "Telephone"
),
2 => array(
"A" => "",
"B" => "",
"C" => "",
"D" => ""
)
);
$last_column = 1;
$n = count($array);
for ($i = 0; $i < $n; ++$i)
{
$copy = array();
$j = 0;
foreach($array[$i] as $key => $value)
{
if ($j <= $last_column)
{
$copy[$key] = $value;
++$j;
}
else
{
$array[$i] = $copy;
break;
}
}
}
var_dump($array);
This leaves only name and surname in the matrix:
array(3) {
[0]=>
array(2) {
["A"]=>
string(4) "Name"
["B"]=>
string(7) "Surname"
}
[1]=>
array(2) {
["A"]=>
string(4) "Name"
["B"]=>
string(7) "Surname"
}
[2]=>
array(2) {
["A"]=>
string(0) ""
["B"]=>
string(0) ""
}
}
Related
I want to get all with the same id_attribute_group and show them but the arrays with the same id_attribute_group are spread apart. I would like tho loop through them multiple times.
array(8) {
["id_attribute"] => string(1)
"1" ["id_attribute_group"] => string(1)
"1" ["attribute"] => string(1)
"S" ["group"] => string(4)
"Size" ["reference"] => string(6)
"demo_1"
}
array(8) {
["id_attribute"] => string(2)
"11" ["id_attribute_group"] => string(1)
"2" ["attribute"] => string(5)
"Black" ["group"] => string(5)
"Color" ["reference"] => string(6)
"demo_1"
}
array(8) {
["id_attribute"] => string(1)
"2" ["id_attribute_group"] => string(1)
"1" ["attribute"] => string(1)
"M" ["group"] => string(4)
"Size" ["reference"] => string(6)
"demo_1"
}
so it should loop once and get all with same number and assign them,loop again and get all with the second one and so on.
Without knowing more about your use case it's hard to tell for certain. However, it seems to me that a simple sorting algorithm (which effectively groups the array by id_attribute_group) followed by a loop to output is all you need:
Code
// Input array
$array = [
[
"id_attribute" => "1",
"id_attribute_group" => "1",
"attribute" => "S",
"group" => "Size",
"reference" => "demo_1",
],
[
"id_attribute" => "11",
"id_attribute_group" => "2",
"attribute" => "Black",
"group" => "Color",
"reference" => "demo_1",
],
[
"id_attribute" => "2",
"id_attribute_group" => "1",
"attribute" => "M",
"group" => "Size",
"reference" => "demo_1",
],
];
// Sort with reference to id_attribute_group
uasort($array, function($a,$b){
return $a["id_attribute_group"] <=> $b["id_attribute_group"];
});
// Output data
foreach($array as $item){
echo "{$item["group"]} {$item["attribute"]}\n";
}
Output
Size S
Size M
Color Black
Notes
Here the uasort sorts by the value id_attribute_group in each sub array.
Using uasort we maintain the original keys so you can always resort etc.
If you want to do something different with each group (e.g. separate them out) then you can update the code to something like:
Additional Code
uasort($array, function($a,$b){
return $a["id_attribute_group"] <=> $b["id_attribute_group"];
});
$last = false;
foreach($array as $item){
if($last && $last != $item["id_attribute_group"]){
echo PHP_EOL;
}
echo "{$item["group"]} {$item["attribute"]}\n";
$last = $item["id_attribute_group"];
}
Output
Size S
Size M
Color Black
Updated Solution
It's probably easiest to reformat your array first and then loop over the details you need:
$transformArray = [];
foreach($array as $item){
$transformArray[$item["id_attribute_group"]][] = $item;
}
foreach($transformArray[2] as $color){
echo "{$color["attribute"]} ";
foreach($transformArray[1] as $size){
echo "{$size["attribute"]} ";
}
echo PHP_EOL;
}
// Output: Black S M
I have two multidimensional arrays of the same structure.
Like this:
array(2) {
[0] =>
array(9) {
'id' =>
string(5) "44994"
'ersatzteil_id' =>
string(3) "120"
'lang' =>
string(6) "name2_tag2"
'title' =>
string(12) "Seitentüren"
'alias' =>
string(12) "seitentueren"
'content' =>
string(1610) "LOREM ISPUM BLALABLBL"
'on_main' =>
string(1) "0"
'disabled' =>
string(1) "1"
'short_text' =>
NULL
}
[1] =>
array(9) {
'id' =>
string(5) "44996"
'ersatzteil_id' =>
string(3) "122"
'lang' =>
string(6) "name1_tag1"
'title' =>
string(7) "Spoiler"
'alias' =>
string(7) "spoiler"
'content' =>
string(1513) "SOME OTHER RANDOM TEXT"
'on_main' =>
string(1) "0"
'disabled' =>
string(1) "0"
'short_text' =>
NULL
}
}
What I need to do is I need to compare first array with the second one.
I have to compare them by keys ersatzteil_id and content , and I find that they have same content I need to store element from first array in another new array, that wasn't existing before.
For example I need something like this, but more efficient:
if(array1[20]['ersatzteil_id'] == array2[145]['ersatzteil_id']
&& array1[20]['content'] == array2[145]['content']){
array3 = array1[20];
}
Try this code:-
$result = [];
foreach($array1 as $arr1){
foreach($array2 as $arr2){
if(($arr1['id'] == $arr2['id']) && ($arr1['ersatzteil_id'] == $arr2['ersatzteil_id'])){
$result[] = $arr1;
}
}
}
echo '<pre>'; print_r($result);
This is my array:
array(4) {
["1"]=>
array(3) {
[0]=>
string(2) "01"
[1]=>
string(2) "02"
[2]=>
string(2) "03"
}
["2"]=>
array(2) {
[0]=>
string(2) "01"
[1]=>
string(2) "02"
}
["3"]=>
array(1) {
[0]=>
string(2) "01"
}
["4"]=>
array(1) {
[0]=>
string(2) "01"
}
}
I want to print the lowest key, but only from the keys, that have less than 3 values.
echo min(array_keys($myarray));
gives me the result: 1
But key 1 already has 3 values, so the result I would need is 2. In the case every key has 3 values then print the next key (in this case would be 5)
I do not know how to do this. I am happy for every hint or advise.
This function iterate over your array and looks for all keys that has a value less then 3 values. It will return the first it founds if none is found the next key is return.
function getLowestKeyWithLessThan($yourArray, $number=3)
{
foreach ($yourArray as $key => $value) {
if (count($value) < $number)
return $key;
}
return count($yourArray) + 1;
}
If I run the following lines:
print "Answer " . getLowestKeyWithLessThan($yourArray);
print "\nAnswer " . getLowestKeyWithLessThan($AllKeysHasThreeElements);
This gives the answer:
Answer 2
Answer 5
Here is the data I used to test this:
$yourArray = array(
"1"=> array(
'0' => "01",
'1' => "02",
'2' => "03",
),
"2"=> array(
'0' => "01",
'1' => "02",
),
"3"=> array(
'0' => "01",
),
"4"=> array(
'0' => "01",
),
);
$threeValues = array(
'0' => "01",
'1' => "02",
'2' => "03",
);
$AllKeysHasThreeElements = array(
"1"=> $threeValues,
"2"=> $threeValues,
"3"=> $threeValues,
"4"=> $threeValues,
);
Of course the data could also been written like this:
$threeValues = array("01", "02", "03");
$yourArray = array($threeValues, array("01", "02"), array("01"), array("01"));
$AllKeysHasThreeElements = array($threeValues,$threeValues,$threeValues,$threeValues);
So lets say we have two arrays as such :
$x = array(
"id" => 12,
"name" => "Joe",
"surname" => "Murphy",
"age" => 52
);
$y = array(
"id" => 12,
"name" => "Joe",
"surname" => "Murphy",
"age" => 53
);
function key_compare_func($key1, $key2)
{
if ($key1 == $key2)
return 0;
else if ($key1 > $key2)
return 1;
else
return -1;
}
var_dump(array_intersect_ukey($x, $y, 'key_compare_func'));
This would return all matching elements from $x
array(4) {
["id"]=> int(12)
["name"]=> string(3) "Joe"
["surname"]=> string(6) "Murphy"
["age"]=> int(52)
}
I need to get only ["age"]=> int(52)
I looked at these, but none seem to offer this sort of functionality, unless I missed something :
array_udiff_assoc,
array_uintersect_assoc,
array_uintersect_uassoc,
array_udiffarray_uintersect,
array_udiff_uassoc
http://php.net/manual/en/function.array-diff-assoc.php
var_dump(array_diff_assoc($x, $y));
I got a PHP array which looks like that:
array(3) {
[0] => array(3) {
["Row"] => string(1) "1"
["Col"] => string(1) "1"
["Value"] => string(4) "lbl1"
}
[1] => array(3) {
["Row"] => string(1) "2"
["Col"] => string(1) "1"
["Value"] => string(4) "lbl2"
}
[2] => array(3) {
["Row"] => string(1) "3"
["Col"] => string(1) "1"
["Value"] => string(4) "lbl3"
}
}
I know that every pair "Row" and "Col" is different from the others. I can't change the way I'm creating the array.
Can I somehow access the array kinda like a hash table like this: arr[Row => 0][Col => 0] and get the value which is in Row 0 and Col 0?
If I understand your question well, you would need to reformat you array in order to access it like so: $array[1][1] to get Value of lbl1.
Try this where $array is your initial Array.
<?php
$return = array();
foreach($array as $value)
$return[$value['Row']][$value['Col']] = $value['Value'];
var_dump($return[1][1]); // outputs "lbl1"
Not sure why you need col and row as fields. Could you the key of the primary array as one? If it's a 2 dimensional array with just col/row, you should be able to create your array just use the keys properly.
$foo[1][1]="lbl1";
$foo[2][1]="lbl2";
$foo[3][1]="lbl3";
It's just as simple to access then, $foo[1][1] returns "lbl1"
You can use array_walk to get the value:
<?php
$a = array(
0 => array(
"Row" => "1",
"Col" => "1",
"Value" => "A",
),
1 => array(
"Row" => "2",
"Col" => "1",
"Value" => "B",
),
2 => array(
"Row" => "3",
"Col" => "1",
"Value" => "C",
)
);
$rowNeedle = 3;
$colNeedle = 1;
$result = null;
array_walk($a, function($row, $key) use($rowNeedle, $colNeedle, &$result) {
if($row['Row'] == $rowNeedle && $row['Col'] == $colNeedle) $result = $row['Value'];
});
echo $result;
The code will return the last match in case the array contains more than one row with the same Row and Col value (though, as you said, that is not your case).