I am using following to find data.
$records = $this->ModelName->find('all', array('fields' => array('name','email')));
It gives me data in following format.
Array
(
[0] => Array
(
[ModelName] => Array
(
[name] => val
[email] => value1
)
)
[1] => Array
(
[ModelName] => Array
(
[name] => val
[email] => value1
)
)
)
Can I get the data in following format? If yes, how? I need following format of data array.
Array
(
[0] => Array
(
[name] => val
[email] => value1
)
[1] => Array
(
[name] => val
[email] => value1
)
)
This will do
$new_array = Set::classicExtract($records, '{n}.ModelName');
foreach ($records as $i => $record) {
$records[$i] = $record['ModelName'];
}
Related
Array
(
[0] => stdClass Object
(
[meta_id] => 23233
[post_id] => 4467
[meta_key] => first_name
[meta_value] => Daud
)
)
How can I echo post_id from this array for all posts using while or foreach statement?
Array
(
[classic-editor-remember] => Array
(
[0] => classic-editor
)
[_edit_lock] => Array
(
[0] => 1582905950:5
)
[_edit_last] => Array
(
[0] => 5
)
[_thumbnail_id] => Array
(
[0] => 4376
)
[slide_template] => Array
(
[0] => default
)
[_yoast_wpseo_content_score] => Array
(
[0] => 30
)
[_yoast_wpseo_primary_advisor_category] => Array
(
[0] =>
)
[title] => Array
(
[0] => Demo Daniel Wrenne, CFP, ChFC
)
[designation] => Array
(
[0] => Wrenne Financial Planing, LLC Lexington, KY
)
[client_specialities] => Array
(
[0] => Gen Y/Millennials, Medical Professionals
)
[address] => Array
(
[0] => 3223 S LEHI DR
)
[phone_number] => Array
(
[0] => 64646446486
)
[email_address] => Array
(
[0] => demo#demo.com
)
[website_url] => Array
(
[0] => a:3:{s:3:"url";s:23:"https://www.google.com/";s:4:"text";s:20:"View Advisor Profile";s:6:"target";s:4:"none";}
)
[first_name] => Array
(
[0] => Daud
)
[last_name] => Array
(
[0] => Yahya
)
)
And how can I get las_name, first_name, email, address, website url, specialities, designation and title from the above array using and loop like while or foreach loop.
This is less a WordPress question and a basic PHP foreach question.
The first example you have is an Object, so you need to access the properties, e.g. meta_id, post_id like:
// THIS IS JUST AN EXAMPLE. YOUR VARIABLE WILL CHANGE BASED ON HOW YOU GOT THE DATA. `$object_array` is how you got the data to begin with.
foreach( $object_array as $object ) {
$post_id = $object->post_id;
echo $post_id;
}
For your second example, since there is only one array key inside each array key, you would set it up like this:
// Example. you would use whatever you used to get the array to begin with as the `$array`.
foreach ($array as $item ) {
$last_name = $item['last_name'][0];
$first_name = $item['first_name'][0];
....
}
I have the following two arrays...
1) how could i get only the different key->value one?
2) how can i insert to mysql the second array?
// first array
$aa = Array
(
[t_a] => Array
(
[0] => Array
(
[f_c] => LAL
[p_r] => RN
[id] =>
[gender] => m
)
)
[t_b] => Array
(
)
[t_l] => Array
(
[0] => Array
(
[p_lev] => 2
[p_date] =>
[p_r] =>
)
)
[t_r] => Array
(
[0] => Array
(
[I_r] => 19
)
)
// second array
$bb = Array
(
[t_a] => Array
(
[0] => Array
(
[f_c] => NAN
[p_r] => RN
[id] => 1214125
[gender] => m
)
)
[t_b] => Array
(
)
[t_l] => Array
(
[0] => Array
(
[p_lev] => 2
[p_date] => 21
[p_r] => 25
)
)
[t_r] => Array
(
[0] => Array
(
[I_r] => 19
)
)
I have used the array_diff function but i get NULL.
please some one help?
$aa=(array)$aa;
$bb=(array)$bb;
$result=array_diff($aa,$bb);
It's unclear what you want. Please give an example or your desired output. Here's one possibility:
$ser_aa = array_map(function($e){return serialize($e);}, $aa);
$ser_bb = array_map(function($e){return serialize($e);}, $bb);
$diff = array_diff($ser_aa, $ser_bb);
$out = array_map(function($e){return unserialize($e);}, $diff);
print_r($out);
Output:
Array
(
[t_a] => Array
(
[0] => Array
(
[f_c] => LAL
[p_r] => RN
[id] =>
[gender] => m
)
)
[t_l] => Array
(
[0] => Array
(
[p_lev] => 2
[p_date] =>
[p_r] =>
)
)
)
I think this is a tough one! Experts only?
Ok, I have some variables (returned from get_defined_vars):
Array
(
[lead] => Array
(
[2] => fstory
[4] => him
[5] => trtr
[1] => 508b38ee02f502.23680245.png
)
[form] => Array
(
[id] => 3
)
[fields] => Array
(
[0] => Array
(
[adminLabel] => formname
[id] => 2
)
[1] => Array
(
[adminLabel] => hisher
[id] => 4
[2] => Array
(
[adminLabel] => fname
[id] => 5
)
[3] => Array
(
[adminLabel] => sign
[id] => 1
)
)
I need to get the array fields key to be the [fields] [adminLabel] and the value to be the [lead] [#].
So in this example the array would have key=value
formname = fstory
fname = trtr
hisher = his
sign = 508b38ee02f502.23680245.png
Make any sense? Possible?
Try this. It is untested.
$result_values = $array['lead'];
$results = array();
foreach ($array['form']['fields'] as $value) {
if (is_array($value)) {
$results[$value['adminLabel']] = $result_values[$value['id']];
}
}
print_r($results);
I have parsed my xml into array using xml2array.php.
It gave array like
Array(
[HotelImage] => Array
(
[0] => Array
(
)
[1] => Array
(
)
[0_attr] => Array
(
[Type] => Bar/Lounge
[URL] => http://images.gta-travel.com/HH/Images/SA/ELS/ELS-HOL1-11.jpg
)
[1_attr] => Array
(
[Type] => Lobby
[URL] => http://images.gta-travel.com/HH/Images/SA/ELS/ELS-HOL1-8.jpg
)
[2] => Array
(
)
[2_attr] => Array
(
[Type] => Exterior
[URL] => http://images.gta-travel.com/HH/Images/SA/ELS/ELS-HOL1-1.jpg
)
[3] => Array
(
)
[3_attr] => Array
(
[Type] => Recreational Facilities
[URL] => http://images.gta-travel.com/HH/Images/SA/ELS/ELS-HOL1-12.jpg
)
)
)
how can i store all _attr key values in a separate array.
// this may cause a dereference error.. if it does just run array_keys
// and stro it in a sperate var that is then passed to preg_grep
$attrKeys = preg_grep('#\d+_attr#', array_keys($xmlArray));
$attrKeys = array_flip($attrKeys);
$attributes = array_intersect_key($xmlArray, $attrKeys);
$tags = array_diff_key($xmlArray, $attributes);
I know I must be doing something simple wrong. When I do this:
echo '<pre>';
print_r($event->when);
echo '</pre>';
I get this:
Array
(
[0] => Zend_Gdata_Extension_When Object
(
[_rootElement:protected] => when
[_reminders:protected] =>
[_startTime:protected] => 2011-06-16T10:00:00.000-05:00
[_valueString:protected] =>
[_endTime:protected] => 2011-06-17T11:00:00.000-05:00
[_rootNamespace:protected] => gd
[_rootNamespaceURI:protected] =>
[_extensionElements:protected] => Array
(
)
[_extensionAttributes:protected] => Array
(
)
[_text:protected] =>
[_namespaces:protected] => Array
(
[atom] => Array
(
[1] => Array
(
[0] => http://www.w3.org/2005/Atom
)
)
[app] => Array
(
[1] => Array
(
[0] => http://purl.org/atom/app#
)
[2] => Array
(
[0] => http://www.w3.org/2007/app
)
)
[gd] => Array
(
[1] => Array
(
[0] => http://schemas.google.com/g/2005
)
)
[openSearch] => Array
(
[1] => Array
(
[0] => http://a9.com/-/spec/opensearchrss/1.0/
)
[2] => Array
(
[0] => http://a9.com/-/spec/opensearch/1.1/
)
)
[rss] => Array
(
[1] => Array
(
[0] => http://blogs.law.harvard.edu/tech/rss
)
)
)
)
)
I'm then trying to get startTime by doing this:
$StartTime = $event->when->startTime;
But I'm not getting anything.
And yet, when I do this:
pr($event->published);
I get this:
Zend_Gdata_App_Extension_Published Object
(
[_rootElement:protected] => published
[_rootNamespace:protected] => atom
[_rootNamespaceURI:protected] =>
[_extensionElements:protected] => Array
(
)
[_extensionAttributes:protected] => Array
(
)
[_text:protected] => 2011-06-15T03:32:14.000Z
[_namespaces:protected] => Array
(
[atom] => Array
(
[1] => Array
(
[0] => http://www.w3.org/2005/Atom
)
)
[app] => Array
(
[1] => Array
(
[0] => http://purl.org/atom/app#
)
[2] => Array
(
[0] => http://www.w3.org/2007/app
)
)
)
)
and I can do this:
$dateAdded = $event->published->text;
echo $dateAdded;
and I see an output...
According to to the official Zend_Gdata_Extension_When documentation, there's a method called getStartTime() which will give you the time.
If you do $event->when[0]->getStartTime() or $event->when[0]->startTime, you'll get the start time.
startTime is marked protected. You can't reference it from outside like you did. There must be a getter function 'getStartTime()' function in that object that would allow you to reference it publicly.
EDIT: Also it is returning an object array - not an single object, so you would need to reference the it like: $event[0]->getterFunction() or loop through the array with a foreach accessing the individual objects in the loop