$content="The value of this variable is {$array['1']['value']}"
I have to do this multiple times within many paragraphs for different mutli-dimensional array values. Can't seem to find the answer online. Thank you!
Edit:
I have a multi dimensional array currently being posted to the page I am posting the array to, so lets say I wanted to get the value that is currently in:
$array['1']['thisvalue']
In the same file I have a variable $content:
I am using $content to store text from an article. In this article in the $content variable, I have some fill in the blanks which I will use the array value ($array['1']['thisvalue']) to fill in. so for example:
$content="my name is_____ and my height is ____"
$array['1']['thisvalue'] would contain my name and $array['2']['thisvalue'] would contain my height
so
$content="my name is $array['1']['thisvalue'] and my height is $array['2']['thisvalue']"
Then at the end of filling in all the blanks I have
file_put_contents($file, $content);
$file is a file I have generated in another directory, and I am putting the contents into the file.
MY QUESTION IS:
How do I use my multidimensional arrays to fill in the blanks of my article so that it will expand with the correct values when i write to this separate $file file.
A foreach or any other kind of loop will do the work for you.
i.e. :
Array :
print_r($arr);
Array
(
[0] => Array
(
[some_key] => Something
)
[1] => Array
(
[some_key] => Another Something
)
[2] => Array
(
[some_key] => Again Another Something
)
[3] => Array
(
[some_key] => Something Else
)
[4] => Array
(
[some_key] => Need more imagination...
)
)
Loop :
foreach ($arr as $item) {
echo $item['some_key'] . "\n";
}
Output :
Something
Another Something
Again Another Something
Something Else
Need more imagination...
Related
I have a large multi-dimensional array with multiple occurrences of #options indexes. The following is a single array example:
FORM => Array
(
[#attached] => Array
(
[library] => quiz/quiz-form-styling
)
[text_0] => Array
(
[#type] => markup
[#markup] =>
Wherelese did Walter White work besides being a teacher?
)
[radio_1] => Array
(
[#type] => radios
[#options] => Array
(
[0] => An elder Care home
[1] => [A car wash]
[2] => A beauty saloon
[3] => For Skylers old boss
)
[#correct] => testing_correct_for radio
)
[text_2] => Array
(
[#type] => markup
[#markup] =>
)
)
In the example above, the parent array of #options is radio_1. But that is not always the case as the arrays are dynamically generated. There is no way to know in advance what the parent index would be but there is always an #options index.
What I'm trying to figure out is how to find and retrieve the data in all occurrences of #options. How can I do that?
I'd suggest iterating the set of form elements and checking if an inner #options key exists. If so, you can add the options to your array of all options.
$all_options = [];
foreach ($form_elements as $name => $settings) {
if (isset($settings['#options'])) {
$all_options[$name] = $settings['#options'];
}
}
I used the element name as key in the example code, because I thought it seemed like it would be convenient to know where the options came from, but you wouldn't have to do it that way. If you just wanted them all in one big list, you could merge them onto $all_options instead of appending them.
$all_options = array_merge($all_options, $settings['#options']);
This is assuming each of the values under FORM is an array representing one form element. If there is any nesting such that #options could appear at a deeper level, a recursive search could handle that, but if not, I think it's best to keep it simple.
You can try something like recursive function
Here is simple example for above case.
$alloptions = array();
function seach($searcharray){
foreach($searcharray as $key=>$value){
if($key == '#options'){
$alloptions[] = $searcharray[$key];
}else if(is_array($value)){
seach($value);
}
}
}
I am reading a GEDCOM-formatted family tree flat file, and producing an array from the data for staging into table. If I encounter the values CONC <some value>, then, instead of adding an element, I need to append <some value> to the value of the last element that was just inserted (regardless of dimension depth).
I tried with current(...) etc but does this work for a multidimensional associative array?
please consider following element in an array:
[#N163#] => Array ( [INDI] => Array ( [TEXT] => Some data of this person) )
if the next line reads "1 CONC including his profession"
instead of adding a line as such
[#N163#] => Array (
[INDI] => Array ( [TEXT] => Some data of this person)
[INDI] => Array ( [CONC] => including his profession) )
I would like the array to look as follows:
[#N163#] => Array (
[INDI] => Array ( [TEXT] => Some data of this person including his profession) )
What I have researched thus far:
end($theArray)
to set pointer to last inserted element followed by $theArray[key($theArray)] = .... to update this element.
But I did not get this method to work for multidimensional arrays and/or it became really messy.
And:
merging two arrays using e.g. += notation,
but this only seems to overwrite a new element, not affect the last one, if keys are same
And:
examples with foreach calls, which does not help in my case.
Hope somebody can shed some light... many thanks!
When you adding $array[#N163#][INDI][TEXT] = 'smtng'; you can save position
$pos = &$array[#N163#][INDI][TEXT];
And if you need concatenate, write
$pos .= "concate line";
I have an associated array who's last elements start with
[0] => value
moving onwards
[1] => value, [2] => value ... etc
But it wont be determined until run time how many elements there will be (1, 2 ,3 etc). So, I am trying to write a script which will pull off those last elements and store them in a separate array for looping. To do this, I will need to know how to point start the pointer at associated array element [0] and then continue until the end of the array.
How can I achieve this?
How about this?
<?php
$array = array("1","2","3","value","more","stuff","here");
if ($foundit = array_search("value",$array)) {
$new_array = array_slice($array,$foundit);
}
print_r($new_array);
Array
(
[0] => value
[1] => more
[2] => stuff
[3] => here
)
I'm assuming that you have control over the code that dynamically generates the elements that need to be added to your array. If this is the case, you can use PHP's array_push() function to append to the end of the existing array.
PHP Manual: array_push
PHP has no pointers, but if you need to iterate over whole array (no matter associative it or not) you can use foreach
like
foreach($array ask $key => $value)
echo $key . ' ' . $value;
i have array like
Array
(
[1] => Array
(
[user_info] => Array
(
[id] => 1
[name] => Josh
[email] => u0001#josh.com
[watched_auctions] => 150022 150031
)
[auctions] => Array
(
[150022] => Array
(
[id] => 150022
[title] => Title of auction
[end_date] => 2013-08-28 17:50:00
[price] => 10
)
[150031] => Array
(
[id] => 150031
[title] => Title of auction №
[end_date] => 2013-08-28 16:08:03
[price] => 10
)
)
)
so i need put in <td> info from [auctions] => Array where is id,title,end_date but when i do like $Info['id'] going and put id from [user_info] when i try $Info[auctions]['id'] there is return null how to go and get [auctions] info ?
Try:
foreach( $info['auctions'] as $key=>$each ){
echo ( $each['id'] );
}
Or,
foreach( $info as $key=>$each ){
foreach( $each['auctions'] as $subKey=>$subEach ){
echo ( $subEach['id'] );
}
}
Given the data structure from your question, the correct way would be for example:
$Info[1]['auctions'][150031]['id']
$array =array();
foreach($mainArray as $innerArray){
$array[] = $innerArray['auctions'];
}
foreach($array as $key=>$val){
foreach($val as $k=>$dataVal){
# Here you will get Value of particular key
echo $dataVal[$k]['id'];
}
}
Try this code
Your question is a bit malformed. I don't know if this is due to a lacking understanding of the array structure or just that you had a hard time to explain. But basically an array in PHP never has two keys. I will try to shed some more light on the topic on a basic level and hope it helps you.
Anyway, what you have is an array of arrays. And there is no difference in how you access the contents of you array containing the arrays than accessing values in an array containing integers. The only difference is that what you get if you retrieve a value from your array, is another array. That array can you then in turn access values from just like a normal array to.
You can do all of this in "one" line if you'd like. For example
echo $array[1]["user_info"]["name"]
which would print Josh
But what actually happens is no magic.
You retrieve the element at index 1 from your array. This happens to be an array so you retrieve the element at index *user_info* from that. What you get back is also an array so you retrieve the element at index name.
So this is the same as doing
$arrayElement = $array[1];
$userInfo = $arrayElement["user_info"];
$name = $userInfo["name"];
Although this is "easier" to read and debug, the amount of code it produces sometimes makes people write the more compact version.
Since you get an array back you can also do things like iterating you array with a foreach loop and within that loop iterate each array you get from each index within the first array. This can be a quick way to iterate over multidimensional array and printing or doing some action on each element in the entire structure.
I'm a bit struggling with the associative arrays in associative arrays. Point is that I always have to drill deeper in an array and I just don't get this right.
$array['sections']['items'][] = array (
'ident' => $item->attributes()->ident,
'type' => $questionType,
'title' => $item->attributes()->title,
'objective' => (string) $item->objectives->material->mattext,
'question' => (string) $item->presentation->material->mattext,
'possibilities' => array (
// is this even neccesary to tell an empty array will come here??
//(string) $item->presentation->response_lid->render_choice->flow_label->response_label->attributes()->ident => (string) $item->presentation->response_lid->render_choice->flow_label->response_label->material->mattext
)
);
foreach ($item->presentation->response_lid->render_choice->children() as $flow_label) {
$array['sections']['items']['possibilities'][] = array (
(string) $flow_label->response_label->attributes()->ident => (string) $flow_label->response_label->material->mattext
);
}
So 'possibilities' => array() contains an array and if I put a value in it like the comment illustrates I get what I need. But an array contains multiple values so I am trying to put multiple values on the position $array['sections']['items']['possibilities'][]
But this outputs that the values are stores on a different level.
...
[items] => Array
(
[0] => Array
(
[ident] => SimpleXMLElement Object
(
[0] => QTIEDIT:SCQ:1000015312
)
[type] => SCQ
...
[possibilities] => Array
(
)
)
[possibilities] => Array
(
[0] => Array
(
[1000015317] => 500 bytes
)
[1] => Array
...
What am trying to accomplish is with my foreach code above is the first [possibilities] => Array is containing the information of the second. And of course that the second will disappear.
Your $array['sections']['items'] is an array of items, so you need to specify which item to add the possibilities to:
$array['sections']['items'][$i]['possibilities'][]
Where $i is a counter in your loop.
Right now you are appending the Arrays to [items]. But you want to append them to a child element of [items]:
You do:
$array['sections']['items']['possibilities'][] = ...
But it should be something like:
$array['sections']['items'][0]['possibilities'][] = ...
$array['sections']['items'] is an array of items, and as per the way you populate the possibilities key, each item will have it's own possibilities. So, to access the possibilities of the item that is being looped over, you need to specify which one from $array['sections']['items'] by passing the index as explained in the first answer.
OR
To make things simpler, you can try
Save the item array (RHS of the first =) to a separate variable instead of defining and appending to the main array at the same time.
Set the possibilities of that variable.
Append that variable to the main $array['sections']['items']
I have:
array[{IsChecked: true, SEC: 0, STP: 0},
{IsChecked: ture ,SEC: 0, STP: 1},
{IsChecked: false, SEC: 1 ,STP: 0}]
How to get each SEC where IsCheked value is true?