I had this response from method post
array(7) {
["enable"]=>
array(2) {
[0]=>
string(2) "on"
[1]=>
string(2) "on"
}
["value"]=>
array(2) {
[0]=>
string(8) "R$ 10,00"
[1]=>
string(8) "R$ 10,00"
}
["zip_code"]=>
array(2) {
[0]=>
string(9) "57200-970"
[1]=>
string(9) "57200-990"
}
["address"]=>
array(2) {
[0]=>
string(28) "Avenida Floriano Peixoto"
[1]=>
string(33) "Povoado Tabuleiro dos Negros"
}
["neighborhood"]=>
array(2) {
[0]=>
string(6) "Centro"
[1]=>
string(4) "Bairro Vermelho"
}
["city"]=>
array(2) {
[0]=>
string(6) "Penedo"
[1]=>
string(6) "Penedo"
}
["state"]=>
array(2) {
[0]=>
string(2) "AL"
[1]=>
string(2) "AL"
}
}
I need first use the foreach to get the $_POST['active'] and get value from another arrays index
foreach (Request::post('enable') as $k => $v) {
print Request::post(array("zip_code", $k)) . "\n";
// I hope same result of $_POST['zip_code'][$k]
}
the real problem is my function
public static function post($key, $clean = false) {
$result = is_array($key) ? array_search($key, $_POST) : $_POST[$key];
if (!empty($result)) {
return ($clean) ? trim(strip_tags($result)) : $result;
}
return NULL;
}
if param key is an array get value from this key array
for example $_POST['a']['b']['c']
[EDIT]
I improve the function, thanks #mickmackusa
public static function post($key, $clean = false) {
$focus = $_POST;
if (is_array($key)) {
foreach ($key as $k) {
if (!isset($focus[$k])) {
return NULL;
}
$focus = $focus[$k];
if (!is_array($focus)) {
$result = $focus;
}
}
} else {
$result = empty($focus[$key]) ? NULL : $focus[$key];
}
return ($clean ? trim(strip_tags($result)) : $result);
}
I think line1 inside post() is fouling things up. Depending on $key's type, you are declaring $result's value as a key or a value. But then seemingly treating both possibilities as a value in the next condition and returning it.
public static function post($key, $clean=false) {
$result = is_array($key) ? array_search($key, $_POST) : $_POST[$key];
// if $key is an array, then $result is the key of the found value within $POST or FALSE if not found
// if $key is not an array, then $result is the value of $_POST[$key]
if (!empty($result)) {
return ($clean) ? trim(strip_tags($result)) : $result;
}
return NULL;
}
The !empty condition is checking for NOT falsey values in $result.
See all falsey values # empty() manual, including 0 which can be a totally legitimate index/key, and more.
Instead, I think you want to use:
public static function post($key,$clean=false){
$focus=$_POST;
foreach($key as $k){
if(!isset($focus[$k])){
return NULL;
}else{
$focus=$focus[$k];
if(!is_array($focus)){
$result=$focus;
}
}
}
return ($clean?trim(strip_tags($result)):$result);
}
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 must update my website to the last version of php 7.4 after that i find this notice in some plugins
**> Notice: Trying to access array offset on value of type null in
C:\projets\htdocs\mapsport\wp-content\plugins\ekit-megamenu\library\scss\scss.inc.php
on line 1753**
protected function sortArgs($prototype, $args) {
$keyArgs = array();
$posArgs = array();
foreach ($args as $arg) {
list($key, $value) = $arg;
$key = $key[1]; // line 1753
if (empty($key)) {
$posArgs[] = $value;
} else {
$keyArgs[$key] = $value;
}
}
if (!isset($prototype)) return $posArgs;
$finalArgs = array();
foreach ($prototype as $i => $names) {
if (isset($posArgs[$i])) {
$finalArgs[] = $posArgs[$i];
continue;
}
$set = false;
foreach ((array)$names as $name) {
if (isset($keyArgs[$name])) {
$finalArgs[] = $keyArgs[$name];
$set = true;
break;
}
}
if (!$set) {
$finalArgs[] = null;
}
}
return $finalArgs;
}
how can i change the code without change my php version ?
the result after add var_dump($args); before the foreach Suggested by #Ro Achterberg
array(2) { [0]=> array(3) { [0]=> NULL [1]=> array(2) { [0]=>
string(3) "var" [1]=> string(23) "ekit-menu-simple__white" } [2]=>
bool(false) } [1]=> array(3) { [0]=> NULL [1]=> array(3) { [0]=>
string(6) "number" [1]=> string(3) "6.5" [2]=> string(1) "%" } [2]=>
bool(false) } }
On line 1752 the value of $key is NULL. It doesn't get any value. So can you please try $key = isset( $key[1] ) ? $key[1] : '';?
I want to delete some data from an array in PHP. Here is the array:
array(4) {
[0]=> array(1) { ["image"]=> string(20) "w85YrKChBGTZ9fQS.jpg" }
[1]=> array(1) { ["image"]=> string(20) "3buahEs6rRWFdYez.jpg" }
[2]=> array(1) { ["image"]=> string(20) "gYPtDrx3sFzkVENB.jpg" }
[3]=> array(1) { ["image"]=> string(20) "JE3rodDvs6521cFm.jpg" }
}
Here is my method and where I am deleting:
public function deleteImage(){
foreach (getCarImages() as $array){
//var_dump($array).'<br>';
$index = array_search('w85YrKChBGTZ9fQS.jpg',$array);
if($index !== FALSE){
var_dump($index).'<br>';
unset($array[$index]);
}else{
echo '<br>else here';
}
}
}
And here is the result of deleteImage()
string(5) "image"
else
here
else
here
else
here
I am confused. How can I delete a nested array from the main array.
If you need to delete a whole subarray from array, then use array_flter function:
public function deleteImage(){
return array_filter(getCarImages(), function ($v) {
return $v['image'] != 'w85YrKChBGTZ9fQS.jpg';
});
}
Update: Anonymous function doesn't know about $imageName variable. You have to use it:
public function deleteImage($imageName = null)
{
$myarray = array_filter(
getCarImages(),
function ($v) use ($imageName) { return $v['image'] != $imageName; }
);
}
You can trans the images as a reference to the function.
public function deleteImage(&$images){
foreach ($images as $k => $array){
//var_dump($array).'<br>';
$index = array_search('w85YrKChBGTZ9fQS.jpg',$array);
if($index !== FALSE){
var_dump($index).'<br>';
unset($images[$k]);
}else{
echo '<br>else here';
}
}
}
I have an array that looks kinda like this:
array(1) { ["Special"]=> array(4) { [0]=> array(2) { ["ID"]=> int(1) ["Visitors"]=> int(2) } [1]=> array(2) { ["ID"]=> int(4) ["Visitors"]=> int(5) } [2]=> array(2) { ["ID"]=> int(169) ["Visitors"]=> int(0) } } }
How can I filter it by 'ID' value, so the result would be looking like this (if I need to get arrays with ID = 4):
array(1) { ["Special"]=> array(4) { [1]=> array(2) { ["ID"]=> int(4) ["Visitors"]=> int(5) } } }
I tried to use that function, but it doesn't return what I need:
function search($array, $key, $value) {
$results = array();
if (is_array($array)) {
if (isset($array[$key]) && $array[$key] == $value) {
$results[] = $array;
}
foreach ($array as $subarray) {
$results = array_merge($results, search($subarray, $key, $value));
}
}
return $results; }
You can use array_filter to do this:
$result = array(
"Special" => array_filter($array["Special"], function($element) {
return $element["ID"] == 4;
})
);
Suppose I have a JSON string:
$json = '{"lemon":"test",
"orange":["toto", "tata", "tete"],
"zob":[{"id":"0"}, {"id":"1"}]}';
I'd like to cycle through that encoded object to modify every string in it, so I have a recursive function:
function doObject($__obj){
$__obj = cycleObject($__obj);
return $__obj;
}
function cycleObject($__obj){
$type = gettype($__obj);
foreach($__obj as $var => &$val){
switch(gettype($val)){
case 'object':
cycleObject($val);
break;
case 'array':
cycleObject($val);
break;
case 'string':
if($type == 'object'){
$__obj->$var = $val.'-ok';
}else{
if($type == 'array'){
$__obj[$var] = $val.'-ok';
}
}
break;
}
}
return $__obj;
}
And I call the function:
$obj = doObject(json_decode($json));
var_dump($obj);
Which gives :
object(stdClass)#1 (3) {
["lemon"]=> string(7) "test-ok"
["orange"]=> array(3) {
[0]=> string(4) "toto"
[1]=> string(4) "tata"
[2]=> string(4) "tete" }
["zob"]=> array(2) {
[0]=> object(stdClass)#2 (1) {
["id"]=> string(4) "0-ok" }
[1]=> object(stdClass)#3 (1) {
["id"]=> string(4) "1-ok" }
}
}
Now my problem is, for some reason, I am unable to modify directly inside an array composed by string, or should I say, the modified string inside an array (and not inside an object inside an array) because the array loses its reference. How do I fix that so in orange I instead obtain:
[0]=> string(7) "toto-ok"
[1]=> string(7) "tata-ok"
[2]=> string(7) "tete-ok"
Your array of strings isn't being scrutinized correctly by your function. Basically, in each array you need a second check to see if you are dealing with another array/object or a string, otherwise regular arrays of strings are being bypassed....oddly enough. The following should work for you:
$json = '{"lemon":"test",
"orange":["toto", "tata", "tete"],
"zob":[{"id":"0"}, {"id":"1"}]}';
function doObject($__obj){
$__obj = cycleObject($__obj);
return $__obj;
}
function cycleObject($__obj){
foreach($__obj as $key => &$val){
if(is_object($val)) {
cycleObject($val);
}
if(is_array($val)) {
foreach($val as &$v) {
if(is_object($v) || is_array($v)) {
cycleObject($v);
} else {
$v .= '-ok';
}
}
}
if(is_string($val)) {
$val .= '-ok';
}
}
return $__obj;
}
$obj = doObject(json_decode($json));
var_dump($obj);
This produced the results you were looking for in my local environment.
object(stdClass)#1 (3) {
["lemon"]=>
string(7) "test-ok"
["orange"]=>
array(3) {
[0]=>
string(7) "toto-ok"
[1]=>
string(7) "tata-ok"
[2]=>
string(7) "tete-ok"
}
["zob"]=>
array(2) {
[0]=>
object(stdClass)#2 (1) {
["id"]=>
string(4) "0-ok"
}
[1]=>
object(stdClass)#3 (1) {
["id"]=>
string(4) "1-ok"
}
}
}