If I have an array like the one below what would be the best way to get all of the ListingCategory items out of the array so I can use them in an if statement within a foreach?
Array:
Array (
[0] => Array (
[ListingId] => SimpleXMLElement Object ([0] => 532712629)
[ListingCategory] => SimpleXMLElement Object ( [0] => 0350-5748-3400- )
) [1] =>
This will cycle through all array items, and all of the children within each ListingCategory.
foreach($array as $item)
{
foreach($item["ListingCategory"]->children() as $val)
{
doSomething($val);
}
}
Related
I have array like below. I would like to push it to 0 element array.
$csvdata is contain original array $pushHeaderSpec variable is what i want to push into original array i have also tried array_merge but not work as expected merge well on output only but when i print original data in csv it is not there.i m generating $csvdata array first and then append this array on last.
Array
(
[Ruder] => no value need on this
[Glas] => no value need on this
[Not] => no value need on this
)
My Multidimention array look something like
Array
(
[0] => Array
(
[0] => Sort order
[1] => Sku
[2] => Title
)
)
Many more element on above array so i just want to merge my first array keys to this array on first element that is 0.
I did try using below code but it doesn't give me output what i want.
array_push($csvdata[0],array_keys($pushHeaderSpec));
Output from code
Array
(
[0] => Array
(
[0] => Sort order
[1] => Sku
[2] => Title
[3] =>array (
[0] => Ruder
[1] => Glas
[2] => Not
)
)
)
Expecting Output
Array
(
[0] => Array
(
[0] => Sort order
[1] => Sku
[2] => Title
[3] => Ruder
[4] => Glas
[5] => Not
)
)
It was just
foreach (array_keys($pushHeaderSpec) as $key => $value) {
array_push($csvdata[0],$value);
}
Is that what you are looking for ?
foreach ($pushHeaderSpec as $key => $val) {
$csvdata[0][] = $key;
}
I have following multidimensional Array and I want to get the difference, if there is just one sub Array or multiple in that array.
For Example:
In Array [1] there is just one sub Array [example]
In Array [2] there are two sub Arrays [example]
[content] => Array
(
[...]
[1] => Array
(
[example] => Array
(
[value] => GET THIS
[attr] => Array
(
[...]
)
)
)
[2] => Array
(
[example] => Array
(
[0] => Array
(
[value] => GET THIS
[attr] => Array
(
[...]
)
)
[1] => Array
(
[value] => GET THIS
[attr] => Array
(
[...]
)
)
)
)
Now to get the [value] from the first Array I would try:
foreach ($content as $example) {
echo($content['example']['value']);
}
And to get each [value] from the second Array I would try:
foreach ($content as $example) {
foreach ($example as $values) {
echo($value['value']);
}
}
So far so good but how do I decide which function to run? Am I missing something?
Is there an if-statement which can help me there?
Something like:
if(multiple sub-arrays){
// do first code example
} else {
// do second code example
}
I simply want a method to get all values called [value] out of the array.
Thank you in advance!
The most obvious solution is to change function which generates your content array so as it always generates sub arrays in a format like:
[content] => Array
(
[...]
[1] => Array
(
[example] => Array
(
[0] => Array
(
[value] => GET THIS
[attr] => Array
(
[...]
)
)
)
)
[2] => Array
(
[example] => Array
(
[0] => Array
(
[value] => GET THIS
[attr] => Array
(
[...]
)
)
[1] => Array
(
[value] => GET THIS
[attr] => Array
(
[...]
)
)
)
)
But if you don't have such option - then use a simple check:
foreach ($content as $item) {
// here check if your `$item` has an `value` subkey under `example` key
if (array_key_exists('value', $item['example'])) {
echo($item['example']['value']);
} else {
foreach ($item['example'] as $values) {
echo ($values['value']);
}
}
}
Assuming that your final dimension allways as a 'value' node:
function arrayIterate($array){
foreach ($content as $example) {
if(!isset($example['value'])){
arrayIterate($example);
}else{
echo($example['value']);
}
}
}
What would be the most efficient way to loop through this multidemsional array? This array is much larger than the example given, and would contain all the visitors ip address who visit my site. Also I would only like to loop through the information contained in 'sc_data'.
Array
(
[#attributes] => Array
(
[status] => ok
)
[sc_data] => Array
(
[0] => Array
(
[ip_addresss] => 1
)
[1] => Array
(
[ip_address] => 1
)
)
)
The most efficient way to loop through an array is foreach
foreach($array['sc_data'] as $key => $value) {
echo $value['ip_addresss'];
}
Hi My Code is the following
if(is_a($values, 'pingidentity\opentoken\helpers\multistringarray'))
{
foreach($values->keySet() as $key)
{
foreach($values->get($key) as $value)
{
$i++;
print "<tr><td class=\"d".($i&1)."\">".$value."</td><tr>";
}
}
}
When I print_r the array output is
pingidentity\opentoken\helpers\MultiStringArray Object
(
[_values:pingidentity\opentoken\helpers\MultiStringArray:private] => Array
(
[not-before] => Array
(
[0] => 2014-06-13T19:33:15Z
)
[authnContext] => Array
(
[0] => urn:oasis
)
[email] => Array
(
[0] => test#test.com
)
[subject] => Array
(
[0] => usernametest
)
)
)
I'm looking for help on how to take the output of the array and input the values into variables
I've now did the followng to cast the object to array
$array = (array) $values;
with results as following
Array
(
[not-before] => Array
(
[0] => 2014-06-13T23:17:08Z
)
[authnContext] => Array
(
[0] => urn:oasis )
[email] => Array
(
[0] => test#test.com
)
[subject] => Array
(
[0] => usernametest
)
)
)
remember that you can access to the value of the key:
$result = array();
foreach($values as $key => $value){
if($key != 'excludeVal' && $key != 'exclude2') //here you can exclude some keys that you don't need
$result[$key] = $value;
}
after that you can use extract function
extract($result);
or even use extract($values);
this function return each key like a variable, eg: if you has a key named ["key1"] after the extract call you can use the variable $key1 and it has the value of the key
You can type cast this object to array.
OR use Object and use -> operator to access the content
I have a PHP array object that can contain zero or more values like this:
Array
(
[0] => stdClass Object
(
[id] => dkgasO05P2XpfyWW
)
[1] => stdClass Object
(
[id] => LzE6G9UQIShOUoKq
)
)
I want to loop through each value in this array and use the id in a query that returns an object that looks like this:
Array
(
[0] => stdClass Object
(
[id] => taWPlKGXHR5Y03cc
[title] => Test Document Title
[filename] => test.docx
)
)
On each iteration of the loop the query returns with one result in the form of an array object. I want to add the object to an array object that in this case would look something like this:
Array
(
[0] => stdClass Object
(
[id] => dkgasO05P2XpfyWW
[title] => Test Document Title 0
[filename] => test0.docx
)
[1] => stdClass Object
(
[id] => LzE6G9UQIShOUoKq
[title] => Test Document Title 1
[filename] => test1.docx
)
)
The query is written and working and I know I need to use a foreach loop to iterate over the array of IDs, but I don't quite get how to set it up so that the end result is an array object as listed just above. I'm using PHP & Codeigniter to do all of this.
The code of the foreach I have so far is something like this:
$child = array();
foreach ($id as $row) {
$child = $this->users_model->get_docnfo($id);
}
Thanks for reading!
You should try it with
$child = array();
foreach ($id as $row) {
$child[] = $this->users_model->get_docnfo($row->id);
}
Note the $row->id instead of $id and also the brackets after $child.