Illegal Offset Type? - php

$images = valley_images();
var_dump($images);
$sorted_data = array();
foreach($images as $key => $value) {
if ($key == 'timestamp') {
$sorted_data[$value][] = $images;
}
}
ksort($sorted_data);
The error is appearing on this line:
$sorted_data[$value][] = $images;
When I do the var dump of images I receive this:
array(2) {
[0]=> array(2) {
["id"]=> string(2) "17" ["timestamp"]=> string(10) "1359797773"
}
[1]=> array(2) {
["id"]=> string(2) "20" ["timestamp"]=> string(10) "1359934365"
}

A nice way to do sorting of a key on a multi-dimensional array without having to know what keys you have in the array first:
<?php
$people = array(
array("name"=>"Bob","age"=>8,"colour"=>"red"),
array("name"=>"Greg","age"=>12,"colour"=>"blue"),
array("name"=>"Andy","age"=>5,"colour"=>"purple"));
var_dump($people);
$sortArray = array();
foreach($people as $person){
foreach($person as $key=>$value){
if(!isset($sortArray[$key])){
$sortArray[$key] = array();
}
$sortArray[$key][] = $value;
}
}
$orderby = "name"; //change this to whatever key you want from the array
array_multisort($sortArray[$orderby],SORT_DESC,$people);
var_dump($people);

Related

How can I return this array without duplicates in PHP?

I´m trying to get this array
[24]=>
array(2) {
[0]=>
string(1) "21"
[1]=>
string(1) "22"
}
So far I have this:
function Job($conn){
$array = array();
foreach(array_unique($_SESSION['ROLES']) as $value)
{
$sql="SELECT employee,task FROM table WHERE id={$value} AND employee=24";//echo($sql);
$result = $conn->executeSQL($sql);
foreach($result as $value)
{
$array[$value['employee']][] = $value['task'];
}
}
return $array;
}
Which returns the tasks for two values I have store in $_SESSION['ROLES'] but if there are duplicates I need to remove them :
[24]=>
array(4) {
[0]=>
string(1) "21"
[1]=>
string(1) "22"
[2]=>
string(1) "21"
[3]=>
string(1) "22"
}
I can think of two ways to do this.
Use array_unique as indicated by comments.
function Job($conn) {
$array = array();
foreach(array_unique($_SESSION['ROLES']) as $value)
{
$sql="SELECT employee,task FROM table WHERE id={$value} AND employee=24";//echo($sql);
$result = $conn->executeSQL($sql);
$temp = [];
foreach($result as $value)
{
$temp[] = $value['task'];
}
$array[$value['employee']] = array_unique($temp);
}
return $array;
}
Add a conditional to the code to add the element if it does not already exist.
function Job($conn) {
$array = array();
foreach(array_unique($_SESSION['ROLES']) as $value)
{
$sql="SELECT employee,task FROM table WHERE id={$value} AND employee=24";//echo($sql);
$result = $conn->executeSQL($sql);
foreach($result as $value)
{
if ( ! in_array($value['task'], $array[$value['employee']]) )
$array[$value['employee']][] = $value['task'];
}
}
return $array;
}

how to use explode for an array of objects

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 ];
}, []);

PHP duplicates in nested multidimensional array to combine certain value

The desired output is to have:
{"userId":1,"email":"example#email.com","first":"Tyler","last":"Kanz","groups":[{"groupId":"1","groupName":"GROUP A","groupRoles":["1", "3"]},{"groupId":"2","groupName":"GROUP B","groupRoles":["2"]}]}
Which would compare the Group IDs and combine duplicates with their Group Roles.
I have tried using,
foreach ($groups as $group) {
if ($group['groupId'] == $group_id) {
array_push($group['groupRoles'], $group_role);
//And then unsetting the array
}
It is just adding all of the Groups/Roles into the Group array onto the Groups Array.
array(3) { ["groupId"]=> string(1) "1" ["groupName"]=> string(5)
"GROUP A" ["groupRoles"]=> array(1) { [0]=> string(1) "1" } } array(3) {
["groupId"]=> string(1) "2" ["groupName"]=> string(10) "GROUP B"
["groupRoles"]=> array(1) { [0]=> string(1) "2" } } array(3) {
["groupId"]=> string(1) "1" ["groupName"]=> string(5) "GROUP A"
["groupRoles"]=> array(1) { [0]=> string(1) "3" } }
{"userId":1,"email":"example#email.com","first":"Tyler","last":"Kanz","groups":[{"groupId":"1","groupName":"IRISS","groupRoles":["1"]},{"groupId":"2","groupName":"GROUP B","groupRoles":["2"]},{"groupId":"1","groupName":"GROUP A","groupRoles":["3"]}]}
foreach ($groupmeta as $value) {
$group_roles = array();
$array = unserialize($value['property_value']);
if ($array[0] == $user_ID) {
//Gets Group ID and Role
$group_id = $value['group_id'];
$group_role = $array[1];
$exists = false;
$group_name_q= json_decode(json_encode($wpdb->get_results($wpdb->prepare('SELECT group_name FROM groups WHERE id=%s', $group_id))), true);
$group_name = $group_name_q[0]['group_name'];
echo $group_role;
echo '<br>';
$group_roles[] = $group_role;
$group_info = array(
'groupId'=>$group_id,
'groupName'=>$group_name,
'groupRoles'=>$group_roles,
);
$groups[] = $group_info;
}
}
$sorted_groups = array();
echo '<br>';
}
$return_arr = array(
"userId"=>$user_ID,
"email"=>$user_email,
"first"=>$user_first,
"last"=>$user_last,
"groups"=>$groups
);
echo json_encode($return_arr);
//var_dump($groupmeta_value);
}
Found an answer, FOREACH runs as a cloned object.
Instead I used the following FOR loop and got the desired results.
$found_group = false;
for ($i = 0; $i < count($groups); $i++) {
if ($groups[$i]['groupId'] == $group_id) {
array_push($groups[$i]['groupRoles'], $group_role);
$found_group = true;
}
}
if (!$found_group) {
//Group Name
$group_name_q= json_decode(json_encode($wpdb->get_results($wpdb->prepare('SELECT group_name FROM groups WHERE id=%s', $group_id))), true);
$group_name = $group_name_q[0]['group_name'];
$group_roles[] = $group_role;
$group_info = array(
'groupId'=>$group_id,
'groupName'=>$group_name,
'groupRoles'=>$group_roles,
);
$groups[] = $group_info;
}

notice trying to access array offset on value of type null wordpress plugins

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] : '';?

build json data from php object

I want to buld a Json object to feed my graphs. I have got the following code to change my PHP object.
$rows = $this->Website_model->getGraphData();
$_rows = array();
$i = 0;
foreach ($rows as $key => $row) {
foreach ($row as $column => $value) {
$_rows[$i][] = $value;
}
$i++;
}
$rows = $_rows;
echo json_encode(array("sEcho" => intval($sEcho), "data" => $rows));
die();
My current ouput looks like this:
array(24) {
[0]=>
array(3) {
[0]=>
string(7) "3283581"
[1]=>
string(10) "2013-10-16"
}
It should look something like this:
{"y":15,"x":"2012-11-19"},{"y":18,"x":"2012-11-19"} etc etc
How can I add the Y and X to my data and take care i will get the right output to feed my graph?
/////////////////////////////////////////////////////
I tried the following:
Now i'm using the following code:
$rows = $this->Website_model->getGraphDataPositives();
$_rows = array();
$i = 0;
foreach ($rows as $key => $row) {
foreach ($row as $column => $value) {
$_rows[$i]['x'] = $value;
$_rows[$i]['y'] = $value;
$i++;
}
}
This results in the following response:
array(48) {
[0]=>
array(2) {
["x"]=>
string(7) "3283581"
["y"]=>
string(7) "3283581"
}
[1]=>
array(2) {
["x"]=>
string(10) "2013-10-16"
["y"]=>
string(10) "2013-10-16"
}
So it isn't okay yet.. it should say:
array(48) {
[0]=>
array(2) {
["x"]=>
string(7) "3283581"
["y"]=>
string(7) "2013-10-16"
}
[1]=>
array(2) {
["x"]=>
string(10) "1512116"
["y"]=>
string(10) "2013-10-17"
}
This would create an array like you wish :
$rows = $this->Website_model->getGraphData();
$_rows = array();
$i = 0;
foreach ($rows as $key => $row) {
$_rows[$i]['y'] = $rows[0];
$_rows[$i]['x'] = $rows[1];
$i++;
}
Your example data is not correct. You have array(3), but you show only 2 elements. Also in the output data you have 3283581 and the y values are 15 and 18. So there is no way I can guess how to convert those values.

Categories