Multidemension array to single array by combianing values - php

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'];
}

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.

Print array 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

Convert an array of 2-element arrays to an array making 2 elements as key => value

Is it possible to convert the following array using some PHP built-in functions to a array that contain the value of id as key and the value of label as the associated value? If not what's the efficient way?
Thanks.
Input Array:
Array
(
[0] => Array
(
[id] => 2
[label] => MTD-589
)
[1] => Array
(
[id] => 3
[label] => MTD-789
)
)
Output Array:
Array
(
[2] => MTD-589,
[3] => MTD-789,
)
I don't know any built in function, but I'll do it this way:
assuming $originalArray as the array you want to convert
$newArray = array();
foreach ($originalArray as $element)
$newArray[$element["id"]] = $element["label"];
output the result
var_dump($newArray);
Introducing array_column (still in PHP 5.5 Beta).
$new_array = array_column($your_array 'label', 'id');
OUTPUT:
Array
(
[2] => MTD-589,
[3] => MTD-789,
)
Using array_walk.
array_walk($array, function($a) use (&$return) { $return[$a['id']] = $a['label']; });
print_r($return);
if you can make it so that one array has your IDs and one array has your labels, you can use array_combine to merge the two as key/value http://php.net/manual/en/function.array-combine.php

Printing Our Array Data

In PHP, Codeigniter: my array, $phoneList, contains the following:
Array
(
[name] => One, Are
[telephonenumber] => 555.222.1111
)
Array
(
[name] => Two, Are
[telephonenumber] => 555.222.2222
)
Array
(
[name] => Three, Are
[telephonenumber] => 555.222.3333
)
How do I list each name out? Each number out? Am I right in saying my array contains three different Arrays? And is that normal, for an array to contain arrays?
When I do a print_r($phoneList), I get the following:
Array ( [0] => Array ( [name] => One, Are [telephonenumber] => 555.222.1111 ) [1] => Array ( [name] => Two, Are [telephonenumber] => 555.222.2222 ) [2] => Array ( [name] => Three, Are [telephonenumber] => 555.222.3333 ) )
You'll probably want to use foreach to loop through them. Something like this:
foreach($data as $arr) { // assuming $data is the variable that has all this in
echo $arr['name'].": ".$arr['telephonenumber']."<br />";
}
Here is the solution. Foreach is the easiest approach.
It's completely normal to have an array of arrays (in this case an array of associative arrays). They can be written like so:
$arrayofarray = array(array('name' => 'aname', 'phone'=>'22233344444'), array('name' => 'bobble', 'phone'=>'5552223333'));
print_r($arrayofarray);
and you should be able to print out the content in this way:
foreach ($arrayofarray as $arr){
print $arr['name']."\n";
print $arr['phone']."\n";
}
If you want to know what terms are set in each associative array you can use array_keys() to return them (as a simple array). For example:
foreach ($arrayofarray as $arr){
$setterms=array_keys($arr);
foreach ($setterms as $aterm){
print "$aterm -> ".$arr[$aterm]."\n";
}
}

Categories