I like to convert a single array into a multidimensional array. This is what I get have web scraping a page, except it is not the end result that I am looking for.
Change:
Rooms: Array (
[0] => name
[1] => value
[2] => size
[3] =>
[4] => name
[5] => value
[6] => size
[7] =>
[8] => name
[9] => value
[10] => size
[11] =>
[12] => name
[13] => value
[14] => size
[15] =>
)
Into:
Rooms: Array (
Room: Array (
[0] => name
[1] => value
[2] => size
),
Room: Array (
[0] => name
[1] => value
[2] => size
),
Room: Array (
[0] => name
[1] => value
[2] => size
)
)
First use array_filter() to get rid of the nodes:
$array = array_filter($array, function($x) { return trim($x) != ' '; });
// Or if your PHP is older than 5.3
$array = array_filter($array, create_function('$x', 'return trim($x) != " ";'));
Then use array_chunk() to split the array into chunks of 3:
$array = array_chunk($array, 3);
This of course assumes you will always and only get tuples containing name, value and size, in that order.
Related
I have a big array as $orderArr:
$orderArr = array("IZQ", "AOH", "VNP", "ICW", "IOQ", "BXP", "SHH", "EAY", "ZAF", "CUW");
which looks like
Array ( [0] => IZQ [1] => AOH [2] => VNP [3] => ICW [4] => IOQ [5] => BXP [6] => SHH [7] => EAY [8] => ZAF [9] => CUW )
and I have two small arrays as $subArr1 and $subArr2:
$subArr1 = array("VNP", "BXP", "ICW", "IZQ");
$subArr2 = array("ZAF", "IZQ", "AOH");
looks like
Array ( [0] => VNP [1] => BXP [2] => ICW [3] => IZQ )
Array ( [0] => ZAF [1] => IZQ [2] => AOH )
Both small arrays (sub array) own elements belong to the big array.
I want to sort two small arrays according to the order of big array, as following:
Array ( [0] => IZQ [1] => VNP [2] => ICW [3] => BXP )
Array ( [0] => IZQ [1] => AOH [2] => ZAF )
I am looking for the simplest codes to do it in php. Any suggestions are welcome.
Probably the simplest would be to compute the intersection and it will return in the order of the first array:
$subArr1 = array_intersect($orderArr, $subArr1);
That will return with the keys of the first array; if you want to reindex instead:
$subArr1 = array_values(array_intersect($orderArr, $subArr1));
You could use usort to sort based on array position:
usort($subArr1, function ($a, $b) use ($orderArr) {
return (array_search($a, $orderArr) < array_search($b, $orderArr)) ? -1 : 1;
});
var_dump($subArr1);
I have two+ Arrays which I set in a while loop.
Every array have a dependence to the other array by the index. And every array have duplications on the same index.
What I want to do is, to delete the duplications and after that to delete the empty indexes of each array, so that all arrays have the same positions like before but without duplications.
What I have tried is this:
$array1 = array_unique($array1);
$array2 = array_unique($array2);
print_r($array1);
echo "<br>";
print_r($array2);
echo "<br>";
sort($array1);
sort($array2);
print_r($artNrArray);
echo "<br>";
print_r($pnameArray);
echo "<br>";
This is the output:
Array (
[0] => 0100_64
[9] => 1999_13
[18] => 5999_12
[19] => 0204_22
[21] => 0241_75 )
Array (
[0] => intKab-4xAWG22-S-oE-oE-K3
[9] => Käbel
[18] => Kabel_test123
[19] => K-A21-V-IBIS-13-3-4-0-0-0m
[21] => K-CAN-17-2m )
Array (
[0] => 0100_64
[1] => 0204_22
[2] => 0241_75
[3] => 1999_13
[4] => 5999_12 )
Array (
[0] => K-A21-V-IBIS-13-3-4-0-0-0m
[1] => K-CAN-17-2m
[2] => Kabel_test123
[3] => Käbel
[4] => intKab-4xAWG22-S-oE-oE-K3 )
The problem if I sort the arrays is, that the dependence of the array contet is changed. What I want to have is this:
Array (
[0] => 0100_64
[1] => 1999_13
[2] => 5999_12
[3] => 0204_22
[4] => 0241_75 )
Array (
[0] => intKab-4xAWG22-S-oE-oE-K3
[1] => Käbel
[2] => Kabel_test123
[3] => K-A21-V-IBIS-13-3-4-0-0-0m
[4] => K-CAN-17-2m )
I have to use array_unique() to delete the duplications.
So how can I delete the array indexes with empty value and contraxt the array from 0 - 4?
Try the next:
$array1 = array_values(array_unique($array1));
$array2 = array_values(array_unique($array2));
How to get count of entries in an array below?
I tried Count arrays comma separated values and didnot get desired solution.Please suggest a method.
For below sample count is 11.
$sample= Array (
[0] => Array (
[0] => Array ( [attendance] => 2012SD71,2010SD94 )
[1] => Array ( [attendance] => 2003SD18,2003SD19 )
[2] => Array ( [attendance] => 2003SD23,2003SD28 ))
[1] =>
[2] =>
[3] =>
[4] =>
[5] =>
[6] =>
[7] =>
[8] =>
[9] =>
[10] =>
[11] =>
[12] =>
[13] => Array (
[0] => Array ( [attendance] => 2012SD81,2010SD84 )
[1] => Array ( [attendance] => 2003SD18,2003SD19,2004SD14 ) ) [14] =>
[15] =>
);
A simple recursive function should work that uses explode() and array_merge():
function recurse($array,&$new)
{
foreach($array as $key => $value) {
if(is_array($value)) {
recurse($value,$new);
}
else {
if(!empty($value)) {
$exp = array_filter(explode(',',$value));
$new = array_merge($new,$exp);
}
}
}
}
# Create a storage array
$new = array();
# Run the recursive function
recurse($sample,$new);
# Show count
print_r(count($new));
I want to build a array or multiple array by breaking the main array , and my array is like ,
Array
(
[0] => string1
[1] => 1
[2] => 2
[3] => 3
[4] => 66
[5] => 34
[6] => string1
[7] => aww
[8] => brr
[9] => string3
[10] => xas
)
So basically by the value 'string1' i want to make a new array or first array which has only those three values (1,2,3) and same for string2 and string3,So each array has its values(three).
Please help me to build this.
Note: those all string names will be static.
Thank you in advance.
Result should me like:
string1 array:
<pre>Array
(
[1] => 1
[2] => 2
[3] => 3
[4] => 66
[5] => 34
)
string2 array:
<pre>Array
(
[1] => aww
[2] => brr
)
string3 array:
<pre>Array
(
[1] => xas
)
This I think will get you what you want.
It does assume that the first entry in the old array will be a keyword!
$old = array('string1',1,2,3,66,34,'string2','aww','brr','string3','xas');
$new = array();
$keywords = array('string1', 'string2', 'string3');
$last_keyword = '';
foreach ($old as $o) {
if ( in_array($o, $keywords) ) {
$last_keyword = $o;
} else {
$new[$last_keyword][] = $o;
}
}
print_r($new);
It creates a new array like this
Array
(
[string1] => Array
(
[0] => 1
[1] => 2
[2] => 3
[3] => 66
[4] => 34
)
[string2] => Array
(
[0] => aww
[1] => brr
)
[string3] => Array
(
[0] => xas
)
)
However I still maintain that it would be better to go back to where the original array gets created and look to amend that process rather than write a fixup for it
I have the following array:
Array
(
[0] => BCD
[1] => ACE
[2] => AHP
[3] => BGH
[4] => ART
[5] => COT
[6] => ARG
[7] => BGT
)
I need to match all elements whose first letter is in the following array:
Array
(
[0] => B
[1] => A
)
to get:
Array
(
[0] => ACE
[1] => AHP
[2] => BGH
[3] => ART
[4] => ARG
[5] => BGT
)
Short of looping through the whole array, how do I do this in PHP? Is there a built-in PHP array function for this or a combination of so? The order does not matter for both keys and values of the resulting array. Thanks much.
You can use array_filter for these operations:
$array = array('CBD', 'NHN', 'NHP', 'WHC', 'NND', 'CQN', 'WST', 'WVT');
$whitelist = array('W', 'N');
$filtered = array_filter($array, function($val) use ($whitelist) {
// check if first letter is in the whitelist array
if (in_array($val{0}, $whitelist)) {
return $val;
}
return false;
});
Output:
Array
(
[1] => NHN
[2] => NHP
[3] => WHC
[4] => NND
[6] => WST
[7] => WVT
)