This question already has answers here:
Looping a multidimensional array in php
(3 answers)
How to load Google Maps in wpf window?
(2 answers)
Closed 5 years ago.
i have arrays like this
Any idea how to access #items subset with foreach or somthing fast like this?
Array
(
[0] => Array
(
[#theme] => item_list
[#items] => Array
(
[0] => Array
(
[nid] => 1
[changed] => 1514034947
[title] => TITLE 1
)
[1] => Array
(
[nid] => 2
[changed] => 1514034947
[title] => TITLE 2
)
)
)
[1] => Array
(
[#theme] => pager
)
BEST
As your elements are located below 3 levels so you need to use foreach thrice like below:
foreach($data as $items_list){
foreach($items_list[#items] as $items){
foreach($items as $val){
echo $val['nid'];
}
}
}
You can simply count array everytime and add condition before foreach loop to check if further array exists or not.
Like you can count main array and add condition with while loop and then use foreach like below:
$count = 0;
while($count<count($array)){
foreach($array[$count]['#items'] as $items ){
foreach($items as $val){
echo $val['nid'];
}
}
$count = $count+1;
}
Before doing anything, its better if you debug your multiple arrays like below:
echo "<pre>"; print_r($array); die;
You can use a nested foreach to get the final item list from this array object...
foreach($data as $items_list)
{
foreach($items_list[#items] as $items)
{
// do something here....
}
}
Assuming the array structure is exactly as you posted here is something you can experiment with. I don't know what you want to do with each item so this example just prints out the elements of each one:
foreach( $array as $outerarray )
{
foreach( $outerarray['#items'] as $item )
{
echo "{$item['nid']} {$item['changed']} {$item['title']}\n";
}
}
Related
Array
(
[0] => Array
(
[datas] => Array
(
[res] => 1
[rows] => Array
(
[0] => stdClass Object
(
[a] => 11
[b] => 901
[c] => 2
[d] => 2
[e] => A
[f] => BT
[g] => arushi
[h] => arushi#gmail.com
[i] => 123445
[j] => 12355.jpg
)
)
)
)
[1] => Array
(
[datas] => Array
(
[res] => 1
[rows] => stdClass Object
(
[person_name] => arushi
)
)
)
)
if i get the value in that kind off array how can i get the value of
both partially with different variable m not able to understand the
structure of the array..
need to get the value in table seperatly in different different table with same page how i can get the values
You would need to do a foreach loop. The only problem is that if you want to make it dynamic and grab the data by itself without you telling it what to get. If not it means you would have to know exactly what you need and the structure of your results. But seeing that there is a pattern you can do some checks until you reach rows. For example ($array would be the variable that has the data you provided):
foreach ($array AS $arr) {
// To make you function/check work faster,
// before even entering to fetch for data
// you can check if it has any data, else you
// probably will end up with an error
if (isset ($arr ['datas']) && $arr ['datas']) {
foreach ($arr ['datas'] AS $data) {
if (isset ($arr ['res']) && 0 < $arr ['res']) {
// here is where it gets tricky because
// from you example its either an array or an object
// but the kool part about it is that you can convert an object into an array
// or just leave it as is because the foreach will take care of it either way :)
// first check it it is an object and convert if yes
$row = $arr ['rows'];
if (!is_array ($row))
$row = (array)$row; // converting it to array is super easy :)
foreach ($row AS $key=>$dat) {
// from here your result can either be another array or just data
// so you run the check once more
if (is_array ($dat)) {
foreach ($dat AS $k=>$d) {
echo "data for $k is $d";
}
}
else
echo "data for $key is $dat";
}
}
}
}
}
You can use foreach to loop through the arrays which have keys starting from 0 to n. So that you can foreach through the main array to get the datas array for all the keys. For getting rows childs you can use another foreach inside it to get each items. But for rows it is object , so you have to use -> to select the key. see below example code. But on your array the second array consist different formate in rows array. so make it like a common formate to loop through it easily.
For eg :-
foeach($parent_array as $array){
$data=$array['datas'];
$res = $data['res'];
$rows=$data['rows'];
foreach($rows as $row){
echo $row->a; // to get the value of key a = 11
}
}
This question already has answers here:
Deleting an element from an array in PHP
(25 answers)
Closed 8 years ago.
I want to search an associative array and when I find a value, delete that part of the array.
Here is a sample of my array:
Array
(
[0] => Array
(
[id] => 2918
[schoolname] => Albany Medical College
[AppService] => 16295C0C51D8318C2
)
[1] => Array
(
[id] => 2919
[schoolname] => Albert Einstein College of Medicine
[AppService] => 16295C0C51D8318C2
)
[2] => Array
(
[id] => 2920
[schoolname] => Baylor College of Medicine
[AppService] => 16295C0C51D8318C2
)
}
What I want to do is find the value 16295C0C51D8318C2 in the AppService and then delete that part of the array. So, for example, if that code was to run on the above array, it was empty out the entire array since the logic matches everything in that array.
Here is my code so far:
foreach($this->schs_raw as $object) {
if($object['AppService'] == "16295C0C51D8318C2") {
unset($object);
}
}
array_filter will help (http://php.net/manual/en/function.array-filter.php)
$yourFilteredArray = array_filter(
$this->schs_raw,
function($var) {
return $object['AppService'] != "16295C0C51D8318C2"
}
);
Try like this:
foreach ($this->schs_raw as &$object) {
if($object['AppService'] == "16295C0C51D8318C2") {
unset($object);
}
}
Eventually:
foreach ($this->schs_raw as $k => $object) {
if($object['AppService'] == "16295C0C51D8318C2") {
unset($this->schs_raw[$k]);
}
}
Try this:
foreach($this->schs_raw as $key=>$object) {
if($object['AppService'] == "16295C0C51D8318C2") {
unset($this->schs_raw[$key]); // unset the array using appropriate index
break; // to exit loop after removing first item
}
}
Why not create a new array? If match not found, add index to new array... If it is found, don't add it. Your new array will only contain the data you want.
I've got an multi-dimensional array at the moment and want to remove the second-level of arrays and have the value of that second level as the new index value on the parent array. My current array is:
Array ( [0] => Array ( [connectee] => 1 ) [1] => Array ( [connectee] => 6 ) )
And want from that:
Array ( [0] => 1, [1] => 6 )
I was poking around the usort function but couldn't get it to work (where $current_connections is my array as above:
function cmp($a, $b) {
return strcmp($a["connectee"], $b["connectee"]);
}
$current_connections = usort($current_connections, "cmp");
The key doesn't need to be maintained (should be destroyed in the process).
foreach ($array as &$value) {
$value = $value['connectee'];
}
Note: Please note that the question statement is very confusing and contradicting, but this answer is based upon your statement for expected output
Array ( [0] => 1, [1] => 6 )
You could do
<?php
$values=array();
$values[0]=array("connectee"=>1);
$values[1]=array("connectee"=>6);
foreach($values as $index=>$value)
{
$values[$index]=$value["connectee"];
}
print_r($values);
?>
I have created an array, as follows:
$results = array();
do {
$results[] = $row_products;
} while ($row_products = mysql_fetch_assoc($products));
print_r($results);
This prints out the array like this:
Array (
[0] => Array (
[productName] => product1
)
[1] => Array (
[productName] => product2
)
[2] => Array (
[productName] => product3
)
I want to now use say the second item in the array in another mysql query.
But I cannot define it. I have tried
$results[1];
but this does not work.
So in effect, if I echo the second item, it would print 'product2'.
You should learn the basics about arrays here: http://www.php.net/manual/en/language.types.array.php
You are using a nested array, so you have the access it like this:
echo $results[1]['productName'];
Another solution would be to use $results[] = $row_products['productName']; and then just echo $results[1].
In addition, you should use a while loop instead of a do/while loop because $row_products does not seem to be defined for the first iteration.
while ($row_products = mysql_fetch_assoc($products)) {
$results[] = $row_products;
}
try this :
echo $results[1]['productName'] ;
$results[1] is an array, If you want see the array print_r($results[1]);
I have an array that looks like the one below. I'm trying to group and count them, but haven't been able to get it to work.
The original $result array looks like this:
Array
(
[sku] => Array
(
[0] => 344
[1] => 344
[2] => 164
)
[cpk] => Array
(
[0] => d456
[1] => d456
)
)
I'm trying to take this and create a new array:
$item[sku][344] = 2;
$item[sku][164] = 1;
$item[cpk][d456] = 1;
I've gone through various iterations of in_array statements inside for loops, but still haven't been able to get it working. Can anyone help?
I wouldn't use in_array() personally here.
This just loops through creating the array as it goes.
It seems to work without needing to first set the index as 0.
$newArray = array();
foreach($result as $key => $group) {
foreach($group as $member) {
$newArray[$key][$member]++;
}
}