Can't access elements of merged arrays - php

So I had 2 objects that need to work together, one idea that was given to me was to merge them. Did that, using array_merge
But now I can't access the second object from the merged one and I'm stuck. The keys that appear after merge look a little weird, have I used it wrongly?
$merged=array_merge_recursive((array)$post,(array)$image);
And the var_dump for $merged shows me this:
array (size=10)
0 =>
object(Models\Post)[7]
protected 'id' => string '17' (length=2)
protected 'title' => string 'qwq' (length=3)
protected 'body' => string 'wqwq' (length=4)
protected 'views' => string '22' (length=2)
protected 'imgId' => string '17' (length=2)
protected '_attr_accessible' =>
array (size=3)
0 => string 'title' (length=5)
1 => string 'body' (length=4)
2 => string 'views' (length=5)
protected '_validators' =>
array (size=0)
empty
protected '_errors' =>
array (size=0)
empty
protected '_valid' => boolean true
'�*�id' => string '17' (length=2)
'�*�path' => string '/media/' (length=7)
'�*�name' => string 'col_right_callout.jpg' (length=21)
'�*�idPost' => string '0' (length=1)
'�*�description' => string 'wqeqw' (length=5)
'�*�_attr_accessible' =>
array (size=3)
0 => string 'name' (length=4)
1 => string 'path' (length=4)
2 => string 'description' (length=11)
'�*�_validators' =>
array (size=0)
empty
'�*�_errors' =>
array (size=0)
empty
'�*�_valid' => boolean true
How do I access the path or any field from the second part of the array?
Content of $post
array (size=1) 0 =>
object(Models\Post)[7]
protected 'id' => string '17' (length=2)
protected 'title' => string 'qwq' (length=3)
protected 'body' => string 'wqwq' (length=4)
protected 'views' => string '23' (length=2)
protected 'imgId' => string '17' (length=2)
protected '_attr_accessible' =>
array (size=3)
0 => string 'title' (length=5)
1 => string 'body' (length=4)
2 => string 'views' (length=5)
protected '_validators' =>
array (size=0)
empty
protected '_errors' =>
array (size=0)
empty
protected '_valid' => boolean true
Content of $image
object(Models\Image)[9]
protected 'id' => string '17' (length=2)
protected 'path' => string '/media/' (length=7)
protected 'name' => string 'col_right_callout.jpg' (length=21)
protected 'idPost' => string '0' (length=1)
protected 'description' => string 'wqeqw' (length=5)
protected '_attr_accessible' =>
array (size=3)
0 => string 'name' (length=4)
1 => string 'path' (length=4)
2 => string 'description' (length=11)
protected '_validators' =>
array (size=0)
empty
protected '_errors' =>
array (size=0)
empty
protected '_valid' => boolean true

Because image is object, protected and private properties have special character prepended when you cast object to array.
With post you are casting array to array. If you do something like (array)$post[0] you will get same issue with both objects.
Take a look how to get array out of it: How to convert (cast) Object to Array without Class Name prefix in PHP?

Related

PHP. array_column() analogue for SimpleXMLElement object

object(SimpleXMLElement)[803]
public 'row' =>
array (size=13)
0 =>
object(SimpleXMLElement)[797]
public '#attributes' =>
array (size=4)
'codeonimage' => string '01' (length=2)
'name' => string 'Крышка' (length=12)
'oem' => string '13711251885' (length=11)
'ssd' => string '$HgsQRnNPF0d$' (length=174)
public 'attribute' =>
array (size=2)
0 =>
object(SimpleXMLElement)[813]
public '#attributes' =>
array (size=3)
'key' => string 'amount' (length=6)
'name' => string 'Количество' (length=20)
'value' => string '1' (length=1)
1 =>
object(SimpleXMLElement)[814]
public '#attributes' =>
array (size=3)
'key' => string 'end_of_production' (length=17)
'name' => string 'end_of_production' (length=17)
'value' => string 'Не производтся с: 19871116' (length=40)
...
I'm trying to take all the 'oem' values from each SimpleXML element like:
array_column($simpleXMLObject->row, 'oem');
... and of course I get an error:
array_column() expects parameter 1 to be array, object given
There is another option with a full search. But maybe there is some more pretty way to do this?

How to convert array of arrays into object in PHP?

I have an array of arrays like this one below and I want to convert it to object array.
array (size=3)
'declaration' =>
array (size=99)
'GO_IMPZONK_ID' => string '130334' (length=6)
'ID' => string '19802862' (length=8)
'CUSTE' => string '10100' (length=5)
'DCLEXP' => null
'DCL_BROKER_CODE' => string '' (length=0)
'RLCCODE' => string '' (length=0)
'items' =>
array (size=1)
0 =>
array (size=50)
'GO_IMPDCL_ID' => string '19802862' (length=8)
'TARIFYEAR' => string '85' (length=2)
'extensions' =>
array (size=6)
0 =>
array (size=5)
'GO_IMPDCL_ID' => string '19802862' (length=8)
'TOTVALUE' => string '0.00' (length=4)
'EXPDATE' => string '2004-03-20' (length=10)
1 =>
array (size=5)
'GO_IMPDCL_ID' => string '19802862' (length=8)
'TOTVALUE' => string '0.00' (length=4)
'EXPDATE' => string '2004-03-20' (length=10)
I did try casting it like this but it only makes Object with arrays inside.
$obj=(object)$array;
what can i do to have nesting multi level Objects from my array?
i improvised another way to do so:
$ar=[
'a'=>[
'field1'=>52,
'field2'=>52,
'field3'=>52,
],
'b'=>[
'field1'=>52,
'field2'=>52,
'field3'=>52,
]
];
function ToObj($data) {
if (gettype($data) == 'array')
return (object)array_map("ToObj", $data);
else
return $data;
}
$ObjectResult = array_map("ToObj", $ar);

How to take two random entries from a multidimensional array in PHP

I am looking for a way to take a multidimensional array and choose 2 random entries from the top level to create a new multidimensional array.
For example, if I have an array $data that looks like the below:
array (size=3)
0 =>
array (size=1)
0 =>
object(stdClass)[500]
public 'id' => int 2
public 'first_name' => string 'Mary' (length=4)
public 'last_name' => string 'Sweet' (length=5)
1 =>
array (size=1)
0 =>
object(stdClass)[501]
public 'id' => int 9
public 'first_name' => string 'Joe' (length=3)
public 'last_name' => string 'Bob' (length=3)
2 =>
array (size=1)
0 =>
object(stdClass)[502]
public 'id' => int 1
public 'first_name' => string 'Shag' (length=4)
public 'last_name' => string 'Well' (length=4)
How do I cut it up so that I take two of the three random entries, to get something like $data2:
array (size=2)
0 =>
array (size=1)
0 =>
object(stdClass)[500]
public 'id' => int 2
public 'first_name' => string 'Mary' (length=4)
public 'last_name' => string 'Sweet' (length=5)
1 =>
array (size=1)
0 =>
object(stdClass)[502]
public 'id' => int 1
public 'first_name' => string 'Shag' (length=4)
public 'last_name' => string 'Well' (length=4)
Use array_rand(). You could get more sophisticated depending on what you are doing, but here is the basic idea:
$randkeys = array_rand($data, 2);
$data2 = array($data[$randkeys[0]], $data[$randkeys[1]]);
See demo

How could I expose data using var_dump on Eloquent ORM Laravel?

I'm new to php development and I want to use var_dump to debug my php program on the client side.
$test= Admin::where("EPANTHERID","=",$email);
var_dump($test);die();
On the client side, I receive this
bject(Illuminate\Database\Eloquent\Builder)[187] protected 'query' => object(Illuminate\Database\Query\Builder)[186] protected 'connection' => object(Illuminate\Database\MySqlConnection)[179] protected 'pdo' => object(PDO)[180] ... protected 'readPdo' => null protected 'reconnector' => object(Closure)[185] ... protected 'queryGrammar' => object(Illuminate\Database\Query\Grammars\MySqlGrammar)[181] ... protected 'schemaGrammar' => null protected 'postProcessor' => object(Illuminate\Database\Query\Processors\MySqlProcessor)[182] ... protected 'events' => object(Illuminate\Events\Dispatcher)[14] ... protected 'paginator' => object(Closure)[184] ... protected 'cache' => object(Closure)[183] ... protected 'fetchMode' => int 8 protected 'transactions' => int 0 protected 'queryLog' => array (size=0) ... protected 'loggingQueries' => boolean true protected 'pretending' => boolean false protected 'database' => string 'xinwen_development' (length=18) protected 'tablePrefix' => string '' (length=0) protected 'config' => array (size=9) ... protected 'grammar' => object(Illuminate\Database\Query\Grammars\MySqlGrammar)[181] protected 'selectComponents' => array (size=11) ... protected 'tablePrefix' => string '' (length=0) protected 'processor' => object(Illuminate\Database\Query\Processors\MySqlProcessor)[182] protected 'bindings' => array (size=5) 'select' => array (size=0) ... 'join' => array (size=0) ... 'where' => array (size=1) ... 'having' => array (size=0) ... 'order' => array (size=0) ... public 'aggregate' => null public 'columns' => null public 'distinct' => boolean false public 'from' => string 'ADMIN' (length=5) public 'joins' => null public 'wheres' => array (size=1) 0 => array (size=5) ... public 'groups' => null public 'havings' => null public 'orders' => null public 'limit' => null public 'offset' => null public 'unions' => null public 'lock' => null protected 'backups' => array (size=0) empty protected 'cacheKey' => null protected 'cacheMinutes' => null protected 'cacheTags' => null protected 'cacheDriver' => null protected 'operators' => array (size=19) 0 => string '=' (length=1) 1 => string '<' (length=1) 2 => string '>' (length=1) 3 => string '<=' (length=2) 4 => string '>=' (length=2) 5 => string '<>' (length=2) 6 => string '!=' (length=2) 7 => string 'like' (length=4) 8 => string 'not like' (length=8) 9 => string 'between' (length=7) 10 => string 'ilike' (length=5) 11 => string '&' (length=1) 12 => string '|' (length=1) 13 => string '^' (length=1) 14 => string '<<' (length=2) 15 => string '>>' (length=2) 16 => string 'rlike' (length=5) 17 => string 'regexp' (length=6) 18 => string 'not regexp' (length=10) protected 'model' => object(Admin)[178] protected 'table' => string 'ADMIN' (length=5) protected 'fillable' => array (size=1) 0 => string 'EPANTHERID' (length=10) protected 'connection' => null protected 'primaryKey' => string 'id' (length=2) protected 'perPage' => int 15 public 'incrementing' => boolean true public 'timestamps' => boolean true protected 'attributes' => array (size=0) empty protected 'original' => array (size=0) empty protected 'relations' => array (size=0) empty protected 'hidden' => array (size=0) empty protected 'visible' => array (size=0) empty protected 'appends' => array (size=0) empty protected 'guarded' => array (size=1) 0 => string '*' (length=1) protected 'dates' => array (size=0) empty protected 'touches' => array (size=0) empty protected 'observables' => array (size=0) empty protected 'with' => array (size=0) empty protected 'morphClass' => null public 'exists' => boolean false protected 'eagerLoad' => array (size=0) empty protected 'macros' => array (size=0) empty protected 'onDelete' => null protected 'passthru' => array (size=12) 0 => string 'toSql' (length=5) 1 => string 'lists' (length=5) 2 => string 'insert' (length=6) 3 => string 'insertGetId' (length=11) 4 => string 'pluck' (length=5) 5 => string 'count' (length=5) 6 => string 'min' (length=3) 7 => string 'max' (length=3) 8 => string 'avg' (length=3) 9 => string 'sum' (length=3) 10 => string 'exists' (length=6) 11 => string 'getBindings' (length=11)
It seems like Eloquent has some mechanism which prevents me from accessing data using var_dump. How could I solve this problem or could anyone show me a better way to debug my php scripts? Thank you.
You should use:
$test= Admin::where("EPANTHERID","=",$email)->get();
or
$test= Admin::where("EPANTHERID","=",$email)->first();
if EPANTHERID is unique and then use var_dump on result.
That's because without get() or first() (and some other methods) you have in your variable only object that holds your query and not the result of running the query (the query in your code has not been launched yet).

How to add button to user profile page in drupal

I start using drupal(7) 3 days ago and I have small task :
I'm using Profile2 Module to create 2 type of users (candite/employer) and I Want to create a button in the wiew page of the profile of employer.
I create new module for that to test if is an employer profil :
function ab_candidate_app_profile2_view_alter($build) {
$test = $build['field_company_name']['#object'];
if(isset($build))
if($test->type == 'employer_'){
// add the button here
}
}
Thank you for your help
EDIT : and whene i do var_dump($build) i get next result :
array (size=12)
'#view_mode' => string 'account' (length=7)
'field_company_name' =>
array (size=16)
'#theme' => string 'field' (length=5)
'#weight' => int 0
'#title' => string 'Name' (length=4)
'#access' => boolean true
'#label_display' => string 'above' (length=5)
'#view_mode' => string 'account' (length=7)
'#language' => string 'und' (length=3)
'#field_name' => string 'field_company_name' (length=18)
'#field_type' => string 'text' (length=4)
'#field_translatable' => string '0' (length=1)
'#entity_type' => string 'profile2' (length=8)
'#bundle' => string 'employer_' (length=9)
'#object' =>
object(Profile)[56]
public 'pid' => string '6' (length=1)
public 'type' => string 'employer_' (length=9)
public 'label' => string 'Employer ' (length=9)
public 'uid' => string '10' (length=2)
public 'created' => string '1411278976' (length=10)
public 'changed' => string '1411278976' (length=10)
protected 'entityType' => string 'profile2' (length=8)
protected 'entityInfo' =>
array (size=22)
...
protected 'idKey' => string 'pid' (length=3)
protected 'nameKey' => string 'pid' (length=3)
protected 'statusKey' => string 'status' (length=6)
protected 'defaultLabel' => boolean false
public 'field_company_name' =>
array (size=1)
...
public 'field_logo' =>
array (size=1)
...
public 'field_company_description' =>
array (size=1)
...
public 'field_country' =>
array (size=1)
...
public 'rdf_mapping' =>
array (size=0)
...
public 'entity_view_prepared' => boolean true
'#items' =>
array (size=1)
0 =>
array (size=3)
...
'#formatter' => string 'text_default' (length=12)
0 =>
array (size=1)
'#markup' => string 'adel' (length=4)
'field_logo' =>
array (size=16)
'#theme' => string 'field' (length=5)
'#weight' => int 2
'#title' => string 'Logo' (length=4)
'#access' => boolean true
'#label_display' => string 'above' (length=5)
'#view_mode' => string 'account' (length=7)
'#language' => string 'und' (length=3)
'#field_name' => string 'field_logo' (length=10)
'#field_type' => string 'image' (length=5)
'#field_translatable' => string '0' (length=1)
'#entity_type' => string 'profile2' (length=8)
'#bundle' => string 'employer_' (length=9)
'#object' =>
object(Profile)[56]
public 'pid' => string '6' (length=1)
public 'type' => string 'employer_' (length=9)
public 'label' => string 'Employer ' (length=9)
public 'uid' => string '10' (length=2)
public 'created' => string '1411278976' (length=10)
public 'changed' => string '1411278976' (length=10)
protected 'entityType' => string 'profile2' (length=8)
protected 'entityInfo' =>
array (size=22)
...
protected 'idKey' => string 'pid' (length=3)
protected 'nameKey' => string 'pid' (length=3)
protected 'statusKey' => string 'status' (length=6)
protected 'defaultLabel' => boolean false
public 'field_company_name' =>
array (size=1)
...
public 'field_logo' =>
array (size=1)
...
public 'field_company_description' =>
array (size=1)
...
public 'field_country' =>
array (size=1)
...
public 'rdf_mapping' =>
array (size=0)
...
public 'entity_view_prepared' => boolean true
'#items' =>
array (size=1)
0 =>
array (size=13)
...
'#formatter' => string 'image' (length=5)
0 =>
array (size=4)
'#theme' => string 'image_formatter' (length=15)
'#item' =>
array (size=13)
...
'#image_style' => string '' (length=0)
'#path' => string '' (length=0)
'field_company_description' =>
array (size=16)
'#theme' => string 'field' (length=5)
'#weight' => int 3
'#title' => string 'Description' (length=11)
'#access' => boolean true
'#label_display' => string 'above' (length=5)
'#view_mode' => string 'account' (length=7)
'#language' => string 'und' (length=3)
'#field_name' => string 'field_company_description' (length=25)
'#field_type' => string 'text_long' (length=9)
'#field_translatable' => string '0' (length=1)
'#entity_type' => string 'profile2' (length=8)
'#bundle' => string 'employer_' (length=9)
'#object' =>
object(Profile)[56]
public 'pid' => string '6' (length=1)
public 'type' => string 'employer_' (length=9)
public 'label' => string 'Employer ' (length=9)
public 'uid' => string '10' (length=2)
public 'created' => string '1411278976' (length=10)
public 'changed' => string '1411278976' (length=10)
protected 'entityType' => string 'profile2' (length=8)
protected 'entityInfo' =>
array (size=22)
...
protected 'idKey' => string 'pid' (length=3)
protected 'nameKey' => string 'pid' (length=3)
protected 'statusKey' => string 'status' (length=6)
protected 'defaultLabel' => boolean false
public 'field_company_name' =>
array (size=1)
...
public 'field_logo' =>
array (size=1)
...
public 'field_company_description' =>
array (size=1)
...
public 'field_country' =>
array (size=1)
...
public 'rdf_mapping' =>
array (size=0)
...
public 'entity_view_prepared' => boolean true
'#items' =>
array (size=1)
0 =>
array (size=3)
...
'#formatter' => string 'text_default' (length=12)
0 =>
array (size=1)
'#markup' => string 'teeeeeeeeeeeeeeeeeeeest' (length=23)
'field_country' =>
array (size=16)
'#theme' => string 'field' (length=5)
'#weight' => int 4
'#title' => string 'Country' (length=7)
'#access' => boolean true
'#label_display' => string 'above' (length=5)
'#view_mode' => string 'account' (length=7)
'#language' => string 'und' (length=3)
'#field_name' => string 'field_country' (length=13)
'#field_type' => string 'list_text' (length=9)
'#field_translatable' => string '0' (length=1)
'#entity_type' => string 'profile2' (length=8)
'#bundle' => string 'employer_' (length=9)
'#object' =>
object(Profile)[56]
public 'pid' => string '6' (length=1)
public 'type' => string 'employer_' (length=9)
public 'label' => string 'Employer ' (length=9)
public 'uid' => string '10' (length=2)
public 'created' => string '1411278976' (length=10)
public 'changed' => string '1411278976' (length=10)
protected 'entityType' => string 'profile2' (length=8)
protected 'entityInfo' =>
array (size=22)
...
protected 'idKey' => string 'pid' (length=3)
protected 'nameKey' => string 'pid' (length=3)
protected 'statusKey' => string 'status' (length=6)
protected 'defaultLabel' => boolean false
public 'field_company_name' =>
array (size=1)
...
public 'field_logo' =>
array (size=1)
...
public 'field_company_description' =>
array (size=1)
...
public 'field_country' =>
array (size=1)
...
public 'rdf_mapping' =>
array (size=0)
...
public 'entity_view_prepared' => boolean true
'#items' =>
array (size=1)
0 =>
array (size=1)
...
'#formatter' => string 'list_default' (length=12)
0 =>
array (size=1)
'#markup' => string 'Malaysia (MY)' (length=13)
'#pre_render' =>
array (size=1)
0 => string '_field_extra_fields_pre_render' (length=30)
'#entity_type' => string 'profile2' (length=8)
'#bundle' => string 'employer_' (length=9)
'#theme' => string 'entity' (length=6)
'#entity' =>
object(Profile)[56]
public 'pid' => string '6' (length=1)
public 'type' => string 'employer_' (length=9)
public 'label' => string 'Employer ' (length=9)
public 'uid' => string '10' (length=2)
public 'created' => string '1411278976' (length=10)
public 'changed' => string '1411278976' (length=10)
protected 'entityType' => string 'profile2' (length=8)
protected 'entityInfo' =>
array (size=22)
'label' => string 'Profile' (length=7)
'plural label' => string 'Profiles' (length=8)
'description' => string 'Profile2 user profiles.' (length=23)
'entity class' => string 'Profile' (length=7)
'controller class' => string 'EntityAPIController' (length=19)
'base table' => string 'profile' (length=7)
'fieldable' => boolean true
'view modes' =>
array (size=1)
...
'entity keys' =>
array (size=4)
...
'bundles' =>
array (size=2)
...
'bundle keys' =>
array (size=1)
...
'label callback' => string 'entity_class_label' (length=18)
'uri callback' => string 'entity_class_uri' (length=16)
'access callback' => string 'profile2_access' (length=15)
'module' => string 'profile2' (length=8)
'metadata controller class' => string 'Profile2MetadataController' (length=26)
'static cache' => boolean true
'field cache' => boolean true
'load hook' => string 'profile2_load' (length=13)
'translation' =>
array (size=0)
...
'schema_fields_sql' =>
array (size=1)
...
'configuration' => boolean false
protected 'idKey' => string 'pid' (length=3)
protected 'nameKey' => string 'pid' (length=3)
protected 'statusKey' => string 'status' (length=6)
protected 'defaultLabel' => boolean false
public 'field_company_name' =>
array (size=1)
'und' =>
array (size=1)
...
public 'field_logo' =>
array (size=1)
'und' =>
array (size=1)
...
public 'field_company_description' =>
array (size=1)
'und' =>
array (size=1)
...
public 'field_country' =>
array (size=1)
'und' =>
array (size=1)
...
public 'rdf_mapping' =>
array (size=0)
empty
public 'entity_view_prepared' => boolean true
'#language' => string 'en' (length=2)
'#page' => null
I solve it by overrieding the user-profile.tpl.php with this code :
<?php print render($user_profile);
if(isset($user_profile['profile_employer_'])){
$account_info=menu_get_object('user');
$acount_id = $account_info->uid;
// var_dump($account_info);die();
echo 'Apply Now';
}
?>
I overied this file in the next path :
C:\wamp\www\jobPortal\themes\bartik\templates\
I am assuming that you need to add a button on profile display and not specifically a "view" from Views module. In that case you must use
hook_node_view
Here is an example
https://www.drupal.org/node/1993368#comment-7406538
or a check out more elaborative answer at
Drupal 7 hook_node_view add a form to the content of a node. You can add a button in the form and call on display.
In case you are using a view from "Views" module, I would suggest to better create a block in your code and place on the particular page rather than altering views. Altered views can be really difficult to maintain. It is best to keep them only for displaying DB content and not embedding forms.
Still you might like to go through the comments on this thread regarding a similar query https://drupal.stackexchange.com/questions/91186/how-do-i-embed-a-node-creation-form-inside-a-view

Categories