Unsetting an array on a variable property - php

So I have an object $entity, and a property $property.
$property has several arrays inside of it.
I want to be able to unset these arrays, as such:
unset($entity->$property['array']);
However, I'm getting the error:
Error: Cannot use string offset as an array
I tried setting $entity->$property to a single variable, but $entity was passed by reference into the function, and changing a new variable isn't going to modify the old object.
How do I ensure the old object gets modified and these arrays get unset?
Here is part of the entity, up until the property:
stdClass Object
(
[vid] => 33128
[uid] => 3
[title] => Title
[log] =>
[status] => 0
[comment] => 0
[promote] => 1
[sticky] => 0
[vuuid] => 3f922255-462b-459a-a8d6-9a2ac30899cf
[hash] => fa14fb00f1355761429977fe916e8f66
[nid] => 29652
[type] => resource
[language] => en-US
[created] => 1137158100
[changed] => 1371649725
[tnid] => 29652
[translate] => 0
[uuid] => 051eba0c-4b52-4269-b0d2-4d0ced9d6a6e
[revision_timestamp] => 1371649725
[revision_uid] => 0
[body] => Array
(
)
[field_file] => Array
(
[und] => Array
(
[0] => Array
(
[fid] => 26750
[uid] => 1
[filename] => filename.pdf
[uri] => public://cms/cms/filepath/filename.pdf
[filemime] => application/pdf
[filesize] => 666960
[status] => 1
[timestamp] => 1370660770
[type] => default
[uuid] => ea7c2c1f-4ef2-44fb-b89d-51c0fa090793
[hash] => aafc3f362cda639dc0475fccbeeb9af2
[rdf_mapping] => Array
(
)
[display] => 1
[description] => filename.pdf
)
)
)
$property is field_file, and looks like this:
Array ( [und] => Array ( [0] => Array ( [fid] => 11285 [uid] => 1 [filename] => filename.pdf [uri] => public://cms/cms/resource_library/datasheets/filepath/filename.pdf [filemime] => application/pdf [filesize] => 666960 [status] => 1 [timestamp] => 1368086259 [type] => default [uuid] => ea7c2c1f-4ef2-44fb-b89d-51c0fa090793 [hash] => cdb2fc5203a9e7f9c066a89bf9f70502 [rdf_mapping] => Array ( ) [display] => 1 [description] => filename.pdf ) ) )

It should be written like this instead:
unset($entity->{$property}['some_array_name']);
Without the curly brackets, the array access operator is bound more tightly. Hence your original expression will actually be processed as...
unset($entity->($property['some_array_name']));
As $property itself is a string, it's obviously wrong to treat as an array - that's what PHP warns you about.

Related

How to parse elasticsearch response in php

I try to parse my elasticsearch response in php.
When i print my json resultat with a var_dump, i've got that:
Array (
[took] => 6
[timed_out] =>
[_shards] => Array (
[total] => 5
[successful] => 5
[failed] => 0
)
[hits] => Array (
[total] => 1
[max_score] => 0.44896343
[hits] => Array (
[0] => Array (
[_index] => car
[_type] => car
[_id] => DqE0c4ygRgC81o39DNmwhQ
[_score] => 0.44896343
[_source] => Array (
[currency] => EUR
[link] => myrurl
[reference] => A785454A
[brand] => mybrand
[model] => mymodel
[description] =>
[link_picture] => mylinkpicture
[price] => myprice
[km] => mykm
[start_years] =>
[active] => 1
[title] => mytitle
[ranking] => 22
[date_create] => 2014-05-26
)
)
)
)
)
But, when i try to list or to find a title by a foreach:
$myData = json_decode($response);
foreach ($myobj->hits->hits as $result) {
echo $result->_source->title;
}
that doesn't work, because i obtain this error:
Message: Invalid argument supplied for foreach()
I don't see why.
Thanks in advnce.
In foreach you are trying to use "$myobj".
But decoded json is in "$myData".

getting title field from the process array in the webform

I am using the webform module for Drupal.
I needed to customize one of the webform nodes.
So I tried printing [title] => title of this webform goes here!!! using <?php print drupal_render($form['process']['title']); ?>
But it returns nothing, what am I doing wrong here?
Thanks!
FYI, the form can be rendered with; drupal_render($form['submitted']);
Array ( [#attached] => Array ( [css] => Array ( [0] => sites/all/modules/webform/css/webform.css ) [js] => Array ( [0] => sites/all/modules/webform/js/webform.js ) ) [#process] => Array ( [0] => webform_client_form_includes ) [#node] => stdClass Object ( [vid] => 22 [uid] => 2 [title] => title of this webform goes here!!! [log] => [status] => 1 [comment] => 1 [promote] => 1 [sticky] => 0 [nid] => 22 [type] => webform [language] => en [created] => 1373585579 [changed] => 1374117294 [tnid] => 0 [translate] => 0 [revision_timestamp] => 1374117294 [revision_uid] => 8 [body] => Array ( ) [rdf_mapping] => Array ( [rdftype] => ...
You should try this,
echo $form[#node]->title;
var_dump($form[#node]);// to test it your node data
As from your array
Array (
[#attached] => Array ([css] => Array ( [0] => sites/all/modules/webform/css/webform.css ) [js] => Array ( [0] => sites/all/modules/webform/js/webform.js ))
[#process] => Array ( [0] => webform_client_form_includes )
[#node] => stdClass Object ( [vid] => 22 [uid] => 2 [title] => title of this webform goes here!!! [log] => [status] => 1 [comment] => 1 [promote] => 1 [sticky] => 0 [nid] => 22 [type] => webform [language] => en [created] => 1373585579 [changed] => 1374117294 [tnid] => 0 [translate] => 0 [revision_timestamp] => 1374117294 [revision_uid] => 8 [body] => Array ( ) [rdf_mapping] => Array ( [rdftype] => ...

How would I read this array ("stdClass Object")

I am using the Quizlet API 2.0, and I am pretty new to this
How do I read a value(s) from something like this:
stdClass Object ( [id] => 102269 [name] => Learn Spanish with Cats! [set_count] => 3 [user_count] => 10 [created_date] => 1308035691 [is_public] => 1 [has_password] => [has_access] => 1 [has_discussion] => 1 [member_add_sets] => 1 [description] => This set is exclusively for Spanish flashcard sets with relevant cat images as the set definitions. [sets] => Array ( [0] => stdClass Object ( [id] => 6081999 [url] => http://quizlet.com/6081999/lesson-4-with-catsdogs-flash-cards/ [title] => Lesson 4 (with cats+dogs) [created_by] => wsvincent [term_count] => 33 [created_date] => 1311984796 [modified_date] => 1312490710 [has_images] => 1 [subjects] => Array ( [0] => spanish cats dogs ) [visibility] => public [editable] => groups [has_access] => 1 ) [1] => stdClass Object ( [id] => 5855751 [url] => http://quizlet.com/5855751/paso-a-paso-book-1-chapter-4-flash-cards/ [title] => Paso a Paso Book 1 Chapter 4 [created_by] => catdawg426 [term_count] => 30 [created_date] => 1307761267 [modified_date] => 1307819129 [has_images] => 1 [subjects] => Array ( [0] => spanish ) [visibility] => public [editable] => only_me [has_access] => 1 ) [2] => stdClass Object ( [id] => 5873819 [url] => http://quizlet.com/5873819/los-gatos-de-viaje-flash-cards/ [title] => Los Gatos de Viaje! [created_by] => tiffreja [term_count] => 21 [created_date] => 1307996657 [modified_date] => 1307996796 [has_images] => 1 [subjects] => Array ( [0] => spanish [1] => language [2] => foreign ) [visibility] => public [editable] => only_me [has_access] => 1 ) ) [members] => Array ( [0] => stdClass Object ( [username] => philfreo [role] => creator [email_notification] => 1 ) [1] => stdClass Object ( [username] => milkncookies [role] => member [email_notification] => 1 ) [2] => stdClass Object ( [username] => Icypaw [role] => member [email_notification] => ) [3] => stdClass Object ( [username] => luckycat10 [role] => member [email_notification] => ) [4] => stdClass Object ( [username] => jeffchan [role] => member [email_notification] => ) [5] => stdClass Object ( [username] => catchdave [role] => member [email_notification] => 1 ) [6] => stdClass Object ( [username] => tiffreja [role] => member [email_notification] => 1 ) [7] => stdClass Object ( [username] => catdawg426 [role] => member [email_notification] => 1 ) [8] => stdClass Object ( [username] => ihaque [role] => member [email_notification] => 1 ) [9] => stdClass Object ( [username] => jalenack [role] => member [email_notification] => 1 ) ) )
For instance, if I want to get the name of that first set, "Learn Spanish with Cats", how do I echo it via variable?
It already converts the JSON to an array I think:
$data = json_decode($json);
Your object is not an array, but rather, well, an Object. So use the -> operator to access its properties:
echo $data->name;
It contains a property which itself is an array of additional objects. For example, to get the URL of id 6081999, you would do:
echo $data->sets[0]->url;
// http://quizlet.com/6081999/lesson-4-with-catsdogs-flash-cards/
Here is a simple solution to convert a stdClass Object in array in php with get_object_vars
Look at : https://www.php.net/manual/en/function.get-object-vars.php
Example :
dump($array);
$var = get_object_vars($array);
dump($var);
Or replace dump() function by print_r()
Use function key
eg echo key($array)
I have looked something before, when you use the json_decode()
$data = json_decode();
U can send some parameters, the first of them is "$json", it will be the json string
{"1":"first","2":"second"}
But that json decode with one parameter return an Object and the default value of the second parameter is "false". If you want that back in array you only need to use the second parameter and send "true".
$data =json_decode($json,true);
And you can recibe it like an array. :)
In case you got stdClass Object in the array, for example
$user = $result1->fetch_object() then set $user into a variable
$val = $user->user_id (make sure user_id is your database column name, its the column name). You will get a single value in $val that comes from database.

Accessing field value in Drupal 7 on load_node in template

In a template for a content type I am loading a node from a node reference.
It loads and if I do a print_r I get this:
stdClass Object (
[vid] => 40
[uid] => 14
[title] => Cover
[log] =>
[status] => 1
[comment] => 0
[promote] => 1
[sticky] => 0
[nid] => 40
[type] => portfolio_image_main
[language] => en
[created] => 1309382711
[changed] => 1309382711
[tnid] => 0
[translate] => 0
[revision_timestamp] => 1309382711
[revision_uid] => 14
[field_portolio_image] => Array (
[en] => Array (
[0] => Array (
[fid] => 5626
[alt] =>
[title] =>
[uid] => 14
[filename] => Cover.jpg
[uri] => public://Cover.jpg
[filemime] => image/jpeg
[filesize] => 147898
[status] => 1
[timestamp] => 1309382711
)
)
)
[name] => jojo
[picture] => 0
[data] => a:1:{s:7:"contact";i:1;}
)
and Im trying to access the single variable here:
$newImagePath1 = $newImage1->field_portfolio_image['en '][0]['filename'];
but so far nothing. Any thoughts?
please try to use below code
$keys = array_keys($arr[field_portolio_image][en]);
$arr[field_portolio_image][en][$keys][filename];
There is a helper function to access field items for the user's correct language (otherwise, you'd have to hardcode the ['en'] part).
field_get_items()
So your code would end up being something like this:
$field_instances = field_get_info('node', $newImage1, 'field_portfolio_image');
// $field_instances should now be an array.
foreach ($field_instances as $field_instance) {
print $field_instance['filepath'];
}

problem assigning array to variable

I'm sure this is a simple one. I have an array in a simplexml object. When I try to assign the array to a variable, it only assigns the first index of the array. How can I get it to assign the whole array. This is my code.
$xml = simplexml_load_string(FlickrUtils::getMyPhotos("flickr.photos.search", $_SESSION['token']));
$photosArray = $xml->photos;
//$photosArray = $xml->photos->photo;
//echo gettype($photosArray);
print_r($photosArray);
This is the result of the print_r($photosArray);
SimpleXMLElement Object
(
[#attributes] => Array
(
[page] => 1
[pages] => 1
[perpage] => 100
[total] => 4
)
[photo] => Array
(
[0] => SimpleXMLElement Object
(
[#attributes] => Array
(
[id] => 5335626037
[owner] => 57991585#N02
[secret] => bd66f06b49
[server] => 5210
[farm] => 6
[title] => 1
[ispublic] => 1
[isfriend] => 0
[isfamily] => 0
)
)
[1] => SimpleXMLElement Object
(
[#attributes] => Array
(
[id] => 5336238676
[owner] => 57991585#N02
[secret] => 898dffa011
[server] => 5286
[farm] => 6
[title] => 2
[ispublic] => 1
[isfriend] => 0
[isfamily] => 0
)
)
[2] => SimpleXMLElement Object
(
[#attributes] => Array
(
[id] => 5335625381
[owner] => 57991585#N02
[secret] => 60a0c84597
[server] => 5126
[farm] => 6
[title] => 4
[ispublic] => 1
[isfriend] => 0
[isfamily] => 0
)
)
[3] => SimpleXMLElement Object
(
[#attributes] => Array
(
[id] => 5335625195
[owner] => 57991585#N02
[secret] => 49348c1e8b
[server] => 5126
[farm] => 6
[title] => 3
[ispublic] => 1
[isfriend] => 0
[isfamily] => 0
)
)
)
)
Thanks for youe help!
I fail to see an array in your example. However, $xml is traversable, so you probably mean that. $xml->photos selects only the first photo element though. You are probably looking for
$photosArray = $xml->xpath('//photo');
which indeed returns an array.
In order to return all photo, can make used on children()
You can cast the list of simplexml objects into array, like
$photosArray = (array)$xml->children();
/* or retain the simplexml object */
$photosArray = $xml->children();

Categories