I have an array which looks like the following
array:2 [▼
0 => array:1 [▼
"input1" => "Something"
]
1 => array:1 [▼
"input2" => ""
]
]
Now the first element will always have some data. It is the second element I am interested in. At the moment, I am trying this
if(!empty($clientGroup[0][1]) || !empty($clientGroup[1][1]))
var_dump("Some Data");
} else {
var_dump("Both Empty");
}
The else should only be triggered if both elements are empty e.g.
array:2 [▼
0 => array:1 [▼
"input1" => ""
]
1 => array:1 [▼
"input2" => ""
]
]
If one of them have any data, the if should be triggered (so for the first array I showed, the if should be triggered).
How would I go about doing this, empty does not seem to work.
Thanks
The 2nd level keys do not exist so you will always be told the values are empty. Change the line
if(!empty($clientGroup[0][1]) || !empty($clientGroup[1][1]))
to,
if(!empty($clientGroup[0]['input1']) || !empty($clientGroup[1]['input2']))
and you should get the results you're after.
It's not realy 2D array because you have associative array inside an other array.
you must use key name (input1, input2) to access the value.
I recommend to use
if($retourdata[0]["input1"] !== "" || $retourdata[1]["input2"] !== "")
Related
I have following array:
array:3 [▼
0 => "1234"
1 => "1234"
2 => "1234"
]
In twig I want to check if all element are same return true else return false.
Hi I have a problem regarding multi-dimension array. I have a set of array looking like this
array:3 [▼
0 => array:1 [▼
"tree" => "0"
]
1 => array:1 [▼
"tree" => "2"
]
2 => array:1 [▼
"tree" => "0"
]
]
the array is from
dd($this->treeArray);
how do I want to get the array which the value is 0. Expected result/filter
For example:
array:2 [▼
0 => array:1 [▼
"tree" => "0"
]
1 => array:1 [▼
"tree" => "0"
]
]
or
array:1 [▼
0 => array:1 [▼
"tree" => "2"
]
]
if changing the index is impossible, its ok..
Thank you
As you are using Laravel, you could use array helper such as this
You can use like this:
use Illuminate\Support\Arr;
$newTreeArray = Arr::where($this->treeArray, function ($tree) => $tree['tree'] === "0");
do you want in_array()?
$arraysContainingZero=[];
foreach($this->treeArray as $wutkey => $wut){
if(in_array("0", $wut, true)){
$arraysContainingZero[$wutkey] = $wut;
}
}
var_dump($arraysContainingZero);
There are a lots of build in function in PHP that is useful for operating on an array. You can see the full list here: https://www.php.net/manual/en/ref.array.php
One of the function is called array_filter (https://www.php.net/manual/en/function.array-filter.php). The function iterates over each value in the array passing them to the callback function. If the callback function returns true, the current value from array is returned into the result array.
So in your case, you want to call the array_filter with a callback that return true if the value is 0. Example:
$input = [
[ 'tree' => 1 ],
[ 'tree' => 0 ],
[ 'tree' => 0 ],
];
$output = array_filter($input, function($arr) {
return $arr['tree'] === 0;
});
var_dump($output);
// result
array(2) {
[1]=>
array(1) {
["tree"]=>
int(0)
}
[2]=>
array(1) {
["tree"]=>
int(0)
}
}
I have a deployments Laravel Collection like this:
Illuminate\Support\Collection {#415 ▼
#items: array:5 [▼
0 => array:7 [▼
"id" => 31
"status" => "active"
"name" => "Deployment 1"
"spots" => array:4 [▼
0 => array:2 [▼
"id" => 33
"status" => "active" <-- Want to change this
]
1 => array:2 [▶]
2 => array:2 [▶]
3 => array:2 [▶]
]
"data" => array:3 [▶]
]
1 => array:7 [▶]
2 => array:7 [▶]
3 => array:7 [▶]
4 => array:7 [▶]
]
}
I want to update the nested status value to inactive. I have used the Laravel map function, but it only seems to work on collections that have one nesting level. So this...
$this->deployments->map(function ($deployment) {
$deployment['spots'][0]['status'] = 'inactive';
});
dd($this->deployments);
...leaves $this->deployments untouched.
Also tried using nested map functions obtaining a Call to a member function map() on array exception on second level as second and next nesting levels are considered arrays...
Any thoughts?
Thanks in advance.
With the map method, you are almost there. You will have to return the change made in $deployment and do an ->all() at the end to update the collection with the modified values.
For updating a single spot:
$deployments = $deployments->map(function($deployment){
$deployment['spots'][0]['status'] = 'inactive';
return $deployment;
})->all();
For updating all spots:
$deployments = $deployments->map(function($deployment){
foreach($deployment['spots'] as &$spot){
$spot['status'] = 'inactive';
}
return $deployment;
})->all();
For anyone researching this, a more elegant solution might be:
For updating a single spot:
$data = $deployments->all();
$deployments = data_set($data, 'spots.0.status', 'inactive');
For updating all spots:
$data = $deployments->all();
$deployments = data_set($data, 'spots.*.status', 'inactive');
I have an array:
array:8 [▼
0 => array:1 [▼
"data" => "789"
]
1 => array:1 [▼
"data" => "800"
]
2 => array:1 [▼
"data" => "789"
]
3 => array:1 [▼
"data" => "787"
]
4 => array:1 [▼
"data" => "787"
]
5 => array:1 [▼
"data" => "787"
]
6 => array:1 [▼
"data" => "787"
]
7 => array:1 [▼
"data" => "787"
]
]
I need to take out the last 2 elements of the array and compare them. I tried using $getLast2 = array_slice($chart_data, -2, 2, true); to get the last 2.
array:2 [▼
6 => array:1 [▼
"data" => "787"
]
7 => array:1 [▼
"data" => "787"
]
]
Which then splits it. But Im not sure how to compare these 2 elements within this new array. As the last 2 elements which are now 6 and 7 could change as more data is added. I basically need to tell if the first element is great than, less than or equal to the second element.
You can use built in end() function and then prev():
if (end($chart_data) == prev($chart_data)) {
echo 'Two last elements of an array are equal!';
}
Pass last variable as false;
preserve_keys
Note that array_slice() will reorder and reset the numeric array indices by default. You can change this behaviour by setting preserve_keys to TRUE.
array_slice($chart_data, -2, 2, false);
You have output this array
array:2 [▼
6 => array:1 [▼
"data" => "787"
]
7 => array:1 [▼
"data" => "787"
]
]
Re-index them to using PHP array_values() function
$outputedArray = array_values($outputedArray)
if($outputedArray[0]['data'] > $outputedArray[1]['data'])
echo "0 index is greater";
If the array you obtained from your previous code is called $splitArray, then you could do the following:
list($array1, $array2) = $splitArray;
if ($array1['data']>$array2['data'])
{
echo "1st is greater than 2nd<br>";
}
else
{
echo "1st is not greater than 2nd<br>";
}
If it suits you, you can just use the array_pop to do the comparison in this manner:
<?php
$array = [
["data" => "789"],
["data" => "800"],
["data" => "789"],
["data" => "787"],
["data" => "787"],
["data" => "789"],
["data" => "787"],
["data" => "787"],
];
// MAKE A COPY OF THE ORIGINAL ARRAY:
$arrayCopy = $array;
// POP THE LAST ELEMENT OFF THE $arrayCopy AND SAVE IT AS $lastElem:
$lastElem = array_pop($arrayCopy);
// POP THE LAST ELEMENT OFF THE $arrayCopy AGAIN AND SAVE IT AS $beforeLastElem:
$beforeLastElem = array_pop($arrayCopy);
// NOW YOU CAN COMPARE THE LAST ELEMENT AND THE ONE BEFORE IT
if($lastElem == $beforeLastElem){
echo "The last 2 Elements of \$array are the same";
}else{
echo "The last 2 Elements of \$array are not identical.";
}
I have a collection of Objects which take the following form
#attributes: array:8 [▼
"name" => "Something1"
"value" => "Some Data"
]
#attributes: array:8 [▼
"name" => "Something1Test"
"value" => "Some Data"
]
#attributes: array:8 [▼
"name" => "NOC M2"
"value" => "Some Data"
]
#attributes: array:8 [▼
"name" => "Couldbeanything M3"
"value" => "Some Data"
]
#attributes: array:8 [▼
"name" => "Couldbeanything M3Test"
"value" => "Some Data"
]
If a name attribute ends with Test, the preceding Object is related to it. So for instance Something1Test is related to Something1. The first part will always be the same "Something1", the cloned version just has Test added to the end.
I am looping my Objects and displaying their content. What I am trying to do is display a checkbox for all Objects that do not have a related Test Object. At the moment I have something like this
<?php if(strpos($data->name, 'Test') === false) { ?>
<div class="col-md-4">
//Display a checkbox
</div>
<?php } ?>
Now this somewhat works in a sense that it displays a checkbox for all Objects that dont have a name attribute which contains Test. So for the above data, it is displaying a checkbox for
Something1
NOC M2
Couldbeanything M3
The problem I am having is that Something1 and Couldbeanything have a related Test Object, so these two should not have a checkbox. With the above data, the only thing that should have a checkbox is NOC M2, because there is no NOC M2Test.
Is there any way to achieve what I am after? The first part of the name could be anything.
Thanks
I would first gather the names:
$names = array_column($objects, 'name');
The above assumes a nested array structure; you may need to do something like this instead:
$names = array();
foreach ($objects as $data) {
$names[] = $data->name;
}
Then I would perform the following check:
// Must not end in Test and must not have a corresponding Test object
if (substr($data->name, -4) != 'Test' && !in_array($data->name . 'Test', $names)) {
// do my output here
}