I have an array that looks like this:
array
(
[0] => personA
[1] => personB
)
and I want to add elements to each person like this:
array
(
[0] => personA
(
[0] => elemA
[1] => elemB
[2] => elemC
)
[1] => personB
)
I'm using this code:
foreach($proj as $key => $cat)
{
$proj[$key] = $this->ReturnFolders(WWW_ROOT . "img/proyectos/" . $cat);
}
That function returns an array that looks like this:
array
(
[0] => elemA
[1] => elemB
)
But obviously is not working, I get this result:
array
(
[0] => Array
(
[0] => elemA
[1] => elemB
[2] => elemC
)
[1] => Array
)
Your "like this" structure is not possible. You cannot have a single array key have two different values like that (personA and the sub-array).
You'd have to build a more complex structure:
[0] => array(
'name' => 'personA'
'values' => array('elemA', 'elemB', 'elemC')
)
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 an array, which is given below:
$test = Array
(
[0] => Array
(
[0] => stud 1
)
[1] => Array
(
[0] => stud 2
)
[2] => Array
(
[0] => stud 3
)
);
I want to add a common element to above array with out using loop. For example, I want to add "test" to each element of array. After adding "test", array will look like:
$test = Array
(
[0] => Array
(
[0] => stud 1
[1] => 'test'
)
[1] => Array
(
[0] => stud 2
[1] => 'test'
)
[2] => Array
(
[0] => stud 3
[1] => 'test'
)
);
Is there any way to add common element array with out using any kind of loop(for, foreach etc...)?
You can use array_map(), check the live demo
array_map(function($v){$v[] = 'test'; return $v;}, $array);
Below is the string that am getting by submitting a form.
qcl[]=25.17.74&qcl[]=25.20.105&qcr[]=24.01&qcr[]=24.01
How do i convert the string to Array so that i get the array in the below format.
Array( [qcl] => 25.17.74 [qcl] => 25.20.105 ) and Array( [qcr] => 24.01 [qcr] => 24.01 )
I tried exploding array with & and then exploding by = but i eneded up with this array.
Array
(
[0] => Array
(
[0] => qcl[]
[1] => 25.17.74
)
[1] => Array
(
[0] => qcl[]
[1] => 25.20.105
)
[2] => Array
(
[0] => qcr[]
[1] => 24.01
)
[3] => Array
(
[0] => qcr[]
[1] => 24.02
)
)
Please help me further.
Use parse_str()
parse_str("qcl[]=25.17.74&qcl[]=25.20.105&qcr[]=24.01&qcr[]=24.01");
print_r($qcl);
print_r($qcr);
Output
// QCL
Array (
[0] => 25.17.74
[1] => 25.20.105
)
// QCR
Array (
[0] => 24.01
[1] => 24.01
)
DEMO
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
I use this in wordpress:
$arr=get_post_meta($post->ID, false);
I receive this array:
Array (
[_edit_last] => Array ( [0] => 2)
[year_completed] => Array ( [0] => 2010 )
[designers] => Array ( [0] => )
[developers] => Array ( [0] => )
[producers] => Array ( [0] => )
[_edit_lock] => Array ( [0] => 1298159324 )
[name_en] => Array ( [0] => game 1)
[name_et] => Array ( [0] => game 2 )
[friends_meta] => Array ( [0] => )
)
How do I echo (no for, foreach, etc please) name_en data? Even print_r ($arr->name_en); doesn't work... I suppose it has to be something like - echo $arr->name_en[0]; ???
It is an array or arrays, so:
print_r($arr['name_en']);
or if you only want to get the data:
echo $arr['name_en'][0];
The -> operator is for accessing properties of objects.
echo $arr['name_en'][0] should work.
this should work
echo $arr[0]['year_completed'];
echo $arr[0]['designers'];
etc