I need to delete all elements where FacetValueCount is lower than 3.
How can I do this?
This is my array: Array name is $farben
array(8) {
[0]=>
array(2) {
["FacetValueName"]=>
string(4) "Blau"
["FacetValueCount"]=>
int(5)
}
[1]=>
array(2) {
["FacetValueName"]=>
string(7) "Schwarz"
["FacetValueCount"]=>
int(3)
}
[2]=>
array(2) {
["FacetValueName"]=>
string(4) "blue"
["FacetValueCount"]=>
int(2)
}
[3]=>
array(2) {
["FacetValueName"]=>
string(4) "Grau"
["FacetValueCount"]=>
int(1)
}
}
<?php
$farben = ARRAY();
$farben[] = array('FacetValueName'=>'Blau', 'FacetValueCount' => 5);
$farben[] = array('FacetValueName'=>'Schwarz', 'FacetValueCount' => 3);
$farben[] = array('FacetValueName'=>'blue', 'FacetValueCount' => 2);
$farben[] = array('FacetValueName'=>'Grau', 'FacetValueCount' => 1);
print '<pre>'; var_dump($farben); print '</pre>';
foreach ($farben AS $key => $row) {
if ($row['FacetValueCount'] < 3) { unset($farben[$key]); }
}
print '<pre>'; var_dump($farben); print '</pre>';
?>
try this...
$farben = array_filter($farben, function($row) {
if($row["FacetValueCount"] > 3) {
return $row;
}
});
Related
I have an array like that:
array(5) {
["code"]=>
int(1)
["messageError"]=>
string(27) "La typologie est incorrecte"
["model"]=>
string(3) "lot"
["grp_regles"]=>
array(1) {
[0]=>
array(1) {
[0]=>
array(3) {
["champ"]=>
string(21) "lot_surface_habitable"
["comparaison"]=>
string(7) "between"
["valeurAttendue"]=>
array(2) {
[0]=>
int(16)
[1]=>
int(40)
}
}
}
}
["prerequis"]=>
array(2) {
[0]=>
array(3) {
["champ"]=>
string(6) "typ_id"
["comparaison"]=>
string(1) "="
["valeurAttendue"]=>
int(1)
}
[1]=>
array(3) {
["champ"]=>
string(22) "tranche.fus.fup.fup_id"
["comparaison"]=>
string(1) "="
["valeurAttendue"]=>
int(1)
}
}
}
I want to do a foreach in "prerequis":
$modelRetrieve = $this->retrieveModel($model);
$modelFind = $modelRetrieve::find($id);
$arrayError=[];
$query = '';
$path = storage_path() . "/json/controle.json";
$json = file_get_contents($path);
foreach (json_decode($json,true) as $key => $value) {
$test = true;
var_dump($value);
if($value['model'] === $model ){
foreach ($value['prerequis'] as $key => $value2) {
if( $test && $modelFind[$value2['champ']] == (int)$value2["valeurAttendue"] )
{
$test = true;
}
}
}
}
I need in second foreach to use in $value2['champ'] where $value2['champ'] is "tranche.fus.fup_id. So I need to explode that to have ['tranche']['fus']['fup_id'].
How to use explode with that ?
thanks everyone :)
you can use laravel data_get helper:
data_get($value2, $value2['champ'])
To nest the $segments of the string starting with the innermost item of the result, we have to array_reverse() the $segments so we can loop over it and wrap each iteration's result with another array level in the next iteration until we looped through the whole $segments array.
$exploded = array_reduce(array_reverse(explode('.', $value2['champ'])), function($res, $segment) {
return [ $segment => $res ];
}, []);
I checked this question and answers:
How to group a multidimensional array by a particular subarray value?
He wanted to group results by 'level'. But how would you do it to group it by 'level' first and then by 'type'?
Its pretty straight forward. Loop through $items array. Get each item's level and type and if they are not set yet, initialize them with an empty array. Then just push the "cust" value into the array.
I have given the code below.
I am assuming "$items" is an array which contains the input.
$g = [];
foreach($items as $k => $v) {
$l = $v["level"];
$t = $v["type"];
$c = $v["cust"];
if(!isset($g[$l])) {
$g[$l] = [];
}
if(!isset($g[$l][$t])) {
$g[$l][$t] = [];
}
$g[$l][$t][] = [
"cust" => $c
];
}
var_dump($g);
The output of this code would be like below:
array(3) {
[1]=>
array(1) {
["standard"]=>
array(2) {
[0]=>
array(1) {
["cust"]=>
string(6) "XT8900"
}
[1]=>
array(1) {
["cust"]=>
string(6) "XT8944"
}
}
}
[3]=>
array(1) {
["premier"]=>
array(2) {
[0]=>
array(1) {
["cust"]=>
string(6) "XT8922"
}
[1]=>
array(1) {
["cust"]=>
string(6) "XT8816"
}
}
}
[7]=>
array(1) {
["standard"]=>
array(1) {
[0]=>
array(1) {
["cust"]=>
string(6) "XT7434"
}
}
}
}
[P.S.]: You can also use sort to solve this problem easily. That's another way of solving this problem.
I've created a super array which have 4 levels.
Here is the beast :
array(4) {
["arrayCoordinateur"]=>
array(2) {
["siret"]=>
array(1) {
[0]=>
string(14) "44306184100039"
}
["sigle"]=>
array(1) {
[0]=>
string(3) "NdP"
}
}
["arrayGroupMember"]=>
array(2) {
["siret"]=>
array(2) {
[0]=>
string(14) "44306184100039"
[1]=>
string(14) "44306184100039"
}
["sigle"]=>
array(2) {
[0]=>
string(7) "rerzrez"
[1]=>
string(5) "Autre"
}
}
["arrayPartEnt"]=>
array(2) {
["sigle"]=>
array(3) {
[0]=>
string(6) "Blabla"
[1]=>
string(11) "CharbonBleu"
[2]=>
string(3) "JsP"
}
["siret"]=>
array(3) {
[0]=>
string(14) "77408201000034"
[1]=>
string(14) "51133834500024"
[2]=>
string(14) "40794236600011"
}
}
["arrayPartenExt"]=>
array(2) {
["sigle"]=>
array(2) {
[0]=>
string(3) "BNN"
[1]=>
string(5) "13456"
}
["siret"]=>
array(1) {
[0]=>
string(14) "00000000000000"
}
}
}
as you can see, the structure is comped of 4 Array who have all subs array called "siret" and "sigle".
I would like to be able to loop only on those siret and sigle without being annoyed by the first level, to put them into a table.
Here is my table :
$i = 0;
foreach ($allPartenaires as $partenaire=>$data) {
for ($i=0; $i < count($data) ; $i++) {
echo "<tr>
<td>
".$data['sigle'][$i]."
</td>
<td>
".$data['siret'][$i]."
</td>
</tr>";
}
}
$allPartenaires being the super array at the beginning of this post.
This code doesn't work since all the subarray dont have the same size...
Any help would be greatly appreciated
I think this is what you need:
$data = array(
"arrayCoordinateur"=>array(
"siret"=>array(
"44306184100039",
),
"sigle"=>array(
"NdP",
),
),
"arrayGroupMember"=>array(
"siret"=>array(
"44306184100039",
"44306184100039",
),
"sigle"=>array(
"rerzrez",
"Autre",
),
),
"arrayPartEnt"=>array(
"siret"=>array(
"77408201000034",
"51133834500024",
"40794236600011",
),
"sigle"=>array(
"Blabla",
"CharbonBleu",
"JsP",
"Something",
),
),
);
echo '<table>';
foreach($data as $key => $value) {
$length = count($value['siret']) > count($value['sigle']) ? count($value['siret']) : count($value['sigle']);
for($i = 0; $i < $length; $i++) { //Now it works even if siret and single doesn't have same number of elemenets
$v1 = '-';
$v2 = '-';
if (isset($value['siret'][$i])) {
$v1 = $value['siret'][$i];
}
if (isset($value['sigle'][$i])) {
$v2 = $value['sigle'][$i];
}
echo
'
<tr>
<td>'.$v1.'</td>
<td>'.$v2.'</td>
</tr>
';
}
}
echo '</table>';
Result:
44306184100039 NdP
44306184100039 rerzrez
44306184100039 Autre
77408201000034 Blabla
51133834500024 CharbonBleu
40794236600011 JsP
- Something
nested foreach loops are what you need
foreach ($allPartenaires as $data) {
echo '<tr>';
foreach($data as $nestedArray) {
foreach($nestedArray as $content) {
echo "<td>{$content}</td>";
}
}
echo '</tr>';
}
Think I'm missing a basic concept. I want to generate html by traversing through a few different arrays of data. They don't use numbers as indexes so numerical looping doesn't work. I cant figure out how to use a foreach() here either. How can I traverse $price and $description when the indexes aren't numbers?
Sample:
$traverser= 0;
while($traverser < $number_of_records)
{
print $traverser . " - " . $price[$traverser] . "<br />";
print $description[$traverser];
$traverser++;
}
Partial Sample of the Array Structure:
object(phpQueryObject)#2799 (13) { ["documentID"]=> string(32) "1d62be942498df890cab4ccb78a007a2" ["document"]=> &object(DOMDocument)#3 (0) { } ["charset"]=> &string(5) "utf-8" ["documentWrapper"]=> &object(DOMDocumentWrapper)#2 (17) { ["document"]=> &object(DOMDocument)#3 (0) { } ["id"]=> string(32) "1d62be942498df890cab4ccb78a007a2" ["contentType"]=> string(9) "text/html" ["xpath"]=> &object(DOMXPath)#4 (0) { } ["uuid"]=> int(0) ["data"]=> array(0) { } ["dataNodes"]=> array(0) { } ["events"]=> array(0) { } ["eventsNodes"]=> array(0) { } ["eventsGlobal"]=> array(0) { } ["frames"]=> array(0) { } ["root"]=> &object(DOMElement)#5 (0) { } ["isDocumentFragment"]=> &bool(true) ["isXML"]=> bool(false) ["isXHTML"]=> bool(false) ["isHTML"]=> bool(true) ["charset"]=> &string(5) "utf-8" } ["xpath"]=> &object(DOMXPath)#4 (0) { } ["elements"]=> array(560) { [0]=> object(DOMElement)#2239 (0) { } [1]=> object(DOMElement)#2240 (0) { } [2]=> object(DOMElement)#2241 (0) { } [3]=> object(DOMElement)#2242 (0) { } [4]=> object(DOMElement)#2243 (0) { } [5]=> object(DOMElement)#2244 (0) { } [6]=> object(DOMElement)#2245 (0) { } [7]=> object(DOMElement)#2246 (0) { } [8]=> object(DOMElement)#2247 (0) { }
Since it looks like you need the array keys as well, since you're referencing multiple different arrays, you want the $a as $k => $v syntax for foreach:
foreach($description as $key => $desc)
{
print $key . " - " . $price[$key] . "<br />";
print $desc;
}
You can take your pic as to how you want to iterate them:
<?php
$ary = array( // demo array
'apple' => 'Apple',
'orange' => 'Orange',
'grape' => 'Grape'
);
// show the structure
var_dump($ary); echo "\r\n";
// use a foreach with the key and value
foreach ($ary as $key => $val)
printf("%s => %s\r\n", $key, $val);
echo "\r\n";
// just get the raw keys
$keys = array_keys($ary);
var_dump($keys); echo "\r\n";
output:
array(3) {
["apple"]=>
string(5) "Apple"
["orange"]=>
string(6) "Orange"
["grape"]=>
string(5) "Grape"
}
apple => Apple
orange => Orange
grape => Grape
array(3) {
[0]=>
string(5) "apple"
[1]=>
string(6) "orange"
[2]=>
string(5) "grape"
}
There's always array_map & array_walk.
I'm not sure I get the question, but it's really as simple as:
<?php
$array = array('foo', 'bar');
foreach ($array as $element) {
echo "{$element}\n";
}
This should output "foo" and "bar".
I have this $record array
array(4) {
[0]=> array(2) { ["ROLE_ID"]=> string(1) "2" ["SUBFUNCTION_ID"]=> string(3) "904" }
[1]=> array(2) { ["ROLE_ID"]=> string(1) "2" ["SUBFUNCTION_ID"]=> string(3) "903" }
[2]=> array(2) { ["ROLE_ID"]=> string(1) "2" ["SUBFUNCTION_ID"]=> string(3) "902" }
[3]=> array(2) { ["ROLE_ID"]=> string(1) "2" ["SUBFUNCTION_ID"]=> string(3) "901" }
}
How can i manipulate it so it will become like this?
array("901","902","903","904");
Thanks in advance
$subfunctionIds = array();
foreach($record as $values) {
$subFunctionIds[] = $values['SUBFUNCTION_ID'];
}
// If you want them reversed like in your example output...
$subFunctionIds = array_reverse($subFunctionIds);
var_dump($subFunctionIds);
function fetch($row) {
return $row["SUBFUNCTION_ID"];
}
$result = array_map("fetch", $record);
sort($result);
var_dump($result);
in 5.3+ you could do better:
$result = array_map(function ($row) { return $row["SUBFUNCTION_ID"]; }, $record);
sort($result);
var_dump($result);
Try doing this:
foreach ($array as $row ) {
$response[] = $row["SUBFUNCTION_ID"];
}
print_r($response);