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'];
}
Related
I wonder if there is better (faster) way to search for value in multidimensional array than looping through every item.
Lets say i have
$id_to_search = '16819976033';
And array which is pretty big
Array
(
[0] => Array
(
[id] => Array
(
[0] => 16771055710
[1] => 16776555710
[2] => 16819976033
)
[o] => 21566
[p] => 12597.66
)
[1] => Array
(
[id] => Array
(
[0] => 14089762
)
[o] => 12606
[p] => 1747.49
)
etc ...
)
I can find it if i loop through each item and than compare them but its very slow because array is big.
You can use by array_search function in PHP:
$key = array_search($id_to_search, array_column($YourArray, 'id'));
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']);
}
}
}
I am working on an indexing proj. its all fine up until when i have to udate the index file. i want whe a website is deleted by the admin, all its index posting lists should be deleted. please help by pointing me to the write way of removing an array from a mutlt dimensional array.
given an array like bellow, 3 is the id of the indexed website in the database. the admin deletes that website. I want also to be able to remove its posting references from my index.
Array
(
[mr] => Array
(
[3] => Array
(
[frequency] => 3
[position] => Array
(
[0] => 16
[1] => 94
[2] => 110
)
)
)
[smith] => Array
(
[3] => Array
(
[frequency] => 3
[position] => Array
(
[0] => 17
[1] => 95
[2] => 111
)
)
)
)
lets call that array $index.
How can someone unset or delet all arrays that have a key of 3. meaning that i will be left with an array like this.
Array
(
[mr] => Array
(
)
[smith] => Array
(
)
)
Very simple.
Loop through the first level array, and then loop through each second level array. If the second level key is 3, set the value at the first level array to be an empty array.
foreach($array as $key => $val) {
foreach($val as $key2 => $val2) {
if($key2 == 3) { unset($array[$key][$key2]); }
}
}
Edited in response to below comment.
I'm a newbie to this associative array concept in PHP. Now I'm having an array named $sample as follows:
Array
(
[name] => definitions
[text] =>
[attributes] => Array
(
[name] => Mediation_Soap_Server_Reporting
[targetnamespace] => https://mediation.moceanmobile.net/soap/reporting
)
[children] => Array
(
[types] => Array
(
[0] => Array
(
[name] => types
[text] =>
[attributes] => Array
(
)
[children] => Array
(
[xsd:schema] => Array
(
[0] => Array
(
[name] => schema
[text] =>
[attributes] => Array
(
[targetnamespace] => https://mediation.moceanmobile.net/soap/reporting
)
[children] => Array
(
[xsd:complextype] => Array
(
[0] => Array
(
[name] => complextype
[text] =>
[attributes] => Array
(
[name] => Mediation_Soap_FaultMessage
)
[children] => Array
(
[xsd:sequence] => Array
(
[0] => Array
(
[name] => sequence
[text] =>
[attributes] => Array
(
)
)
)
)
)
)
)
)
)
)
)
)
)
)
From the above array I want to refer(or access) to the key xsd:schema. But I'm not able to do it. Can you please tell me how should I access or refer this key from the associative array names $sample? Thanks in advance.
To access this value you would use:-
$sample['children']['types'][0]['children']['xsd:schema'];
If you have multiple of these elements in your types array you will need to loop through them:-
foreach($sample['children']['types'] as $type) {
if(isset($type['children']) && isset($type['children']['xsd:schema'])) {
// Perform action on element
$type['children']['xsd:schema'];
}
}
If you do not know your structure (as in xsd:schema can occur outside of types) then you will need to write a recursive function or loop for finding it.
I guess your goal is to seek for the key/value pair where the key is "xsd" ?
If so, in PHP, you can do so by using the follwing logic:
while (list($key, $value) = each($arr)) {
echo "Key: $key; Value: $value<br />\n";
}
// OR
foreach ($arr as $key => $value) {
echo "Key: $key; Value: $value<br />\n";
}
Just add a set of recursing or nested loops to traverse the structure until you find the proper key.