My current project consist of multidimensional arrays in which it holds a date and some text contents.
i already used the normal arrays in my project and array_push is used for inserting an element to array. no i'm stuck in multidimensional array in which i don't know how to insert and display multi dimensional array(beginner to multi dimensional arrays).
i found a lot of results from stackoverflow itself, but none of them helped for me. i created a multidimensional array like this
$complaints = array(
$each_complaints => array(
"date" => "",
"text" => ""
)
);
then i want to add data's into this array on the loop of mysql result
<?php foreach($query_56 as $notes):
// eg: array_push " $notes->date , $notes->corresponding_text "
endforeach; ?>
and i want to display the array like this
array[date][text]=> [2014-11-18] [1st complaint]
array[date][text]=> [2015-01-15] [2nd complaint]
how can i achieve this, im begginer to multi dimesional arrays.
Any help would be greatly appreciated.
<?php foreach($query_56 as $key=>$notes):
$each_complaints[$key]["date"] = $notes->date;
$each_complaints[$key]["text"] = $notes->corresponding_text ;
endforeach;
echo "<pre>";print_r($each_complaints);
?>
**Output :**
(
[0] => Array
(
[date] => 01-11-2014
[text] => rrrrrr
)
[1] => Array
(
[date] => 02-11-2014
[text] => fffff
)
[2] => Array
(
[date] => 03-11-2014
[text] => ddddd
)
)
Related
I am working with php and arrays, I have multiple arrays like following
Array
(
[0] => Array
(
[wallet_address] => 0x127e61982701axxxxxxxxxxxxxxxxxxxxxxxxxxx
)
[1] => Array
(
[wallet_address] => 0xf80a41eE97e3xxxxxxxxxxxxxxxxxxxxxxxxxxxx
)
[2] => Array
(
[wallet_address] => 0x24361F1602bxxxxxxxxxxxxxxxxxxxxxxxxxxxxx
)
and so on....
And i want to make them in single array with comma like following way
$set = array("0x127e61982701axxxxxxxxxxxxxxxxxxxxxxxxxxx","0xf80a41eE97e3xxxxxxxxxxxxxxxxxxxxxxxxxxxx","0x24361F1602bxxxxxxxxxxxxxxxxxxxxxxxxxxxxx");
How can i do this ?Here is my current code but not working,showing me same result(0,1,2 keys),Where i am wrong ?
$GetUserFollower; //contaning multiple array value
$set=array();
foreach($GetUserFollower as $arr)
{
$set[]=$arr;
}
echo "<pre>";print_R($set);
The original array is an Assoc array and therefore the wallet_address needs to be addressed specifically in a loop. Or you could use the array_column() builtin function to achieve the same thing.
$GetUserFollower; //contaning multiple array value
$set=array();
foreach($GetUserFollower as $arr)
{
$set[] = $arr['wallet_address'];
}
echo "<pre>";print_r($set);
Or
$new = array_column($GetUserFollower, 'wallet_address');
print_r($new);
RESULT
Array
(
[0] => 0x127e61982701axxxxxxxxxxxxxxxxxxxxxxxxxxx
[1] => 0xf80a41eE97e3xxxxxxxxxxxxxxxxxxxxxxxxxxxx
[2] => 0x24361F1602bxxxxxxxxxxxxxxxxxxxxxxxxxxxxx
)
Your comments are making me think you want an array without a key, which is impossible. If you do this with the example you show in your comments
$set = array("0x127e61982701axxxxxxxxxxxxxxxxxxxxxxxxxxx","0xf80a41eE97e3xxxxxxxxxxxxxxxxxxxxxxxxxxxx","0x24361F1602bxxxxxxxxxxxxxxxxxxxxxxxxxxxxx");
print_r($set);
You will see
Array
(
[0] => 0x127e61982701axxxxxxxxxxxxxxxxxxxxxxxxxxx
[1] => 0xf80a41eE97e3xxxxxxxxxxxxxxxxxxxxxxxxxxxx
[2] => 0x24361F1602bxxxxxxxxxxxxxxxxxxxxxxxxxxxxx
)
This question already has answers here:
Two arrays in foreach loop
(24 answers)
Closed 2 years ago.
I would like to collate data from multiple different data sources into one numeric array using foreach loops, however I am having some trouble building my array in the expected format. I would like to keep the key numeric.
For example, if I had two data sources: $fruit_data and $flavor_data and my code was:
foreach($fruit_data as $fruit){
$fruits[]['name'] = $fruit['name'];
}
foreach($flavor_data as $flavor){
$fruits[]['flavor'] = $flavor['taste'];
}
The data is added to the array with separate numeric keys, like this:
Array
(
[0] => Array
(
[name] => Example Name 1
)
[1] => Array
(
[flavor] => Example Flavor 1
)
[2] => Array
(
[name] => Example Name 2
)
[3] => Array
(
[flavor] => Example Flavor 2
)
)
However, my desired output is to display the data under the same numeric key, like this:
Array
(
[0] => Array
(
[name] => Example Name 1
[flavor] => Example Flavor 1
)
[1] => Array
(
[name] => Example Name 2
[flavor] => Example Flavor 2
)
)
Could somebody please explain where I am going wrong, and if possible, link me to a resource that explains how to overcome this issue? It may be that I have a fundamental misunderstanding of multidimensional arrays, but I cannot seem to find any references to this issue.
Any help is much appreciated.
foreach($fruit_data as $index => $fruit){
$fruits[] = [
'name' => $fruit['name'],
'flavor' => $flavor_data[$index]['taste']
];
}
Same as
foreach($fruit_data as $index => $fruit){
$fruits[] = [
'name' => $fruit_data[$index]['taste'],
'flavor' => $flavor_data[$index]['taste']
];
}
I am searching for the best way to get specific values in a multidimensional array. My array looks like this. I dont know how many Arrays i get back so i need this in a foreach.
e.g. I want now to return from every array the [event_budget][0] value. How can i achieve this?
Array
(
[0] => Array
(
[event_budget] => Array
(
[0] => Bis € 4000
)
[event_date] => Array
(
[0] => 27.10.2019
)
[event_date_timestamp] => Array
(
[0] => 1572134400
)
)
[1] => Array
(
[event_budget] => Array
(
[0] => Bis € 500
)
[event_date] => Array
(
[0] => 29.10.2019
)
[event_date_timestamp] => Array
(
[0] => 1572307200
)
)
)
There are different ways in which you can get your required result.
The most basic :
$finalArray = array();
foreach($result as $key => $value){
$finalArray[] = $value['event_budget'][0];
}
The $finalArray will have your required result. You can also use like #rakesh have mentioned.
array_map('array_shift',array_column($a, 'event_budget'));
It would be great if you have control over the main array. If that is the case you can avoid event_budget to be an array.
If possible, it should be made like this:
[event_budget] => Array
(
[0] => Bis € 500
)
[event_budget] => Bis € 500
Hope it was helpful.
Thanks
You can use array_map with array_column and array_shift
$aa = array_map('array_shift',array_column($a, 'event_budget'));
print_r($aa);
Working example : https://3v4l.org/fKpjY
Start with:
print_r(array_column($array, 'event_budget'));
But as event_budget is array too, you need to get columns with index 0:
print_r(array_column(
array_column($array, 'event_budget'),
0
));
Full demo.
I need to create a array from another one that already exists, making a new data structure.
At this point I already have the new array
http://phpfiddle.org/main/code/x0gn-1ikz
The problem is:
At the new array in the 10:00:00 - 15:00:00 should apear 3 records:
Array
(
[10:00:00 - 15:00:00] => Array
(
[2014-08-03] => Jonh
[2014-08-04] => Jonh
[2014-08-04] => Lewis
)
But as you can see in the fidle only two of the apear, I belive this is a simple trick, but had blowed my head, the "problem" is in the records that have a date ans time equal....
Can I have some help please?
Change 114th line of your code to this:
$final[$time][$value[2]][] = $value[1];
to create array of names. print_r($final) will then print:
Array
(
[10:00:00 - 15:00:00] => Array
(
[2014-08-03] => Array
(
[0] => Jonh
)
[2014-08-04] => Array
(
[0] => Jonh
[1] => Lewis
)
)
Is that what you want?
Let's say array shown above is $table_array. Now to write name in column you use something like:
echo '<td>'.$table_array['10:00:00 - 15:00:00']['04-08-2014'].'</td>';
instead, you would write something like:
$names = '';
foreach ($table_array['10:00:00 - 15:00:00']['04-08-2014'] as $name)
$names .= $name . ', ';
$names = rtrim($names, ', ');
echo '<td>'.$names.'</td>';
If you do it different way then please post your code so I know how to help you
Your problem is duplicate key. For an instance, you try to push both jonh and lewis at the index of [10:00:00 - 15:00:00][2014-08-03] You need another array there, so the structure will be a 3D array.
One plausible structure looks like,
Array
(
[10:00:00 - 15:00:00] => Array
(
[2014-08-03] => Array
(
[0] => Jonh
)
[2014-08-04] => Array
(
[0] => Lewis
[1] => Jonh
)
)
I have three arrays each having SimpleXML Objects in them. They are structured like so:
Array
(
[0] => SimpleXMLElement Object
(
[post_id] => 1476
[name] => Johnson Fisheries Ltd.
[owner] => Mr. John Johnson
)
)
I want to be able to compare all 3 arrays and filter out the differences so that the results have only the elements that are the same in all 3 arrays.
For example:
Array1
(
[0] => 1476
[1] => 1560
[2] => 1342
)
Array2
(
[0] => 2454
[1] => 1476
)
Array3
(
[0] => 3412
[1] => 7512
[2] => 2454
[4] => 1476
)
The resulting array would only contain [0] => 1476
What's the best way to do this? I've looked for a function that will compare arrays in this way but I have had no luck. Any ideas?
Any help is very appreciated!
Best option will be using php's built-in array-intersect function.
$answer = array_intersect($Array1,$Array2,$Array3);
Loop over your first array, checking it against the 2nd and 3rd using in_array, like this:
foreach($array1 as $post_id) {
if(in_array($post_id, $array2) && in_array($post_id, $array3)) {
// We have a winner!
}
}
create a new array.
add all elements of the first array into the new array.
iterate through all the other elements, find out which elements in your new array are in the array to be compared. remove all the elements that don't appear.
at the end of your iterations, you have your desired array.