Print array php - php

I got this array output in php:
Array
(
[blabla0] => Array
(
[0] => lalala
[1] => lelele
)
[blabla1] => Array
(
[0] => lalala
[1] => lelele
)
)
If I'd like to print by associative name, it's like this: $myArray['blabla0'][0] , and it works. But I want to do like this: echo $myArray[0][0], by index...is there any way?

Mark bakers answer is best for one option. You can convert all of the array with array_values eg
$new_array = array_values($old_array);
echo $new_array[0][0];
It depends how many times you wish to access the array
http://php.net/manual/en/function.array-values.php

Related

Array values to single array using foreach loop in PHP

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
)

get specific values of a multidimensional array in php

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.

Multidemension array to single array by combianing values

I have an array with following format.
Array
(
[0] => Array
(
[keyword] => thumbnail_follow
[content] => width
)
[1] => Array
(
[keyword] => thumbnail_resize
[content] => yes
)
)
My desire output is following
Array
(
[thumbnail_follow] => width
[thumbnail_resize] => yes
)
What I have tried so far?
array_shift() but this remove duplicate keys and output become
Array
(
[keyword] => thumbnail_follow
[content] => width
)
array_column I am able to get only single type of values. For example array_column($array, 'keyword')
Array
(
[0] => thumbnail_follow
[1] => thumbnail_resize
)
Using array_column and loop, I can get desire output but there must be some more efficient way to do it but I am known from this
Can you please help me guys?
Using array_column 's third parameter index_key you can do it. Please look at 2nd example of function.
Use it like this
print_r( array_column($array, 'content', 'keyword') );
You should get your desire output by this.
Try this
$newArr = [];
foreach($yourArr as $row) {
$newArr[$row['keyword']] = $row['content'];
}

Create a new multidimensional array from a existing array (php)

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
)
)

Can I turn an array in memory into the code representation of that array?

I'm not even entirely sure how to ask the question, but assuming I have an array in memory like this:
Array (
[0] => Array
(
[0] => Array
(
[0] => 18451
[1] => MDX
)
[1] => Array
(
[0] => 18450
[1] => NSC
)
[2] => Array
(
[0] => 18446
[1] => RL
)
)
)
Is there some existing functionality to turn it into the code version of that array? I have a # of arrays I need to do this for, nested to various degrees. So I imagine I want output something like
$arrayname[] = array(array('18451','MDX'),array('18450','NSC'),array('18446','RL'));
I can write something to do it, but I'd rather not recreate the wheel if there's an existing way to do it.
This may be all you need:
http://us.php.net/var_export
manually loop through the array recursively and create a string like how you want.
var_export could help you there i think. The other option would be looping through it manually :(

Categories