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';
}
}
}
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 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);
}
array(1) {
["farm"]=>
array(2) {
["folder1"]=>
array(2) {
["horse"]=>
array(1) {
["fred.jpg"]=>
string(30) "farm/folder1/horse/fred.jpg"
}
["cat"]=>
array(1) {
["john.jpg"]=>
string(28) "farm/folder1/cat/john.jpg"
}
}
["folder2"]=>
array(1) {
["cat"]=>
array(2) {
["sam.jpg"]=>
string(27) "farm/folder2/cat/sam.jpg"
["cat"]=>
array(1) {
["john.jpg"]=>
string(32) "farm/folder2/cat/cat/john.jpg"
}
}
}
}
}
Is it possible to detect only the most inner array keys?
foreach($array as $key => $value){
if ($key == $most_inner_array) {
echo $key;
}
}
So that in this case the result would be:
fred.jpg
john.jpg
sam.jpg
john.jpg
You can go through your array recursively and print the key if the value is not an array.
In case of just printing a value like in your example, you can do it with just array_walk_recursive() without any condition. Something like:
array_walk_recursive($your_array, function($value, $key) {
echo $key . "\n";
});
I'm trying to get this array ($resdata) with object(SimpleXMLElement) into a php array:
$resdata =
array(59) {
[0]=> ...
[10]=> object(SimpleXMLElement)#294 (28) {
["reservation_id"]=> string(7) "8210614"
["event_id"]=> string(6) "279215"
["space_reservation"]=> array(2) {
[0]=> object(SimpleXMLElement)#344 (9) {
["space_id"]=> string(4) "3760"
["space_name"]=> string(9) "205"
["formal_name"]=> string(33) "Center" }
[1]=> object(SimpleXMLElement)#350 (9) {
["space_id"]=> string(4) "3769"
["space_name"]=> string(9) "207"
["formal_name"]=> string(32) "Right" } } }
}
I've tried:
$res = (array)$resdata;
$reservation = $res['reservation'];
$result = array();
foreach ($reservation as $key => $value){
$res = array($value);
$spid = $res[0]->space_reservation->space_id;
echo $value->event_id."<br />";
echo $spid."<br />";
}
This only outputs the first space_id and I need to get all the space_ids within "space_reservation" array. Not all records will have multiple space_ids. Any help pointing me in the right direction is appreciated. Not sure if I should use xpath but I need to re-write my foreach statement regardless.
I was hoping to be able to literally convert all references to "object(SimpleXMLElement)#_ (#)" to "array(#)"
[10]=> array (28) {
["reservation_id"]=> string(7) "8210614"
["event_id"]=> string(6) "279215"
["space_reservation"]=> array(2) {
[0]=> array (9) {
["space_id"]=> string(4) "3760"
["space_name"]=> string(9) "205"
["formal_name"]=> string(33) "Center" }
[1]=> array (9) {
["space_id"]=> string(4) "3769"
["space_name"]=> string(9) "207"
["formal_name"]=> string(32) "Right" } } }
}
the function in my cakephp 1.3 controller is this:
$xml = simplexml_load_string($string);
$this->data['events']= $xml->children();
$resdata = $this->data['events'];
$this->set('resdata',$resdata);
I think this should do what you are looking for:
foreach ($resdata as $res) {
echo $res->event_id . '<br />';
foreach ($res->space_reservation as $reservation) {
echo $reservation->space_id . '<br />';
}
}
Googled it and found a general solution for any SimpleXMLElement to array conversion:
function xml2array($xml) {
$arr = array();
foreach ($xml as $element) {
$tag = $element->getName();
$e = get_object_vars($element);
if (!empty($e)) {
$arr[$tag] = $element instanceof SimpleXMLElement ? xml2array($element) : $e;
}
else {
$arr[$tag] = trim($element);
}
}
return $arr;
}